1.文本格式

sentence,label
游戏太坑,暴率太低,太克金,平民不能玩,negative
让人失望,negative
能解决一下服务器问题?网络正常老掉线,换手机也一样。。。,negative
期待,positive
一星也不想给,这特么简直龟速,炫舞老年版?,negative
衣服不好看游戏内容无特色,界面乱糟糟的,negative
喜欢喜欢,positive
从有了这个手游就一直玩,很喜欢呀,希望更多漂漂衣服,positive
因违反评价条例规定被折叠,negative

2.数据预处理过程

import jieba
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn import metricsdef get_stop_words():filename = "your stop words file path"stop_word_list = []with open(filename, encoding='utf-8') as f:for line in f.readlines():stop_word_list.append(line.strip())return stop_word_listdef processing_sentence(x, stop_words):cut_word = jieba.cut(str(x).strip())words = [word for word in cut_word if word not in stop_words and word != ' ']return ' '.join(words)def data_processing():train_file = “your train file path"df = pd.read_csv(train_file)x_train, x_test, y_train, y_test = train_test_split(df['sentence'], df['label'], test_size=0.1)stop_words = get_stop_words()x_train = x_train.apply(lambda x: processing_sentence(x, stop_words))x_test = x_test.apply(lambda x: processing_sentence(x, stop_words))tf = TfidfVectorizer()x_train = tf.fit_transform(x_train)x_test = tf.transform(x_test)x_train_weight = x_train.toarray()x_test_weight = x_test.toarray()return x_train_weight, x_test_weight, y_train, y_test

整体还是将文本分词,然后将其转化为tf-idf特征。

3.构建LR模型

def model_train():x_train_weight, x_test_weight, y_train, y_test = data_processing()lr = LogisticRegression(C=1.0, penalty='l2', tol=0.01)lr.fit(x_train_weight, y_train)train_score = lr.score(x_train_weight, y_train)print("训练集准确率: ", train_score)y_predict = lr.predict(x_test_weight)confusion_mat = metrics.confusion_matrix(y_test, y_predict)print('测试集准确率:', metrics.accuracy_score(y_test, y_predict))print("confusion_matrix is: ", confusion_mat)print('分类报告:', metrics.classification_report(y_test, y_predict))

最后代码输出的训练过程与结果为

训练集准确率:  0.8926945588554086
测试集准确率: 0.746588693957115
confusion_matrix is:  [[177  64][ 66 206]]
分类报告:               precision    recall  f1-score   supportnegative       0.73      0.73      0.73       241positive       0.76      0.76      0.76       272accuracy                           0.75       513macro avg       0.75      0.75      0.75       513
weighted avg       0.75      0.75      0.75       513

使用Logistic Regression进行文本分类相关推荐

  1. 机器学习笔记04:逻辑回归(Logistic regression)、分类(Classification)

    之前我们已经大概学习了用线性回归(Linear Regression)来解决一些预测问题,详见: 1.<机器学习笔记01:线性回归(Linear Regression)和梯度下降(Gradien ...

  2. NLP-Beginner:自然语言处理入门练习----task 1基于机器学习的文本分类

    任务一:基于机器学习的文本分类 任务传送门 项目是在github上的,数据集需要在kaggle上下载,稍微有些麻烦. wang盘:http://链接:https://pan.baidu.com/s/1 ...

  3. NLP-Task1:基于机器学习的文本分类

    NLP-Task1:基于机器学习的文本分类 实现基于logistic/softmax regression的文本分类 数据集:Classify the sentiment of sentences f ...

  4. 自然语言处理入门实战1:基于机器学习的文本分类

    基于机器学习的文本分类 配置文件 数据集 数据预处理 model 模型 主函数 预测 结果 参考代码 本文参考复旦大学自然语言处理入门练习,主要是实现用tensorflow实现基于logistic/s ...

  5. 线性模型(3):Logistic Regression

    此笔记源于台湾大学林轩田老师<机器学习基石><机器学习技法> (一)Logistic Regression 原理 对于分类问题,假设我们想得到的结果不是(x属于某一类)这种形式 ...

  6. 机器学习算法与Python实践之逻辑回归(Logistic Regression)

    转载自:http://blog.csdn.net/zouxy09/article/details/20319673 机器学习算法与Python实践这个系列主要是参考<机器学习实战>这本书. ...

  7. Lecture6 逻辑斯蒂回归(Logistic Regression)

    目录 1 常用数据集 1.1 MNIST数据集 1.2 CIFAR-10数据集 2 课堂内容 2.1 回归任务和分类任务的区别 2.2 为什么使用逻辑斯蒂回归 2.3 什么是逻辑斯蒂回归 2.4 Si ...

  8. 二项逻辑回归模型(logistic regression model)

    Binary logistic regression model 是分类模型,由概率分布P(Y∣X)P(Y|X)P(Y∣X)计算,是参数化的Logistic分布 先概述一下这个模型的条件概率分布 P( ...

  9. ml-3-1-逻辑回归( Logistic Regression)

    逻辑回归( Logistic Regression) Classification 在分类问题中,你要预测的变量 y 是离散的值,我们将学习一种叫做逻辑回归 (Logistic Regression) ...

最新文章

  1. 基于C++实现线程池加速
  2. 算法(掘金上经典的文档)
  3. Flutter 36: 图解自定义 View 之 Canvas (三)
  4. gin+vue的前后端分离开源项目
  5. 提高tomcat的并发能力
  6. Python数据库编程pymysql
  7. easyphp 登陆mysql_EasyPHP/phpmyadmin 尝试连接到 mysql 服务器 但服务器拒绝连接
  8. sql server数据表转换成xml
  9. SQL Server警报解决性能问题:如何创建和使用SQL Server警报
  10. mysql模式匹配详解_老生常谈MYSQL模式匹配 REGEXP和like的用法
  11. 5.Linux 高性能服务器编程 --- Linux 网络编程基础 API
  12. 【经典传颂】人月神话The Mythical Man-Month
  13. 《人类简史》笔记——认知革命和农业革命背后的思考
  14. android 半圆型菜单,Android-自定义view之圆形与“半圆形”菜单
  15. iPhone6和iPhone6 Plus和iPhone5s屏幕尺寸,分辨率,密度 对比
  16. python程序设计基础之注释
  17. 计算机网络之IP协议与以太网
  18. centos7 分辨率修改_centos系统修改屏幕分辨率问题
  19. 《击掌为盟》读后感1742字
  20. 如何在云栖社区显示一个QAP DEMO

热门文章

  1. stdcall 以及 其他的dll调用方式
  2. MVC 模式简单介绍
  3. 独行温哥华,漫步西雅图(上)
  4. 农信社最常见的5个面试问题
  5. 360、美团、快手等公司携手智源研究院共建AI开放实验室
  6. 安装web服务器访问站点的三种方式,Web服务器配置方法
  7. ###关于k-means的新理解
  8. Vue 点击table中的某列,带参数跳转到另外一个页面
  9. 哈佛大学构建动态网站--第七讲ajax
  10. 【刘浩运7.19作业】