简体   繁体   English

Pandas DataFrame与sci-kit fit_transform()函数不兼容

[英]Pandas DataFrame incompatible with sci-kit fit_transform() function

So I have created a classifier that distinguishes fraudulent messages from genuine messages. 因此,我创建了一个分类器,以区分欺诈性消息和真实消息。 Snippet of the code is as follows: 代码片段如下:

# Import training set as DataFrame from CSV
dataset = pd.read_csv('data.csv', sep=',')
class_names = { 1: 'no-flag', 2: 'flag' }

# Separate training data to message, class pairs
X_train, y_train = dataset.iloc[:,0], dataset.iloc[:, 1]

messages = pd.read_csv('messages.csv', header=None)
X_predict = messages.iloc[:,0]

print "TRAIN:\n"
print type(X_train)
print "PREDICT:\n"
print type(X_predict)

# Vectorise text data
vect = TfidfVectorizer(ngram_range=(1, 2), lowercase=True, preprocessor=sanitise_message)
X_train_tfidf = vect.fit_transform(X_train)
X_predict_tfidf = vect.transform(X_predict)

I used to run this with ten-fold cross validation on the training set, using: 我以前在训练集上使用十倍交叉验证来运行此操作,方法是:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=1)

And that used to work fine. 过去工作正常。 Now I want to use the entire training set as training data, and predict unclassified data. 现在,我想将整个训练集用作训练数据,并预测未分类的数据。 However, the call to X_predict_tfidf = vect.transform(X_predict) throws an error, as follows: 但是,对X_predict_tfidf = vect.transform(X_predict)的调用将引发错误,如下所示:

Traceback (most recent call last):
File "post-test.py", line 3, in <module>
classify()
File "/Users/user/Documents/MyTutor/mi_datawarehouse/classifier.py", line 90, in classify
X_predict_tfidf = vect.transform(X_predict)
File "/Users/user/miniconda2/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 1409, in transform
X = super(TfidfVectorizer, self).transform(raw_documents)
File "/Users/user/miniconda2/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 923, in transform
_, X = self._count_vocab(raw_documents, fixed_vocab=True)
File "/Users/user/miniconda2/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 792, in _count_vocab
for feature in analyze(doc):
File "/Users/user/miniconda2/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 266, in <lambda>
tokenize(preprocess(self.decode(doc))), stop_words)
File "/Users/user/miniconda2/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 119, in decode
raise ValueError("np.nan is an invalid document, expected byte or "
ValueError: np.nan is an invalid document, expected byte or unicode string.

What is interesting is that the types of both X_train and X_predict are identical: 有趣的是,X_train和X_predict的类型相同:

TRAIN:
<class 'pandas.core.series.Series'>
PREDICT:
<class 'pandas.core.series.Series'>

What am I doing wrong? 我究竟做错了什么? I've been going crazy over this as I've looked everywhere, including the scikit-learn docs. 我到处都在寻找,包括scikit-learn文档,让我为此感到疯狂。

NOTE: this is NOT a duplicate of a similar question , I have tried everything in that question and nothing worked. 注意:这不是类似问题的重复,我已经尝试了该问题中的所有内容,但没有任何效果。 The data structures and problem are slightly different. 数据结构和问题略有不同。

quick fix might be to remove NaN. 快速解决方案可能是删除NaN。 Try messages.dropna() 尝试messages.dropna()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM