Archivo de la etiqueta: java

Robust PCA in Java using R

If you want to avoid the curse of dimensionality when working with large datasets, you need to apply certain methods to reduce the number of attributes/features of your data. One common technique is Principal Component Analysis (PCA), but it does not work properly in datasets with outliers. Hence, Robust PCA technique was developed to overcome this issue.

I have been looking for a Java implementation of Robust PCA, but unfortunately I couldn’t find any. Luckily, I came upon an R implementation available in rrcov package. My initial plan was to translate this method to Java language, but it was going to take a lot of time. Therefore, I developed a Java wrapper that connects with R interface using JRI library, which is included in rJava package (a low-level R to Java interface). You can get the code here: (mirror).

If you are looking for a Robust PCA Matlab implementation you can get the code of LIBRA project, a Matlab library for robust analysis, but I encourage you to use R instead for a number of reasons.

Image: Copyright by Matthias Scholz


How to import MOA into Eclipse

Several people have asked me how to set up MOA (Massive Online Analysis) for developing new classifiers. With this quick tutorial I explain how to load MOA source code in Eclipse for Windows, although it should be very similar in Linux or Mac.

  1. Download and install the latest version of Java SDK (not JRE) if you don’t have it already.
  2. Download the latest version of Eclipse (Classic should be enough). Extract it in any suitable location of your hard drive.
  3. Download and install TortoiseHg (or similar) in order to clone the MOA repository.
  4. Open TortoiseHG and go to File, Clone repository. Set the Source to and the Destination to a folder of your choice (e.g. C:\Users\Manuel\moa-hg). Click on Clone button and you’ll get the code of MOA in your chosen folder.
  5. Open Eclipse and go to Help, Install new software. Set “Work with” to your Eclipse repository (in my case is Juno). Look for m2e plugin and install it (also you can install the slf4j for Maven plugin).
  6. Once Eclipse is restarted, go to File, Import. Select “Existing Maven Projects” option. Set “Root Directory” to your MOA code folder (in my case C:\Users\Manuel\moa-hg). Follow all the steps of the wizard. It is possible that you get this error, but just ignore it. Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:license-maven-plugin:1.1:add-third-party (execution: add-third-party, phase: generate-resources). Eclipse will restart again.
  7. Now you should have three projects folder in the Package Explorer tab: moa, moa-pom and weka-package. For running MOA GUI, look for moa/src/main/java/moa.gui/ file and run it as a Java application.

Please, let me know if you have any problem with this steps or if you know how to solve the error of step 6. Also you can subscribe and participate in MOA development email list.

Saving and loading tasks in MOA

MOA (Massive Online Analysis) is a great opensource and free software for data stream mining. If you are an enthusiastic of machine learning, classification or clustering, you should try it.

You can work with MOA using the GUI, as well as the command line interface via Java. If you are using the graphical interface, you can’t save and load previous tasks. Because of this, i have done some modifications in order to make my work easy.

As you can see in the image, I have add a couple of buttons to the interface:

  • Save Log: create a log file with the strings of the tasks previously executed (you can select one or more from the task list).
  • Set task manually: load a panel in which you can write the command of the task to run (for example, you can copy and paste a task from a previous log file).

The patch for moa.gui.TaskManagerPanel class is available in pastebin.

Calcular edad en Java

Me ha costado averiguar cómo se calcula la edad de una persona usando Java, así que dejo aquí mi solución por si a alguien más le pudiera servir.

Gracias a Blaxter por el chivazo del plugin de resaltado.

Leer un fichero de texto en Java

Me ha costado demasiadas búsquedas en Google cómo leer un mísero archivo de texto en Java. Os dejo la solución que encontré aquí.

En vez de de un FileReader también se puede usar un InputStreamReader.