简体   繁体   English

从python中的矩阵的稀疏到密集表示

[英]From a sparse to dense representation of matrix in python

I have a list of vectors in python with 3 values with represent a sparse matrix. 我在python中有一个向量列表,其中3个值代表一个稀疏矩阵。 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. 实际上,第一值是2d数组的索引(30是第一索引s的最大值,而9是第二索引s的最大值),第三元素是矩阵的值。 How can I calculate the denserepresantation of the matrix 30x9 from that list? 如何从该列表中计算矩阵30x9的密集表示? 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. 我想要一个矩阵,其中行从1到31,列索引从1到9,矩阵的值是向量的第三个值。 In the end I want to perform NMF in the dataset found here . 最后,我想在此处找到的数据集中执行NMF。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM