Hello, I have a file with data in SVMlight format, which have a progressive number and the “:” sign before each feature, and the label at the start of the row in numerical form (I have 2 classes, so the labels are “+1” and “-1”) which looks like this:
etc.
If I import the file with the File Reader I still get the progressive number with the “:”, and for example “1:0.34541”, “2:0.36456”, etc. are read as strings.
How can I convert them into useful data, directly into Knime, and get rid of the “1:”, “2:”, “3:”, etc.?
The Cell Splitter node or the Java Snippet node could help you.
Unfortunately they both operate only on one column - so you would need to insert 6 nodes for your input file example.
With the Java Snippet node you could replace the column with a new column of type Double and the code would probably look like this: String c = $Col6$;
String val = c.substring(c.indexOf(’:’) + 1);
return Double.parseDouble(val);
Thank you, it works
But the result just has 3 decimals, while the original is way longer, can it be fixed? (I’m not a Java expert)
And another question: I forgot to mention that I have 62 features plus the class column (the 6 features were just for example), so putting 62 nodes is not practicable. Do you have any advice on how to iterate on all the columns?
Ugly, but may work: You could read each line of your file as into one string cell, use the Java Snippet node to get rid of the 1:, 2:, etc
return $Col1$.replaceAll(" \d+:", " ");
and then use the cell splitter to get the columns.
The digits of the numbers are not lost, but the render in the table view only displays three digits by default. This can be changed by right-clicking on the column head and changing the renderer to “full precision”.
As Thorsten said two comments above: You could try to convince the file reader to the entire line as a single cell, process this entire string with some regular expression and later on split up the elements into individual columns.
Alternatively, you combine all your “5:0.12764” cells into a collection cell (there is a node “Create Collection Column”), which can then be processed with the java snippet node. The content of this cell is available as array to the java snippet node. As for the return type, you would use “Double” + check the “Array Return” field down below and later on split up the result collection using a “Split Collection Column” node.