python-3.x - 如何在大型数据集(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 列表中创建一个数据框。
然后使用具有所有停用词语料库的 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 之后的变换向量器
更多你可以在这里找到我的实现
我希望这会有所帮助...谢谢:)
推荐阅读
- python - 如何使用 NetGear 发送视频
- python - 如何在构建数据集时处理内存错误
- algorithm - 凝聚聚类和分裂聚类最终能得到相同的结果吗?
- arrays - 使用 powershell 将 exe 转换为 hexdump
- android - 包含布局中的 OnClick 侦听器
- python - 如何为 QPushButton 的背景颜色设置动画(动态更改按钮颜色)
- azure - 将 Azure AD 凭据集成到 Kubeflow 笔记本 pod
- android - 如果我需要在某些参数更改后重新加载数据,那么在 MVVM 中使用实时数据的正确方法是什么?
- javascript - h3 不会显示可能是因为导航栏
- python - Azure Function ImportError while importing test module / ModuleNotFoundError: No module named