python - 使用我的数据而不是 20 个新闻组进行情绪分析

标签 python machine-learning scikit-learn

对于下面的代码,请告诉我如何使用我自己的数据集而不是 sklearn 的 20newsgroup 数据集。

我有 100 个聊天室 (.txt) 文件已被分类为正面和负面。

from sklearn.datasets import fetch_20newsgroups
import sklearn.feature_extraction.text as ext

categories = ['sci.space']

twenty_train = fetch_20newsgroups(subset='train',
        categories=categories,
        remove=('headers', 'footers', 'quotes'),
        shuffle=True,
        random_state=42)

count_chars = ext.CountVectorizer(analyzer='char_wb',
        ngram_range=(3,3),
        max_features=10).fit(twenty_train['data'])
count_words = ext.CountVectorizer(analyzer='word',
        ngram_range=(3,3),
        max_features=10,
        stop_words='english').fit(twenty_train['data'])
X = count_chars.transform(twenty_train.data)

print (count_words.get_feature_names())
print ( X[1].todense())

感谢您的宝贵帮助!

最佳答案

将对 fetch-20newsgroups 的调用替换为代码

  • 加载您的数据
  • 预处理

关于python - 使用我的数据而不是 20 个新闻组进行情绪分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33028237/

相关文章:

Python mock 修补函数装饰器的参数值

python - 打印一系列列对齐的列表

python - 如果 Python Scrapy 中的蜘蛛已经看到了 Item(),如何省略对 Item() 的处理

python - 如何使用 Pandas 删除项目或字符串少于一定数量的行?

python - 在数据集上拟合决策树分类器时出现 ValueError

python - 使用 MLP 分类器,类的精度为零

machine-learning - 如何使用 scikit-learn RandomForestClassifier 实现稳定性?

python - Sklearn 列转换器 ValueError : could not convert string to float:

python - 如何存储 TfidfVectorizer 以备将来在 scikit-learn 中使用?

python - 不应用所选参数的随机搜索 CV