【发布时间】:2016-12-31 03:54:17
【问题描述】:
我正在尝试解决分类问题。当我将文本提供给 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)
【问题讨论】:
-
@jezrael 最终类别是对应于每条消息的类标签(文本数据),我通过映射到 label_num 列将其更改为数值。它在我只是没有显示的数据集中没有丢失。当我尝试使用 countvectorizer 拟合和转换消息时出现问题。
-
我的解决方案是否有效?
标签: python-2.7 pandas dataframe scikit-learn text-classification