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.
# TF-IDF Vectorization from sklearn.feature_extraction.text import TfidfVectorizer vectorizer = TfidfVectorizer(max_features=2000) X = vectorizer.fit_transform(df['Tweet']) y = df.Suicide
From raw text data
to a life-saving AI.
The complete pipeline for the Suicide Tweet Prediction machine learning project. Click any step to jump straight to its code and beginner-friendly explanation.
Data Import
Load the Suicidal Tweet Detection dataset from Kaggle into a Pandas DataFrame.
Data Check & Processing
Check for null values, inspect class distribution, and encode string labels into numerical format using LabelEncoder.
TF-IDF Vectorization
Convert raw tweet text into numerical features using Term Frequency-Inverse Document Frequency.
Deep Learning Model (TensorFlow)
Build and train a Keras Sequential neural network with GELU activations to classify tweets.
Multi-Model Benchmark
Train and compare 5 powerful ML algorithms: XGBoost, CatBoost, LightGBM, Random Forest, and SVC.
Voting Ensemble & Conclusion
Combine all models into a soft Voting Classifier to create a robust, high-accuracy final prediction system.
Load the dataset.
Introducing our groundbreaking project: Suicide Tweet Prediction Analysis. We start by importing the dataset from Kaggle.
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.
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')
dfInspect & encode the labels.
We need to check for missing values and convert our text labels into numbers that a machine learning model can understand.
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.
# 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)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.
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.
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)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.
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!
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}")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.
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!
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}")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.
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.
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}")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. ๐๐ค๐
Share this Project
If you found this walkthrough helpful, share it with your friends and fellow developers!
