Using Natural Language Processing to prevent suicide

The Basics

Natural Language Processing

Sentiment Analysis to prevent Suicide

Step 1

# TFIDF Vector
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.svm import LinearSVC
from sklearn.metrics import classification_report
X = df['tweet']
tfidf = TfidfVectorizer(max_features=10000, ngram_range=(1, 2))
X = tfidf.fit_transform(X)
y = df['intention']

Step 2

import warnings
warnings.simplefilter("ignore", DeprecationWarning)# Load the LDA model from sk-learn
from sklearn.decomposition import LatentDirichletAllocation as LDA

# Helper function
def print_topics(model, count_vectorizer, n_top_words):
words = count_vectorizer.get_feature_names()
for topic_idx, topic in enumerate(model.components_):
print("\\nTopic #%d:" % topic_idx)
print(" ".join([words[i]
for i in topic.argsort()[:-n_top_words - 1:-1]]))

# Tweak the two parameters below
number_topics = 5
number_words = 10# Create and fit the LDA model
lda = LDA(n_components=number_topics, n_jobs=-1)
lda.fit(count_data)# Print the topics found by the LDA model
print("Topics found via LDA:")
print_topics(lda, count_vectorizer, number_words)
Topic 1: Possibly Suicide
Words: "kill" , "die" , "death" , *"worthless" , "murder" , "self-murder" , "depressed", "Lonely"
Topic 2: Normal
Words:"american","alright" , "covid-19" , "good" , "lover"

Let’s test the model!

X = 'I just want my life to end already'
vec = tfidf.transform([X])
clf.predict(vec)
array([1])
X = 'congratulations, you have done it'
vec = tfidf.transform([X])
array([0])

Training and testing our model

The Future

--

--

--

15 yo futurist, change maker and innovator at The Knowledge Society.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Reading Traffic Signs with Human Like Accuracy

Introduction to Neural Networks For Self Driving Cars (Foundational Concepts — Part 2)

What is attention mechanism? Can I have your attention please?

Predicting Chaotic Systems Using Reservoir Computing

Self Questionnaire for ML Projects

Day4-Machine Learning Specialization with Python

Top 4 commonly used Machine Learning Algorithms

Setting up PyTorch and TensorFlow on a Windows Machine

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alisha Arora

Alisha Arora

15 yo futurist, change maker and innovator at The Knowledge Society.

More from Medium

Artificial Intelligence vs. Augmented Intelligence

AI, The Future of Healthcare

Sentiment Graphs of COVID News Articles Since 2020