Countvectorizer中的TypeError scikit-learn:预期的字符串或缓冲区

时间:2016-08-24 06:39:55

标签: python-2.7 pandas dataframe scikit-learn text-classification

我正在尝试解决分类问题。当我将文本提供给CountVectorizer时,它会给出错误:

  

预期的字符串或缓冲区。

我的数据集有什么问题,因为它包含数字和单词的消息混合,甚至特殊字符也在消息中。

示例消息如下所示:

0         I have not received my gifts which I ordered ok
1                 hth her wells idyll McGill kooky bbc.co
2                                   test test test 1 test
3                                                    test
4                         hello where is my reward points
5       hi, can you get koovs coupons or vouchers here...

以下是我用来进行分类的代码:

import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
df = pd.read_excel('training_data.xlsx')
X_train = df.message
print X_train.shape
map_class_label = {'checkin':0, 'greeting':1,'more reward options':2,'noclass':3, 'other':4,'points':5,
                           'referral points':6,'snapbill':7, 'thanks':8,'voucher not working':9,'voucher':10}
df['label_num'] = df['Final Category'].map(map_class_label)
y_train = df.label_num
vectorizer = CountVectorizer(lowercase=False,decode_error='ignore')
X_train_dtm = vectorizer.fit_transform(X_train)

2 个答案:

答案 0 :(得分:1)

您需要按astype将列message转换为string,因为数据中包含一些数值:

df = pd.read_excel('training_data.xlsx')
df['message'] = df['message'].values.astype('unicode')
...
...

答案 1 :(得分:0)

我只传递一个字符串就遇到了同样的错误,

cv.fit_transform('Making my way down,')

相反,您必须传递带有字符串的列表,如下所示:

cv.fit_transform(['Making my way down,', ])