python--电影评论文本情感分类

为了记录kaggle学习心得。

参考了大神文章。

1.http://www.cnblogs.com/lijingpeng/p/5787549.html

2.python机器学习及实战

from sklearn.datasets import fetch_20newsgroups
X, y = news.data , news.target

查看X的长度 , 以及X[0]的长度

print(len(X) ,len(X[0]),len(X[0][0]))
from bs4 import BeautifulSoup
import nltk ,re
news = fetch_20newsgroups(subset='all')
def news_to_sentences(news):
    news_text = BeautifulSoup(news).get_text() # 去掉HTML标签,拿到内容
    tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
    raw_sentences = tokenizer.tokenize(news_text)
    sentences = []
    for sent in raw_sentences:
        sentences.append(re.sub('[^a-zA-Z]', ' ', sent.lower().strip()).split()) # 小写化所有的词,并转成词list用正则表达式取出符合规范的部分
    return sentences
sentences = []
for x in X:
    sentences += news_to_sentences(x)
from gensim.models import word2vec


num_features = 300                       
min_word_count = 20                        
num_workers = 2    
context = 5                                                                               
downsampling = 1e-3   


from gensim.models import word2vec

model = word2vec.Word2Vec(sentences, workers=num_workers, \
            size=num_features, min_count = min_word_count, \
            window = context, sample = downsampling)

model.init_sims(replace=True)
model.most_similar('morning')
from sklearn.datasets import fetch_20newsgroups
X, y = news.data , news.target

查看X的长度 , 以及X[0]的长度

print(len(X) ,len(X[0]),len(X[0][0]))
from bs4 import BeautifulSoup
import nltk ,re
news = fetch_20newsgroups(subset='all')
def news_to_sentences(news):
    news_text = BeautifulSoup(news).get_text()
    
    tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
    raw_sentences = tokenizer.tokenize(news_text)
    
    sentences = []
    
    for sent in raw_sentences:
        sentences.append(re.sub('[^a-zA-Z]', ' ', sent.lower().strip()).split())
    return sentences
sentences = []
for x in X:
    sentences += news_to_sentences(x)
from gensim.models import word2vec


num_features = 300                       
min_word_count = 20                        
num_workers = 2    
context = 5                                                                               
downsampling = 1e-3   


from gensim.models import word2vec

model = word2vec.Word2Vec(sentences, workers=num_workers, \
            size=num_features, min_count = min_word_count, \
            window = context, sample = downsampling)

model.init_sims(replace=True)
model.most_similar('morning')




  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
电影评论情感分类是一个有趣且挑战性的问题,特别是使用Python 3.6进行情感分类更加有趣。首先,我们需要一个合适的数据集,其包含有打上情感标签的电影评论文本。可以使用一些公共的数据集,如IMDB电影评论数据集。 在Python 3.6,我们可以使用自然语言处理库NLTK来处理文本数据。首先,我们需要对文本进行预处理,包括去除标点符号、转换为小写字母等等。然后,我们可以使用NLTK库的词袋特征提取器或TF-IDF特征提取器来将文本转化为数值特征向量。 接下来,我们需要选择一个分类器来对情感进行分类。有很多机器学习算法可以用于情感分类,包括朴素贝叶斯、支持向量机、决策树等等。我们可以使用scikit-learn库的这些算法来训练我们的情感分类模型。 在Python 3.6,我们可以使用以下代码来加载数据集、预处理文本、提取特征并训练分类器: ``` import nltk from nltk.corpus import stopwords from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.svm import SVC from sklearn.metrics import accuracy_score nltk.download('stopwords') # 加载数据集 data = load_data() # 预处理文本 preprocessed_data = preprocess_data(data) # 提取特征 vectorizer = TfidfVectorizer(stop_words=stopwords.words('english')) features = vectorizer.fit_transform(preprocessed_data) # 定义标签 labels = data['sentiment'] # 划分训练集和测试集 train_features, test_features, train_labels, test_labels = split_data(features, labels) # 训练分类器 classifier = SVC() classifier.fit(train_features, train_labels) # 预测 predictions = classifier.predict(test_features) # 计算准确率 accuracy = accuracy_score(test_labels, predictions) ``` 以上是基于Python 3.6的情感分类代码的示例。在实际应用,可能还需要进行更多的数据处理和特征工程的步骤,以及使用交叉验证等技术来提高模型的准确性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值