最近,我开始阅读更多关于 NLP 的内容并关注 Python 教程,以了解有关该主题的更多信息。我遇到的问题,现在我正在尝试制作我自己的分类算法(文本发送正面/负面消息)关于训练和测试数据集。在我发现的所有示例中,只使用了一个数据集,该数据集后来被拆分为训练/测试。我有两个数据集,我的方法是将两个数据集中的所有文本(预处理后)放在同一个语料库中,然后将语料库分成测试集和训练集。
datasetTrain = pd.read_csv('train.tsv', delimiter = '\t', quoting = 3)
datasetTrain['PN'].value_counts()
datasetTest = pd.read_csv('test.tsv', delimiter = '\t', quoting = 3)
datasetTest['PN'].value_counts()
corpus = []
y = []
# some preprocessing
y.append(posNeg)
corpus.append(text)
from sklearn.feature_extraction.text import TfidfVectorizer
transf = TfidfVectorizer(stop_words = stopwords, ngram_range = (1,1), min_df = 5, max_df = 0.65)
X = transf.fit_transform(corpus).toarray()
# Splitting the dataset into the Training set and Test set
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.11, random_state = 0)
我这样做的原因是因为我正在使用词袋模型,如果我从头开始创建X_train和X_test(分别为y_train,y_test)而不使用拆分功能,我会在运行分类算法:
X_train = transf.fit_transform(corpustrain).toarray()
X_test = transf.fit_transform(corpustest).toarray()
...
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
ValueError: Number of features of the model must match the input. Model n_features is 2770 and input n_features is 585
我对此有点陌生,我想知道是否有人可以指导我正确的方向?