在Python中创建稀疏矩阵

时间:2016-06-21 20:21:35

标签: python numpy scikit-learn sparse-matrix sklearn-pandas

使用数据并希望创建一个稀疏矩阵,以便以后用于聚类目的。

fileHandle = open('data', 'r')

for line in fileHandle:
    json_list = []
    fields = line.split('\t')
    json_list.append(fields[0])
    json_list.append(fields[1])
    json_list.append(fields[3])

现在数据看起来像这样:

term, ids, quantity
['buick', '123,234', '500']
['chevy', '345,456', '300']
['suv','123', '100']

我需要的输出是这样的:

term, quantity, '123', '234', '345', '456', '567'
buick, 500, 1, 1, 0, 0, 0
chevy, 300, 0, 0, 1, 1, 0
suv,   100, 1, 0, 0, 0, 0

我尝试使用numpy稀疏矩阵库但没有成功。

2 个答案:

答案 0 :(得分:0)

我有一个懒惰的方法

data = [['term', 'ids', 'quantity'],
... ['buick', ['123', '234'], 500],
... ['chevy', ['345', '456'], 300],
... ['suv', ['123', '567'], 100]]
res = []
for i,line in enumerate(data):
...     if i == 0:
...         header = line
...     else:
...         temp  = {}
...         for j,ele in enumerate(line):
...             if j in [0,2]:
...                 temp.update( {header[j] : ele} )
...             else:
...                 for num in line[1]:
...                     temp.update( { num:1 } )
...         res.append(temp)

with open(filepath,'wb') as f:
...      w = csv.DictWriter(f,set( [ k for ele in res for k in ele.keys()] ))
...      w.writeheader()
...      w.writerows(res)

输出

term    456 567 345 123 234 quantity
buick               1   1   500
chevy   1       1           300
suv     1       1           100

答案 1 :(得分:0)

scikit_learn可能有很容易做到的工具,但我将展示一个基本的Python / numpy解决方案。

原始数据 - 列表列表

In [1150]: data=[['buick', '123,234', '500'],
                 ['chevy', '345,456', '300'],
                 ['suv','123', '100']]

我可以通过列表推导来提取真实的列。在一个非常大的情况下,这可能不是最快的,但是现在它是逐个解决问题的简单方法。

In [1151]: terms=[row[0] for row in data]

In [1152]: terms
Out[1152]: ['buick', 'chevy', 'suv']

In [1153]: quantities=[int(row[2]) for row in data]

In [1154]: quantities
Out[1154]: [500, 300, 100]

创建可能的ID列表。我可以从data中提取这些内容,但您显然使用的是更大的列表。它们可以是字符串而不是整数。

In [1155]: idset=[123,234,345,456,567]

In [1156]: ids=[[int(i) for i in row[1].split(',')] for row in data]

In [1157]: ids
Out[1157]: [[123, 234], [345, 456], [123]]

np.in1d是一个方便的工具,用于查找这些子列表在主列表中的位置。结果idM是特征矩阵,有很多0和几个。

In [1158]: idM=np.array([np.in1d(idset,i) for i in ids],int)

In [1159]: idM
Out[1159]: 
array([[1, 1, 0, 0, 0],
       [0, 0, 1, 1, 0],
       [1, 0, 0, 0, 0]])

我们可以用各种方式组装这些作品。

例如,可以使用以下命令创建结构化数组:

In [1161]: M=np.zeros(len(data),dtype='U10,int,(5)int')

In [1162]: M['f0']=terms

In [1163]: M['f1']=quantities

In [1164]: M['f2']=idM

In [1165]: M
Out[1165]: 
array([('buick', 500, [1, 1, 0, 0, 0]), ('chevy', 300, [0, 0, 1, 1, 0]),
       ('suv', 100, [1, 0, 0, 0, 0])], 
      dtype=[('f0', '<U10'), ('f1', '<i4'), ('f2', '<i4', (5,))])

idM可以转换为稀疏矩阵:

In [1167]: from scipy import sparse

In [1168]: c=sparse.coo_matrix(idM)

In [1169]: c
Out[1169]: 
<3x5 sparse matrix of type '<class 'numpy.int32'>'
    with 5 stored elements in COOrdinate format>

In [1170]: c.A
Out[1170]: 
array([[1, 1, 0, 0, 0],
       [0, 0, 1, 1, 0],
       [1, 0, 0, 0, 0]])

在这次探索中,首先创建更密集的数组更容易,并从中做出稀疏。

但是sparse提供了一个bmat函数,可以让我从单行列表中创建多行矩阵。 (有关直接构造coo输入的版本,请参阅我的编辑历史记录)

In [1220]: ll=[[sparse.coo_matrix(np.in1d(idset,i),dtype=int)] for i in ids]

In [1221]: sparse.bmat(ll)
Out[1221]: 
<3x5 sparse matrix of type '<class 'numpy.int32'>'
    with 5 stored elements in COOrdinate format>

In [1222]: sparse.bmat(ll).A
Out[1222]: 
array([[1, 1, 0, 0, 0],
       [0, 0, 1, 1, 0],
       [1, 0, 0, 0, 0]], dtype=int32)