如何将数据帧转换与串列到csr_matrix
问题描述:
我工作的一个PMI的问题,到目前为止,我有这样一个数据帧:如何将数据帧转换与串列到csr_matrix
w = ['by', 'step', 'by', 'the', 'is', 'step', 'is', 'by', 'is']
c = ['step', 'what', 'is', 'what', 'the', 'the', 'step', 'the', 'what']
ppmi = [1, 3, 12, 3, 123, 1, 321, 1, 23]
df = pd.DataFrame({'w':w, 'c':c, 'ppmi': ppmi})
我想这个数据帧转换成稀疏矩阵。由于w
和c
是字符串列表,如果我做csr_matrix((ppmi, (w, c)))
,它会给我一个错误TypeError: cannot perform reduce with flexible type
。什么是转换此数据框的另一种方法?
答
也许你可以尝试用coo_matrix
:
import pandas as pd
import scipy.sparse as sps
w = ['by', 'step', 'by', 'the', 'is', 'step', 'is', 'by', 'is']
c = ['step', 'what', 'is', 'what', 'the', 'the', 'step', 'the', 'what']
ppmi = [1, 3, 12, 3, 123, 1, 321, 1, 23]
df = pd.DataFrame({'w':w, 'c':c, 'ppmi': ppmi})
df.set_index(['w', 'c'], inplace=True)
mat = sps.coo_matrix((df['ppmi'],(df.index.labels[0], df.index.labels[1])))
print(mat.todense())
输出:
[[ 12 1 1 0]
[ 0 321 123 23]
[ 0 0 1 3]
[ 0 0 0 3]]
我不认为''scipy'支持csr_matrix'混合类型,所以我不知道你是什么期待...你可能会考虑一个'pandas' [稀疏的数据结构](http://pandas.pydata.org/pandas-docs/version/0.15.2/sparse.html)。 –