Jose Ramon - 6 months ago 34

Python Question

I have a list of vectors in python with 3 values with represent a sparse matrix. In fact the first values are the indexes of a 2d array (30 is the max value for the first index s and 9 the max value for the second index) and the third elements is the values of the matrix. How can I calculate the denserepresantation of the matrix 30x9 from that list? My data has the following structure:

`> [1, 1, 4],`

[1, 4, 2],

[1, 6, 1],

[1, 8, 5],

....

[31, 9, 6],

...

I want a matrix with rows to be from 1-31 and columns index to be from 1-9 and the values of the matrix to be the third value of the vectors. In the end I want to perform NMF in the dataset found here.

Answer

You can try this:

```
vectors = [[1,1,2],[2,4,6],[31,2,8]]
tab=[[0 for col in range(9)] for row in range(31)]
for x in vectors:
tab[x[0]-1][x[1]-1] = x[2]
print tab
```