NLP & Deep Learning Project ยท Mental Health AI

Suicide Tweet
Prediction
Analysis.

With an unwavering commitment to mental health and well-being, we have harnessed the power of advanced data analysis to create a platform that identifies and analyzes tweets with the potential for self-harm indicators. This project combines cutting-edge machine learning algorithms with linguistic and contextual analysis to accurately predict and flag concerning content.

Start the Walkthrough View Dataset
๐Ÿง NLP & TF-IDF
1,787Tweets analyzed
6ML & DL models
94%Best accuracy
tweet_analysis.py
# TF-IDF Vectorization
from sklearn.feature_extraction.text import TfidfVectorizer

vectorizer = TfidfVectorizer(max_features=2000)
X = vectorizer.fit_transform(df['Tweet'])
y = df.Suicide
94% AccuracyEnsemble model
6 ModelsBenchmarked & voted
NLP PowerTF-IDF + Keras
Step 01 / Data Import

Load the dataset.

Introducing our groundbreaking project: Suicide Tweet Prediction Analysis. We start by importing the dataset from Kaggle.

๐Ÿง‘โ€๐Ÿซ Beginner’s Guide โ€” Data Import

What is this dataset? The Suicidal Tweet Detection Dataset contains Twitter posts labeled as either “Not Suicide post” or “Potential Suicide post”.

Why Pandas? Pandas is the standard Python library for data manipulation. pd.read_csv() loads our data into a DataFrame, which is like an Excel spreadsheet inside Python.

step1_import.py
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O

df = pd.read_csv('/kaggle/input/suicidal-tweet-detection-dataset/Suicide_Ideation_Dataset(Twitter-based).csv')
df
Out[1]:
Tweet Suicide 0 making some lunch Not Suicide post 1 @Alexia You want his money. Not Suicide post 2 @dizzyhrvy that crap took me forever to put to… Potential Suicide post 3 @jnaylor #kiwitweets Hey Jer! Since when did y… Not Suicide post 4 Trying out “Delicious Library 2” wit… Not Suicide post … … … 1786 @dancingbonita “I friggin love you!!!”&quo… Not Suicide post1787 rows ร— 2 columns
Step 02 / Data Check

Inspect & encode the labels.

We need to check for missing values and convert our text labels into numbers that a machine learning model can understand.

๐Ÿง‘โ€๐Ÿซ Beginner’s Guide โ€” Data Cleaning

Checking for nulls: df.isnull().sum() tells us if any rows are missing data. We found 2 missing tweets, which we will drop later.

Class distribution: value_counts() shows we have 1,127 “Not Suicide” posts and 660 “Potential Suicide” posts.

LabelEncoder: Machine learning models only understand numbers. LabelEncoder converts “Not Suicide post” to 0 and “Potential Suicide post” to 1.

step2_check.py
# Check for missing values
df.isnull().sum()

# Check class distribution
df.Suicide.value_counts()

# Encode labels
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
df.Suicide = le.fit_transform(df.Suicide)
Out[2]:
Tweet 2 Suicide 0 dtype: int64
Out[3]:
Not Suicide post 1127 Potential Suicide post 660 Name: Suicide, dtype: int64
Out[4]:
0 0 1 0 2 1 3 0 .. 1782 0 Name: Suicide, Length: 1787, dtype: int64
Step 03 / TF-IDF

Convert text to numbers with TF-IDF.

Machine learning models cannot read raw text. We use the Term Frequency-Inverse Document Frequency (TF-IDF) technique to convert tweets into numerical vectors.

๐Ÿง‘โ€๐Ÿซ Beginner’s Guide โ€” NLP Vectorization

What is TF-IDF? It evaluates how important a word is to a document in a collection. Words that appear frequently in one tweet but rarely in others get a higher score.

Max Features: We limit the vocabulary to the top 2000 words to keep our dataset manageable and avoid overfitting to rare words.

Splitting: We divide the data into 75% for training the models and 25% for testing their accuracy.

step3_tfidf.py
from sklearn.feature_extraction.text import TfidfVectorizer

# Drop missing values first
df = df.dropna()

vectorizer = TfidfVectorizer(max_features=2000)
X = vectorizer.fit_transform(df['Tweet'])
y = df.Suicide
X = X.toarray()

# Split data (75% train, 25% test)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
Step 04 / Deep Learning

Build a neural network with TensorFlow.

By leveraging this technology, we aim to provide timely interventions. Our first model is a Keras Sequential neural network using GELU activations.

๐Ÿง‘โ€๐Ÿซ Beginner’s Guide โ€” Keras Neural Network

The Architecture: We use 3 layers. Dense(128) and Dense(64) extract complex patterns from the text, while Dense(1, activation='sigmoid') outputs a probability between 0 and 1.

Epochs & Batch Size: The model processes 32 tweets at a time (batch size) and makes 5 full passes through the dataset (epochs).

The Result: In just 5 epochs, the neural network achieves a 93% accuracy score on unseen test data!

step4_tensorflow.py
import tensorflow as tf
from sklearn.metrics import accuracy_score

model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(128, activation='gelu', input_shape=(X.shape[1],)),
    tf.keras.layers.Dense(64, activation='gelu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=5, batch_size=32)

y_pred = model.predict(X_test)
y_pred = np.round(y_pred)
score = accuracy_score(y_pred, y_test)
print(f"Accuracy Score: {score:.2f}")
Out[7]:
Epoch 1/5 42/42 [==============================] – 2s 6ms/step – loss: 0.6235 – accuracy: 0.6607 Epoch 2/5 42/42 [==============================] – 0s 5ms/step – loss: 0.2937 – accuracy: 0.9178 Epoch 3/5 42/42 [==============================] – 0s 5ms/step – loss: 0.0910 – accuracy: 0.9776 Epoch 4/5 42/42 [==============================] – 0s 5ms/step – loss: 0.0386 – accuracy: 0.9925 Epoch 5/5 42/42 [==============================] – 0s 5ms/step – loss: 0.0184 – accuracy: 0.9970 14/14 [==============================] – 0s 2ms/step ————————————– Accuracy Score: 0.93
Step 05 / Multi-Models

Benchmarking 5 powerful ML algorithms.

To ensure we have the best possible model, we test 5 popular gradient boosting and ensemble algorithms alongside our neural network.

๐Ÿง‘โ€๐Ÿซ Beginner’s Guide โ€” Ensemble Models

Why test multiple models? Different algorithms make different mistakes. By training XGBoost, CatBoost, LightGBM, Random Forest, and SVC, we can see which handles this specific text data best.

The Results: CatBoost and LightGBM tie for the top spot at 94% accuracy, beating the TensorFlow neural network!

step5_multimodel.py
from xgboost import XGBClassifier
from catboost import CatBoostClassifier
from lightgbm import LGBMClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC

models = [
    ('XGBClassifier', XGBClassifier()),
    ('CatBoostClassifier', CatBoostClassifier(verbose=0)),
    ('LGBMClassifier', LGBMClassifier()),
    ('RandomForestClassifier', RandomForestClassifier()),
    ('SVC', SVC(probability=True))
]

for model_name, model in models:
    print(f"Training {model_name}...")
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)
    score = accuracy_score(y_pred, y_test)
    print(f"{model_name} Accuracy Score: {score:.2f}")
Out[9]:
Training XGBClassifier… XGBClassifier Accuracy Score: 0.93 ————————————– Training CatBoostClassifier… CatBoostClassifier Accuracy Score: 0.94 ————————————– Training LGBMClassifier… LGBMClassifier Accuracy Score: 0.94 ————————————– Training RandomForestClassifier… RandomForestClassifier Accuracy Score: 0.93 ————————————– Training SVC… SVC Accuracy Score: 0.93 ————————————–
Step 06 / Voting Ensemble

Combine everything into a Voting Classifier.

The Suicide Tweet Prediction Analysis project stands as a testament to the positive applications of technology for the greater good. Let’s combine our models for maximum impact.

๐Ÿง‘โ€๐Ÿซ Beginner’s Guide โ€” Voting Ensemble

What is a Voting Classifier? Instead of picking just one model, we let all 5 of our top models “vote” on whether a tweet indicates suicidal ideation. If the majority says yes, the final prediction is yes.

Soft Voting: Because we set voting='soft', the models average their probabilities instead of just saying yes/no. This usually results in higher accuracy.

The Final Result: Our Voting Ensemble achieves a final accuracy of 94%, making it a robust tool for identifying concerning content.

step6_voting.py
from sklearn.ensemble import VotingClassifier

# 'models' list from Step 5
voting_classifier = VotingClassifier(estimators=models, voting='soft')

print(f"Training voting classifier...")
voting_classifier.fit(X_train, y_train)

y_pred2 = voting_classifier.predict(X_test)
score = accuracy_score(y_pred2, y_test)
print(f"Voting Ensemble Accuracy Score: {score:.2f}")
Out[10]:
Training voting classifier… Voting Ensemble Accuracy Score: 0.94
โœ“Model trained successfully. Together, we can make a difference. ๐Ÿ’™
The Impact: With the potential to save lives and make a meaningful impact, this project demonstrates how advanced data analysis can be used for the greater good. Join us in our mission to create a safer and more supportive online community.
08 / FAQ

Frequently Asked Questions

What is the Suicide Tweet Prediction Analysis project? +

It is an NLP and machine learning project that analyzes Twitter data to identify and flag potential suicidal ideation, aiming to provide timely interventions and support mental health research.

How are the tweets processed for machine learning? +

The text data is processed using TF-IDF Vectorization (Term Frequency-Inverse Document Frequency) to convert the tweets into numerical features that the ML and Deep Learning models can understand.

Which models are used in this analysis? +

The project benchmarks a TensorFlow Keras neural network against XGBoost, CatBoost, LightGBM, Random Forest, and SVC, ultimately combining them into a soft Voting Classifier ensemble.

Run the project
yourself.

Open a terminal, install the required libraries, and run the file end-to-end. Or click “Copy Full Project Code” at the top of this page. Together, we can make a difference. ๐Ÿ’™๐Ÿค–๐ŸŒŸ

NLP & TF-IDF6 ML & DL modelsMental Health AIFull source code
09 / Share

Share this Project

If you found this walkthrough helpful, share it with your friends and fellow developers!

Scroll to Top