日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/11/29 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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構建

總結

以上是生活随笔為你收集整理的nlp构建_使用NLP构建自杀性推文分类器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 99精品中文字幕 | 亚洲精品自拍 | 亚洲一区在线不卡 | 国产一区二区在线观看免费 | av无码久久久久久不卡网站 | 鲁一鲁在线视频 | 岛国大片在线观看 | 精品九九九九 | 日韩视频免费观看 | 国产真实乱人偷精品 | av网址观看 | 综合网av | 爱爱免费视频网站 | 91福利专区| 懂色av蜜臀av粉嫩av分享吧 | 免费一区 | 亚洲欧美综合在线观看 | 香港三级韩国三级日本三级 | 亚洲校园激情 | 国产在线看 | 热热色国产| 亚洲国产无码久久 | 8x8ⅹ8成人免费视频观看 | 调教小屁屁白丝丨vk | 国产美女免费观看 | 国产视频中文字幕 | 海角社区在线视频播放观看 | 欧洲视频一区 | 在线免费国产 | 久久黄色小视频 | 欧美一级片网址 | 三级视频网站在线观看 | 欧美日韩国产综合网 | 尤物国产视频 | 美女狠狠干 | 中文字幕国产 | 噜噜色网 | 加勒比成人av | 四虎在线免费播放 | 久色视频在线观看 | av在线日韩 | 91视频播放 | 中国在线观看片免费 | 亚洲久久久久久 | 国产成人在线免费观看 | 91成人在线视频 | 久草视频在 | 成人小视频免费看 | 国产精品呻吟 | 色av影院 | 日韩精品在线免费视频 | 怡春院欧美 | 久久精品123 | 国产精品96久久久久久 | 一本大道久久 | 双性皇帝高h喷汁呻吟 | www黄色在线观看 | 亚洲精品 日韩无码 | 午夜看片在线 | 99久久久国产精品 | 国内精品第一页 | 久久久久久久久久久久久久免费看 | 欧美日韩中文视频 | 亚洲欧美国产日韩精品 | 国产伦理一区 | 一级黄在线观看 | 欧美成人三级伦在线观看 | 国产精品久久一区二区三区 | 亚洲一区和二区 | 丰满人妻一区二区三区免费视频 | 都市激情久久 | 91精品在线观看视频 | 久久婷婷亚洲 | 黄色av网站免费在线观看 | 亚洲九九九九 | 欧美日韩在线播放 | 天天插天天操 | 国产超碰 | 国产21页| 免费a在线观看 | 国产不卡精品视频 | 日韩亚洲欧美综合 | 亚洲综合免费 | 91无限观看 | 性感少妇在线观看 | 中文字幕第8页 | 丁香婷婷激情 | 免费在线看黄网址 | 欧美黑人又粗又大又爽免费 | 玉足调教丨vk24分钟 | 99视频99 | 亚洲第二色 | 在线午夜电影 | 亚洲av无码一区二区三区网址 | 一本色道久久综合亚洲二区三区 | 在线亚洲欧美 | www.国产精品.com | 做a爰小视频 | 秋霞亚洲|