Lab*_*bba 2 machine-learning word-count scikit-learn text-classification
我目前有一个 SVM 模型,可以将文本分为两个不同的类别。我目前正在使用 CountVectorizer 和 TfidfTransformer 来创建我的“词向量”。
问题是,当我首先转换所有文本然后将其拆分时,我认为我可能以错误的顺序进行操作。
我的问题是,如果我先执行train_test_split,然后仅对训练数据执行fit_transform,然后对测试数据进行转换,会有什么区别吗?
正确的做法是什么?
非常感谢,祝您编码愉快!
count_vect = CountVectorizer(stop_words='english')
X_counts = count_vect.fit_transform(textList)
tfidf_transformer = TfidfTransformer()
X_tfidf = tfidf_transformer.fit_transform(X_counts)
X_train, X_test, y_train, y_test = train_test_split(X_tfidf, correctLabels, test_size=.33, random_state=17)
Run Code Online (Sandbox Code Playgroud)