nlp构建_使用NLP构建自杀性推文分类器

nlp构建

Over the years, suicide has been one of the major causes of death worldwide, According to Wikipedia, Suicide resulted in 828,000 global deaths in 2015, an increase from 712,000 deaths in 1990. This makes suicide the 10th leading cause of death worldwide. There is also increasing evidence that the Internet and social media can influence suicide-related behaviour. Using Natural Language Processing, a field in Machine Learning, I built a very simple suicidal ideation classifier which predict whether a text is likely to be suicidal or not.

多年来,自杀一直是全世界主要的死亡原因之一。据维基百科称 ,自杀导致2015年全球死亡828,000人,比1990年的712,000人有所增加。这使自杀成为全球第十大死亡原因。 越来越多的证据表明,互联网和社交媒体可以影响 自杀相关行为 。 使用机器学习中的自然语言处理这一领域,我建立了一个非常简单的自杀意念分类器,该分类器可预测文本是否可能具有自杀意味。

数据 (Data)

I used a Twitter crawler which I found on Github, made some few changes to the code by removing hashtags, links, URL and symbols whenever it crawls data from Twitter, the data were crawled based on query parameters which contain words like:

我使用了一个在Github上找到的Twitter搜寻器,通过在每次从Twitter抓取数据时删除标签,链接,URL和符号来对代码进行了一些更改,这些数据是根据包含以下单词的查询参数进行抓取的:

Depressed, hopeless, Promise to take care of, I dont belong here, Nobody deserve me, I want to die etc.

沮丧,绝望,无极照顾,我不属于这里,没人值得我,我想死等等。

Although some of the text we’re in no way related to suicide at all, I had to manually label the data which were about 8200 rows of tweets. I also sourced for more Twitter Data and I was able to concatenate with the one I previously had which was enough for me to train.

尽管有些文本根本与自杀无关,但我不得不手动标记大约8200行tweet数据。 我还获得了更多的Twitter数据,并且能够与以前拥有的足以进行训练的数据相结合。

建立模型 (Building the Model)

数据预处理 (Data Preprocessing)

I imported the following libraries:

我导入了以下库:

import pickle
import re
import numpy as np
import pandas as pd
from tqdm import tqdm
import nltk
nltk.download('stopwords')

I then wrote a function to clean the text data to remove any form of HTML markup, keep emoticon characters, remove non-word character and lastly convert to lowercase.

然后,我编写了一个函数来清除文本数据,以删除任何形式HTML标记,保留表情符号字符,删除非单词字符并最后转换为小写字母。

def preprocess_tweet(text):
text = re.sub('<[^>]*>', '', text)
emoticons = re.findall('(?::|;|=)(?:-)?(?:\)|\(|D|P)', text)
lowercase_text = re.sub('[\W]+', ' ', text.lower())
text = lowercase_text+' '.join(emoticons).replace('-', '')
return text

After that, I applied the preprocess_tweet function to the tweet dataset to clean the data.

之后,我将preprocess_tweet函数应用于tweet数据集以清理数据。

tqdm.pandas()df = pd.read_csv('data.csv')
df['tweet'] = df['tweet'].progress_apply(preprocess_tweet)

Then I converted the text to tokens by using the .split() method and used word stemming to convert the text to their root form.

然后,我使用.split()方法将文本转换为标记,并使用词干将文本转换为其根形式。

from nltk.stem.porter import PorterStemmer
porter = PorterStemmer()def tokenizer_porter(text):
return [porter.stem(word) for word in text.split()]

Then I imported the stopwords library to remove stop words in the text.

然后,我导入了停用词库,以删除文本中的停用词。

from nltk.corpus import stopwords
stop = stopwords.words('english')

Testing the function on a single text.

在单个文本上测试功能。

[w for w in tokenizer_porter('a runner likes running and runs a lot') if w not in stop]

Output:

输出:

['runner', 'like', 'run', 'run', 'lot']

矢量化器 (Vectorizer)

For this project, I used the Hashing Vectorizer because it data-independent, which means that it is very low memory scalable to large datasets and it doesn’t store vocabulary dictionary in memory. I then created a tokenizer function for the Hashing Vectorizer

在此项目中,我使用了Hashing Vectorizer,因为它与数据无关,这意味着它的内存非常低,可扩展到大型数据集,并且不将词汇表存储在内存中。 然后,我为Hashing Vectorizer创建了tokenizer函数

def tokenizer(text):
text = re.sub('<[^>]*>', '', text)
emoticons = re.findall('(?::|;|=)(?:-)?(?:\(|D|P)',text.lower())
text = re.sub('[\W]+', ' ', text.lower())
text += ' '.join(emoticons).replace('-', '')
tokenized = [w for w in tokenizer_porter(text) if w not in stop]
return tokenized

Then I created the Hashing Vectorizer object.

然后,我创建了哈希向量化器对象。

from sklearn.feature_extraction.text import HashingVectorizervect = HashingVectorizer(decode_error='ignore', n_features=2**21, 
preprocessor=None,tokenizer=tokenizer)

模型 (Model)

For the Model, I used the stochastic gradient descent classifier algorithm.

对于模型,我使用了随机梯度下降分类器算法。

from sklearn.linear_model import SGDClassifier
clf = SGDClassifier(loss='log', random_state=1)

培训与验证 (Training and Validation)

X = df["tweet"].to_list()
y = df['label']

For the model, I used 80% for training and 20% for testing.

对于模型,我使用了80%的训练和20%的测试。

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,
y,
test_size=0.20,
random_state=0)

Then I transformed the text data to vectors with the Hashing Vectorizer we created earlier:

然后,使用之前创建的Hashing Vectorizer将文本数据转换为矢量:

X_train = vect.transform(X_train)
X_test = vect.transform(X_test)

Finally, I then fit the data to the algorithm

最后,然后将数据拟合到算法中

classes = np.array([0, 1])
clf.partial_fit(X_train, y_train,classes=classes)

Let's test the accuracy on our test data:

让我们在测试数据上测试准确性:

print('Accuracy: %.3f' % clf.score(X_test, y_test))

Output:

输出:

Accuracy: 0.912

I had an accuracy of 91% which is fair enough, after that, I then updated the model with the prediction

我的准确度是91%,这还算公允,之后,我用预测更新了模型

clf = clf.partial_fit(X_test, y_test)

测试和做出预测 (Testing and Making Predictions)

I added the text “I’ll kill myself am tired of living depressed and alone” to the model.

我在模型中添加了文本“我会厌倦生活在沮丧和孤独中,杀死自己”。

label = {0:'negative', 1:'positive'}
example = ["I'll kill myself am tired of living depressed and alone"]
X = vect.transform(example)
print('Prediction: %s\nProbability: %.2f%%'
%(label[clf.predict(X)[0]],np.max(clf.predict_proba(X))*100))

And I got the output:

我得到了输出:

Prediction: positive
Probability: 93.76%

And when I used the following text “It’s such a hot day, I’d like to have ice cream and visit the park”, I got the following prediction:

当我使用以下文字“天气真热,我想吃冰淇淋并参观公园”时,我得到以下预测:

Prediction: negative
Probability: 97.91%

The model was able to predict accurately for both cases. And that's how you build a simple suicidal tweet classifier.

该模型能够准确预测这两种情况。 这就是您构建简单的自杀性推文分类器的方式。

You can find the notebook I used for this article here

您可以在这里找到我用于本文的笔记本

Thanks for reading 😊

感谢您阅读😊

翻译自: https://towardsdatascience.com/building-a-suicidal-tweet-classifier-using-nlp-ff6ccd77e971

nlp构建

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392136.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

域名跳转

案例&#xff1a;当访问lsx.com网站&#xff0c;是我最早论坛的域名。回车之后会自动跳转到lshx.com。 为什么药lsx跳转到lshx.com呢&#xff1f; 为了统一品牌。建议换成了lshx.com。所有之前的lsx.com就不要用了&#xff0c;就让它跳转到lshx.com。是因为之前lsx.com上有很多…

Elastic Stack 安装

Elastic Stack 是一套支持数据采集、存储、分析、并可视化全面的分析工具&#xff0c;简称 ELK&#xff08;Elasticsearch&#xff0c;Logstash&#xff0c;Kibana&#xff09;的缩写。 安装Elastic Stack 时&#xff0c;必须相关组件使用相同的版本&#xff0c;例如&#xff1…

区块链去中心化分布式_为什么渐进式去中心化是区块链的最大希望

区块链去中心化分布式by Arthur Camara通过亚瑟卡马拉(Arthur Camara) 为什么渐进式去中心化是区块链的最大希望 (Why Progressive Decentralization is blockchain’s best hope) 不变性是区块链的最大优势和最大障碍。 逐步分权可能是答案。 (Immutability is blockchain’s…

编译原理—语义分析(Java)

递归下降语法制导翻译 实现含多条简单赋值语句的简化语言的语义分析和中间代码生成。 测试样例 begin a:2; b:4; c:c-1; area:3.14*a*a; s:2*3.1416*r*(hr); end #词法分析 public class analyzer {public static List<String> llistnew ArrayList<>();static …

linux问题总结

linux问题总结 编写后台进程的管理脚本&#xff0c;使用service deamon-name stop的时候&#xff0c;出现如下提示&#xff1a;/sbin/service: line 66: 23299 Terminated env -i LANG"$LANG" PATH"$PATH" TERM"$TERM" "${SERVICEDIR}/${SE…

linux vi行尾总是显示颜色,【转载】Linux 下使用 vi 没有颜色的解决办法

vi 是没有颜色的&#xff0c;vim 是有颜色的。我们可以通过 rpm -qa |grep vim 看看系统中是否安装了下面 3 个 rpm 包&#xff0c;如果有就是安装了 vim 。[rootBetty ~]# rpm -qa |grep vimvim-minimal-7.0.109-7.el5vim-enhanced-7.0.109-7.el5vim-common-7.0.109-7.el5如果…

时间序列分析 lstm_LSTM —时间序列分析

时间序列分析 lstmNeural networks can be a hard concept to wrap your head around. I think this is mostly due to the fact that they can be used for so many different things such as classification, identification or just simply regression.神经网络可能是一个难…

关于计算圆周率PI的经典程序

短短几行代码&#xff0c;却也可圈可点。如把变量s放在PI表达式中&#xff0c;还有正负值的处理&#xff0c;都堪称经典。尤其是处处考虑执行效率的思想令人敬佩。 /* pi/41-1/31/5-1/71/9-…… */ #include <stdio.h> int main(){ int s1; float pi0.,n1.,…

华为产品技术学习笔记之路由原理(一)

路由器&#xff1a;路由器是一种典型的网络连接设备&#xff0c;用来进行路由选择和报文转发。路由器与它直接相连的网络的跳数为0&#xff0c;通过一台路由器可达的网络的跳数为1.路由协议&#xff1a;路由器之间维护路由表的规则&#xff0c;用以发现路由&#xff0c;生成路由…

Linux网络配置:设置IP地址、网关DNS、主机名

查看网络信息 1、ifconfig eth0 2、ifconfig -a 3、ip add 设置主机名需改配置文件&#xff1a; /etc/hosts /etc/sysconfig/network vim /etc/sysconfig/network NETWORKINGyes NETWORKING_IPV6no HOSTNAMEwendyhost Linux配置网络 方法一&#xff1a; 1、使用setup命令进入如…

编译原理—小型(简化)高级语言分析器前端(Java)

实现一个一遍扫描的编译前端&#xff0c;将简化高级语言的部分语法成分&#xff08;含赋值语句、分支语句、循环语句等&#xff09;翻译成四元式&#xff08;或三地址代码&#xff09;&#xff0c;还要求有合理的语法出错报错和错误恢复功能。 测试样例 beginwhile a<b do…

linux boot菜单列表,Bootstrap 下拉菜单(Dropdowns)简介

Bootstrap 下拉菜单是可切换的&#xff0c;是以列表格式显示链接的上下文菜单。这可以通过与 下拉菜单(Dropdown) JavaScript 插件 的互动来实现。如需使用下拉菜单&#xff0c;只需要在 class .dropdown 内加上下拉菜单即可。下面的实例演示了基本的下拉菜单&#xff1a;实例主…

dynamodb管理ttl_如何使用DynamoDB TTL和Lambda安排临时任务

dynamodb管理ttlby Yan Cui崔燕 如何使用DynamoDB TTL和Lambda安排临时任务 (How to schedule ad-hoc tasks with DynamoDB TTL and Lambda) CloudWatch Events let you easily create cron jobs with Lambda. However, it’s not designed for running lots of ad-hoc tasks,…

5g创业的构想_数据科学项目的五个具体构想

5g创业的构想Do you want to enter the data science world? Congratulations! That’s (still) the right choice.您想进入数据科学世界吗&#xff1f; 恭喜你&#xff01; 那(仍然)是正确的选择。 The market currently gets tougher. So, you must be mentally prepared f…

Microsoft Windows Phone 7 Toolkit Silverlight SDK XNA Game Studio 4.0 开发工具套件正式版下载...

Windows Phone 7开发工具套件包括Visual Studio 2010 Express for Windows Phone、Windows Phone模拟器、Expression Blend 4 for Windows Phone、XNA Game Studio 4.0和新增加的必应地图SDK。 英文版的光盘镜像&#xff1a;点击下载 文档中心&#xff1a;Windows Phone develo…

数据挖掘—Apriori算法(Java实现)

算法描述 &#xff08;1&#xff09;扫描全部数据&#xff0c;产生候选1-项集的集合C1&#xff1b; &#xff08;2&#xff09;根据最小支持度&#xff0c;由候选1-项集的集合C1产生频繁1-项集的集合L1&#xff1b; &#xff08;3&#xff09;对k>1&#xff0c;重复执行步骤…

怎么汇报一周开发工作情况_如何在没有经验的情况下获得第一份开发人员工作

怎么汇报一周开发工作情况Whether you’ve done a coding bootcamp or taught yourself, getting your first developer job with only a few months of coding under your belt is hard.无论您是完成了编码训练营还是自学了&#xff0c;仅靠几个月的编码就很难拿到第一份开发人…

vue.js的认知

Vue.js&#xff08;读音 /vjuː/, 类似于 view&#xff09; 是一套构建用户界面的渐进式框架。 Vue 只关注视图层&#xff0c; 采用自底向上增量开发的设计。 Vue 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件。 Vue 学习起来非常简单&#xff0c;。转载于…

c语言中的无符号字节,C语言之有符号数和无符号数

我们知道&#xff0c;在C语言中存在无符号数和有符号数(一些高级语言如Java里面是没有无符号数的)&#xff0c;但是对于计算机而言&#xff0c;其本身并不区别有符号数和无符号数&#xff0c;因为在计算机里面都是0或者1&#xff0c;但是在我们的实际使用中有时候需要使用有符号…

8种排序算法比较

8种排序算法&#xff0c;各算法名称见下表或见源码。运行程序时&#xff0c;将需要你输入一数值&#xff0c;以确定对多少随机数进行排序。然后将会显示各排序算法的耗时。并且你可选择时否进行正序和反序测试。 由于水平有限&#xff0c;可能存在一些错误&#xff0c;还请各位…