1

我正在尝试解决分类问题。当我喂文本CountVectorizer提示错误:Counterctorizer中的TypeError scikit-learn:预期的字符串或缓冲区

expected string or buffer.

什么不对的,因为它包含数和文字,甚至特殊字符的消息混合物,也是我的信息数据集。

样品怎样消息看起来是以下几点:

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) 
+0

@jez rael最终分类是对应于每个消息的类标签(文本数据),我通过映射到label_num列更改为数值。它没有丢失在我刚刚没有显示的数据集中。因为当我尝试使用countvectorizer拟合和转换消息时发生问题。 –

+0

而我的解决方案是否有效?由于UnicodeEncodeError错误, – jezrael

回答

1

您需要通过astype将列messagestring,因为在数据是一些数字值:

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

无法转换。我也试过df.message.apply(str)。 –

+0

嗯,我有一个想法 - 可能设置在Excel列'消息'字符串? – jezrael

+0

比你非常。 – jezrael

相关问题