как правильно использовать numpy hstack - PullRequest
1 голос
/ 29 марта 2020

У меня есть список документов. Я использую TfidfVectorizer, чтобы получить dt_matrix, то есть разреженную матрицу <class 'scipy.sparse.csr.csr_matrix'>

comments = get_comments()
tfidf_vector = TfidfVectorizer(tokenizer=tokenizer, lowercase=False)
dt_matrix = tfidf_vector.fit_transform(comments)

dt_matrix примерно так:

  (0, 642)  0.14738966496831196
  (0, 1577) 0.20377626427753473
  (0, 1166) 0.2947793299366239
  : :
 (1046, 166)    0.500700591796996

Теперь я хотел бы добавить к этой матрице длину документов как функцию. Итак, у меня есть массив length. В i-й позиции находится длина i-го документа.

length=get_comments_length()

length - это массив numpy, что-то вроде этого:

[141  56  79 ...  26  26  26]

I попробуйте сделать hstack:

features = np.hstack((dt_matrix, length))

Я получаю этот вывод:

ValueError: Found input variables with inconsistent numbers of samples: [1048, 1047]

Я напечатал формы:

print(np.shape(length))
print(np.shape(dt_matrix))

И вывод:

(1047,)
(1047, 2078)

Что я делаю не так?

Редактировать :

sparse.hstack((dt_matrix, length.reshape((length.shape[0], 1)))) это рабочий код. Использование sparse из scipy, спасибо @hpaulij и @kederrak за помощь

Ответы [ 2 ]

1 голос
/ 29 марта 2020
In [123]: from scipy import sparse  

Создайте матрицу scipy.sparse:

In [124]: M = sparse.random(5,4,.2)                                                            
In [125]: M                                                                                    
Out[125]: 
<5x4 sparse matrix of type '<class 'numpy.float64'>'
    with 4 stored elements in COOrdinate format>
In [126]: print(M)                                                                             
  (0, 3)    0.006222105671732758
  (1, 0)    0.7198559134274957
  (2, 0)    0.3603986399431639
  (4, 2)    0.9519927602284366
In [127]: M.A                                                                                  
Out[127]: 
array([[0.        , 0.        , 0.        , 0.00622211],
       [0.71985591, 0.        , 0.        , 0.        ],
       [0.36039864, 0.        , 0.        , 0.        ],
       [0.        , 0.        , 0.        , 0.        ],
       [0.        , 0.        , 0.95199276, 0.        ]])
In [128]: type(M)                                                                              
Out[128]: scipy.sparse.coo.coo_matrix

Попытка использовать hstack:

In [129]: np.hstack([M, np.arange(5)[:,None]])                                                 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-129-f06fc972039d> in <module>
----> 1 np.hstack([M, np.arange(5)[:,None]])

<__array_function__ internals> in hstack(*args, **kwargs)

/usr/local/lib/python3.6/dist-packages/numpy/core/shape_base.py in hstack(tup)
    341     # As a special case, dimension 0 of 1-dimensional arrays is "horizontal"
    342     if arrs and arrs[0].ndim == 1:
--> 343         return _nx.concatenate(arrs, 0)
    344     else:
    345         return _nx.concatenate(arrs, 1)

<__array_function__ internals> in concatenate(*args, **kwargs)

ValueError: all the input arrays must have same number of dimensions, 
but the array at index 0 has 1 dimension(s) and the array at index 1
has 2 dimension(s)

Правильное использование sparse.hstack:

In [130]: sparse.hstack([M, np.arange(5)[:,None]])                                             
Out[130]: 
<5x5 sparse matrix of type '<class 'numpy.float64'>'
    with 8 stored elements in COOrdinate format>
In [131]: _.A                                                                                  
Out[131]: 
array([[0.        , 0.        , 0.        , 0.00622211, 0.        ],
       [0.71985591, 0.        , 0.        , 0.        , 1.        ],
       [0.36039864, 0.        , 0.        , 0.        , 2.        ],
       [0.        , 0.        , 0.        , 0.        , 3.        ],
       [0.        , 0.        , 0.95199276, 0.        , 4.        ]])

Если второй массив имеет форму (5,) вместо (5,1), я получу вашу последнюю ошибку:

In [132]: sparse.hstack([M, np.arange(5)])                                                     
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-132-defd4158f59e> in <module>
----> 1 sparse.hstack([M, np.arange(5)])

/usr/local/lib/python3.6/dist-packages/scipy/sparse/construct.py in hstack(blocks, format, dtype)
    463 
    464     """
--> 465     return bmat([blocks], format=format, dtype=dtype)
    466 
    467 

/usr/local/lib/python3.6/dist-packages/scipy/sparse/construct.py in bmat(blocks, format, dtype)
    584                                                     exp=brow_lengths[i],
    585                                                     got=A.shape[0]))
--> 586                     raise ValueError(msg)
    587 
    588                 if bcol_lengths[j] == 0:

ValueError: blocks[0,:] has incompatible row dimensions. Got blocks[0,1].shape[0] == 1, expected 5.
1 голос
/ 29 марта 2020

вы можете использовать:

np.hstack((dt_matrix, length.reshape((1047, 1))))

или:

np.hstack((dt_matrix, length.reshape((length.shape[0], 1))))

из документов :

Параметры: tup: последовательность ndarrays

The arrays must have the same shape along all but the second axis
...