使用我的数据而不是 20 个新闻组进行情绪分析
use my data instead of 20 newsgroup for sentiment analysis
对于下面的代码,请告诉我如何使用我自己的数据集而不是 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
的调用
- 加载您的数据
- 对其进行预处理
对于下面的代码,请告诉我如何使用我自己的数据集而不是 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
的调用
- 加载您的数据
- 对其进行预处理