Translate lists to a 2D numpy matrix
<p>I have a list of lists in python. The lists is the following:</p>

<pre><code>[[196, 242, 3], 
 [186, 302, 3], 
 [22, 377, 1],
 [196, 377, 3],
 ....
 ]
</code></pre>

<p>The first column correspond to users (1:943) and the second to items(1:1682) and their votes to items. I want to try the matrix factorization <a href="https://github.com/mnick/scikit-tensor" rel="nofollow">library</a>. Should I created a users x items matrix? If yes how can I create a matrix like that in python, with the one axis to be in size of users the other one the size of items and the values the votes of the users?</p>

<p><strong>EDIT:</strong> I check also the implementation of nmf.py which requir as input a 2D matrix and not a list or a sparse represantation.</p>

<p>Here is how you can create a sparse matrix from the list of set items:</p>

<pre><code>data = [
    [196, 242, 3], 
    [186, 302, 3], 
    [22, 377, 1],
    [196, 377, 3],
    ....
]

user_count = max(i[0] for i in data) + 1
item_count = max(i[1] for i in data) + 1

data_mx = scipy.sparse.dok_matrix((user_count, item_count))
for (user, item, value) in data:
    data_mx[user, item] = value
</code></pre>

python numpy matrix