将字典稀疏矩阵转换为 numpy 稀疏矩阵

Convert dictionary sparse matrix to numpy sparse matrix

我对 numpy 和稀疏矩阵比较陌生。根据以下说明,我正在尝试将我的数据转换为稀疏矩阵

if you take the current format and read the data in as a dictionary, then you can easily convert the feature-value mappings to vectors ( you can choose these vectors to be sparse matrices).

给定一个 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'),)

我该怎么做?我错过了什么吗?

来自文档: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()