Converting CSV file to LIBSVM compatible data file using python

admin

Administrator
Staff member
I am doing a project using libsvm and I am preparing my data to use the lib. How can I convert CSV file to LIBSVM compatible data?

<blockquote>
CSV File:
<a href="https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/datasets/data/iris.csv" rel="nofollow">https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/datasets/data/iris.csv</a>
</blockquote>

In the frequencies questions:

<blockquote>
How to convert other data formats to LIBSVM format?

It depends on your data format. A simple way is to use libsvmwrite in the libsvm matlab/octave interface. Take a CSV (comma-separated values) file in UCI machine learning repository as an example. We download SPECTF.train. Labels are in the first column. The following steps produce a file in the libsvm format.
</blockquote>

Code:
matlab&gt; SPECTF = csvread('SPECTF.train'); % read a csv file
matlab&gt; labels = SPECTF(:, 1); % labels from the 1st column
matlab&gt; features = SPECTF(:, 2:end); 
matlab&gt; features_sparse = sparse(features); % features must be in a sparse matrix
matlab&gt; libsvmwrite('SPECTFlibsvm.train', labels, features_sparse);
The tranformed data are stored in SPECTFlibsvm.train.
Alternatively, you can use convert.c to convert CSV format to libsvm format.

but I don't wanna use matlab, I use python.

I found this solution as well using <a href="http://nayefreza.wordpress.com/2013...le-to-libsvm-compatible-data-file-using-java/" rel="nofollow">JAVA</a>

Can anyone recommend a way to tackle this problem ?