Проблема с CountVectorizer
:
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
d = {'f1': [1, 2], 'f2': ['foo goo', 'goo zoo'], 'target':[0, 1]}
df = pd.DataFrame(data=d)
df['f2'] = CountVectorizer().fit_transform(df['f2'])
df.values
:
array([[1,
<2x3 sparse matrix of type '<class 'numpy.int64'>'
with 4 stored elements in Compressed Sparse Row format>,
0],
[2,
<2x3 sparse matrix of type '<class 'numpy.int64'>'
with 4 stored elements in Compressed Sparse Row format>,
1]], dtype=object)
Мы видим, что мы смешиваем разреженную матрицу с плотной матрицей. Вы можете преобразовать его в плотный с помощью: todense()
:
dense_count = CountVectorizer().fit_transform(df['f2']).todense()
где dense_count
- это что-то вроде:
matrix([[1, 1, 0],
[0, 1, 1]], dtype=int64)