import pandas as pd
from joblib.numpy_pickle_utils import xrange
import wash
from sklearn.feature_extraction.text import CountVectorizer
import numpy as np
from sklearn.ensemble import RandomForestClassifier
# 使用pandas包的read_csv函数读取训练数据集,选12条数据进行测试
train = pd.read_csv("labeledTrainData.tsv", header=0, delimiter="\t", quoting=3, nrows=12)
# 查看原始训练数据
print(train)
# 得到训练数据的size,其实是3
num_reviews = train["review"].size
print(num_reviews)
# 初始化一个空的列表来装清洗后的数据
clean_train_reviews = []
# 进行训练集数据的清洗并查看结果
for i in xrange(0, num_reviews):
clean_train_reviews.append(wash.review_to_words(train["review"][i]))
print(clean_train_reviews)
# 初始化一个对象用于统计评论中的单词频度,为了限制后面的每个特征向量的大小,最多统计前100个出现最多的词的频度
vector = CountVectorizer(analyzer="word",
tokenizer=None,
preprocessor=None,
stop_words=None,
max_features=100)
# 将干净数据特征向量化
train_data_features = vector.fit_transform(clean_train_reviews)
# 将特征向量转为数组,查看数组规模
train_data_features = train_data_features.toarray()
print(train_data_features.shape)
# 查看词汇表
vocab = vector.get_feature_names()
print(vocab)
# 查看词汇表中每个单词的计数
dist = np.sum(train_data_features, axis=0)
for tag, count in zip(vocab, dist):
print(count, tag)
# 开始训练
print("start train......")
# 使用随机森林算法,设置树为10
forest = RandomForestClassifier(n_estimators=10)
# 使用数字训练特征和每个特征向量的原始情绪标签
forest = forest.fit(train_data_features, train["sentiment"])
# 读取4条测试数据
test = pd.read_csv("testData.tsv", header=0, delimiter="\t",
quoting=3, nrows=4)
print(test.shape)
# 查看测试数据的评论条数,实际为4
test_num_reviews = len(test["review"])
print(test_num_reviews)
# 创建一个空的列表装清洗后的测试数据
clean_test_reviews = []
# 清理测试数据,并查看
for i in xrange(0, test_num_reviews):
clean_test_reviews.append(wash.review_to_words(test["review"][i]))
print(clean_test_reviews)
# 测试数据特征化
test_data_features = vector.transform(clean_test_reviews)
test_data_features = test_data_features.toarray()
print(test_data_features)
# 使用训练结果进行预测
result = forest.predict(test_data_features)
print(result)
# 格式化输出结果
output = pd.DataFrame(data={"id": test["id"], "sentiment": result})
# 创建csv文件
output.to_csv("movie_word_model_beta.csv", index=False, quoting=3)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【资源介绍】 Python基于IMDB电影评论数据集进行评论分析源码+项目说明.zip 该项目是个人毕设项目,答辩评审分达到95分,代码都经过调试测试,确保可以运行!欢迎下载使用,可用于小白学习、进阶。 该资源主要针对计算机、通信、人工智能、自动化等相关专业的学生、老师或从业者下载使用,亦可作为期末课程设计、课程大作业、毕业设计等。 项目整体具有较高的学习借鉴价值!基础能力强的可以在此基础上修改调整,以实现不同的功能。 - 使用IMDB电影评论数据集,进行评论分析 - 数据集来自https://www.kaggle.com/competitions/word2vec-nlp-tutorial/data - 算法参考同上 - 项目代码结构说明: 1. wash.py:实现原始评论的数据清理,去除了html标签、无意义词 2. main.py:分别使用wash.py清洗原始数据和测试数据,并分别为其构造“单词包”特征向量。使用随机森林算法训练原始特征向量集合得到训练模型。使用训练模型对测试特征向量集合进行预测。 - beta代码结构说明: 1. main_beta.py:由于个人pc无法承载50,000数据工作,笔者学习时,采用了12条训练数据,4条测试数据,遵循3:1。 2. movie_word_model_beta.csv:按照task要求生成标准csv文件
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 5 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
- zwz51268012024-07-11资源很实用,对我启发很大,有很好的参考价值,内容详细。
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
雨果同学的编程之路
- 粉丝: 3683
- 资源: 2214
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)