title: Converting CSV file to LIBSVM compatible data file using python
body: <p>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?</p>
  
  <blockquote>
    <p>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></p>
  </blockquote>
  
  <p>In the frequencies questions: </p>
  
  <blockquote>
    <p>How to convert other data formats to LIBSVM format? </p>
    
    <p>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.</p>
  </blockquote>
  
  <pre><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.
  </code></pre>
  
  <p>but I don't wanna use matlab, I use python. </p>
  
  <p>I found this solution as well using <a href="http://nayefreza.wordpress.com/2013/09/18/converting-csv-file-to-libsvm-compatible-data-file-using-java/" rel="nofollow">JAVA</a> </p>
  
  <p>Can anyone recommend a way to tackle this problem ?</p>