turn categorical data to numeric and save to libsvm format python
<p>I have a DataFrame that looks something like this:</p>

<pre><code>    A         B        C        D
1   String1   String2  String3  String4
2   String2   String3  String4  String5
3   String3   String4  String5  String6
.........................................
</code></pre>

<p>My goal is to turn this DataFrame to a libSVM format.</p>

<p>What I have tried so far is the following:</p>

<pre><code>dummy= pd.get_dummies(dataframe)
dummy.to_csv('dataframe.csv', header=False, index=False)
</code></pre>

<p>is there a way to turn the dataframe or the csv file to this format. Or is there a smarter way to do the transformation?</p>

<p>I tried loading the script that's meant to do <a href="https://github.com/zygmuntz/phraug/blob/master/README.md" rel="nofollow">this</a> from this repository as follows:</p>

<pre><code>%load libsvm2csv.py
</code></pre>

<p>and the script is loaded correctly, but when I run:</p>

<pre><code>libsvm2csv.py dataframe.csv dataframe.data 0 True
</code></pre>

<p>or </p>

<pre><code>libsvm2csv.py dataframe.csv dataframe.txt 0 True
</code></pre>

<p>I get <code>"SyntaxError: invalid syntax"</code> pointing at dataframe.csv</p>

<p>After preprocessing your data, you can extract a matrix and use scikit-learns <a href="http://scikit-learn.org/stable/modules/generated/sklearn.datasets.dump_svmlight_file.html#sklearn.datasets.dump_svmlight_file" rel="nofollow">dump_svmlight_file</a> to create this format.</p>

<h3>Example code:</h3>

<pre><code>import pandas as pd
from sklearn.datasets import dump_svmlight_file

dummy = pd.get_dummies(dataframe)
mat = dummy.as_matrix()
dump_svmlight_file(mat, y, 'svm-output.libsvm')  # where is your y?
</code></pre>

<h3>Remarks / Alternative:</h3>

<p>You are mentioning <strong>libsvm2csv.py</strong> to do this conversion, but it's just the wrong direction. It is <strong>libsvm-format -> csv</strong>.</p>

<p>Check phraugs <strong>csv2libsvm.py</strong> if you want to convert from <strong>cvs -> libsvm</strong> (without scikit-learn).</p>

<p><em>I prefer the usage of scikit-learn (compared to phraug)</em></p>

python csv dataframe libsvm