Converting CSV file to LIBSVM compatible data file using python


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?

CSV File:
<a href="" rel="nofollow"></a>

In the frequencies questions:

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.

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="" rel="nofollow">JAVA</a>

Can anyone recommend a way to tackle this problem ?