Input data type for sklearn SVD fit_transform function
<p>I have already processed document data in CSV file, which I read in pandas DataFrame:</p>

<pre><code>+----------+------+------------+
| document | term | count      |
+----------+------+------------+
| 1        | 126  | 1          |
| 1        | 80   | 1          |
| 1        | 1221 | 2          |
| 2        | 2332 | 1          |
</code></pre>

<p>So it consists of document_id, term, and term frequency.</p>

<p>I don't have original documents, but just this processed data, and I want to apply SVD with sklearn, but I can not figure how to prepare this DataFrame for SVD <a href="http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.TruncatedSVD.html#sklearn.decomposition.TruncatedSVD.fit_transform" rel="nofollow">fit_transform()</a>, which expects:</p>

<blockquote>
  <p>X : {array-like, sparse matrix}, shape (n_samples, n_features)</p>
</blockquote>

<p>You can convert this CSV to libsvm format:</p>

<pre><code>&lt;label&gt; &lt;index1&gt;:&lt;value1&gt; &lt;index2&gt;:&lt;value2&gt; ...
.
.
.
</code></pre>

<p>So, your example data will look like:</p>

<pre><code>0 80:1 126:1 1221:2
0 2332:1
</code></pre>

<p>Then read this file using <code>sklearn.datasets.load_svmlight_file</code></p>

<pre><code>from sklearn.datasets import load_svmlight_file
X, y = load_svmlight_file('your_libsvm_format_file.libsvm')
</code></pre>

<p>then,</p>

<pre><code>from sklearn.decomposition import SVD
svd = SVD()
X_transformed = svd.fit_transform(X)
</code></pre>

python scikit-learn nlp svd dimensionality-reduction