setting an array element with a sequence error in scikit learn GradientBoostingClassifier

南楼画角 提交于 2019-12-09 04:38:26

The problem is with 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 is:

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)

We can see that we are mixing sparse matrix with dense matrix. You can transform it to dense with: todense():

dense_count = CountVectorizer().fit_transform(df['f2']).todense()

where dense_count is something like:

matrix([[1, 1, 0],
        [0, 1, 1]], dtype=int64)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!