首页 > 解决方案 > 如何在大型数据集(Train、Dev、Test)上使用 CountVectorizer 和 TfidfTransformer?

问题描述

仅使用 CountVectorizer​ 和​TfIdTransformer 实现以下每个功能模板。我有训练、开发、测试数据。

这是我的样本火车数据:

Train_dataset = ['This is country of srilanka','This  was very much', ' hi bro how are you']

一个。仅表示具有二进制值的单词出现(​基线​)。
湾。删除停用词(这些是英语中最常见的词)。
C。表示词的词频。

标签: python-3.xmachine-learningsentiment-analysis

解决方案


你需要做的,只是从你的 python 列表中创建一个数据框。
然后使用具有所有停用词语料库的 nltk 库创建一个自定义函数如果那里有一些不相关的标签,请删除并预处理您的数据。 示例代码:

stop = set(stopwords.words('english')) #set of stopwords
Now you can check iterating over your data and remove the stopwords

然后假设你已经清理了你的文本,就像下面的 count & tfidf

count_vect = CountVectorizer()
X_train = count_vect.fit_transform(X_train_data['CleanedText'])
X_test = count_vect.transform(X_test_data['CleanedText'])
print(X_train.shape) 
print(X_test.shape)

注意:X_train、X_test 是经过 countvectorizer 的变换向量化器

与 tfidf 相同,

tf_idf_vect = TfidfVectorizer()
X_train = tf_idf_vect.fit_transform(X_train_data['CleanedText'])
X_test = tf_idf_vect.transform(X_test_data['CleanedText'])
print(X_train.shape)
print(X_test.shape)

注意:X_train、X_test 是 tfidf vectorizer 之后的变换向量器

更多你可以在这里找到我的实现


我希望这会有所帮助...谢谢:)


推荐阅读