我有一个.csv文件格式如下:
id_A,id_B
2135,1
2303,1
6292,1
大约180k的条目表示稀疏矩阵中矩阵的位置为1(另一个值为0)。 我想知道是否有一种方法可以用numpy或scipy在python中导入它。在
谢谢
我试过这样的方法,似乎奏效了
with open('icm.csv', 'rt') as f:
reader = csv.reader(f)
list = list(reader)
row = [] #i[:] the row indices of the matrix entries
coloumn = [] #[:] the column indices of the matrix entries
data=[] #data[:] the entries of the matrix, in any order
for i in range(1,len(list)):
row.append(int(list[i][0]))
coloumn.append(int(list[i][1]))
data.append(1)
matrix = coo_matrix((data, (coloumn, row)))
从您的示例中创建一个示例“文件”:
用
^{pr2}$genfromtxt
加载它;结果是一个有2个字段('columns')的结构化数组:从该数据创建
coo
格式矩阵:相关问题 更多 >
编程相关推荐