我对numpy和稀疏矩阵比较陌生。我正试图将我的数据转换为稀疏矩阵,给出以下指令
如果您采用当前格式并将数据作为字典读取,那么您可以轻松地将特征值映射转换为向量(您可以选择这些向量为稀疏矩阵)。
给定一个pandas DataFrame,如下所示
sentiment tweet_id tweet
0 neg 1 [(3083, 0.4135918197208131), (3245, 0.79102943...
1 neg 2 [(679, 0.4192120119709425), (1513, 0.523940563...
2 neg 3 [(225, 0.5013098541806313), (1480, 0.441928325...
我把它转换成字典-
sparse_mat = {
(0, 3083): 0.4135918197208131,
(0, 3245): 0.7910294373931178,
(0, 4054): 0.4507928968357355,
(1, 679): 0.4192120119709425,
(1, 1513): 0.5239405639724402,
(1, 2663): 0.2689391233917331,
(1, 3419): 0.5679685442982928,
(1, 4442): 0.39348577488961367,
(2, 225): 0.5013098541806313,
(2, 1480): 0.44192832578442043,
(2, 2995): 0.3209783438156829,
(2, 3162): 0.4897198689787062,
(2, 3551): 0.2757628355961508,
(2, 3763): 0.3667287774412633
}
从我的理解这是一个有效的稀疏矩阵。我想把它存储为一个numpy对象,比如csr_matrix。我尝试运行以下代码-
csr_matrix(sparse_mat)
给出这个错误-
TypeError: no supported conversion for types: (dtype('O'),)
我该怎么做呢?我错过什么了吗?
from doc: https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html
from scipy.sparse import csr_matrix
d = {
(0, 3083): 0.4135918197208131,
(0, 3245): 0.7910294373931178,
(0, 4054): 0.4507928968357355,
(1, 679): 0.4192120119709425,
(1, 1513): 0.5239405639724402,
(1, 2663): 0.2689391233917331,
(1, 3419): 0.5679685442982928,
(1, 4442): 0.39348577488961367,
(2, 225): 0.5013098541806313,
(2, 1480): 0.44192832578442043,
(2, 2995): 0.3209783438156829,
(2, 3162): 0.4897198689787062,
(2, 3551): 0.2757628355961508,
(2, 3763): 0.3667287774412633
}
keys = d.keys()
row = [k[0] for k in keys]
col = [k[1] for k in keys]
data = list(d.values())
sparse_arr = csr_matrix((data, (row, col)))
arr = sparse_arr.toarray()