Reddit/Twitter Posts Classifications using Logistic Regression
This video builds a logistic regression classifier for depressed versus non-depressed Reddit and Twitter posts, reaching around 95 percent accuracy.
Social media posts carry more signal about a person's mental state than most people realize, and machine learning offers one way to systematically detect it. This video works through a project that classifies Reddit and Twitter posts as depressed or non-depressed using logistic regression, walking through both the underlying math and the full implementation pipeline.
The logistic regression foundation
Logistic regression is a statistical method used for binary classification, meaning it works when the predicted classes are binary, either yes or no, or zero or one. Rather than outputting a direct value, it predicts the probability that an instance belongs to one of the two classes. That probability comes from the sigmoid function, sigma of z equals 1 over 1 plus e to the negative z, where z is a weighted combination of the input features: z equals b plus x1w1 plus x2w2 and so on through xnwn, where the x values are the input features, the w values are their respective weights, and b is the bias term.
Defining the classification task
Post classification, in this context, means categorizing or labeling content, and here the goal is to sort posts into depressed or non-depressed categories. The data comes from posts made on Reddit and Twitter.
Building the dataset
The project draws from three separate datasets: a Reddit dataset, a Twitter dataset, and a Twitter non-advertisement dataset. Each has a different structure. The Reddit dataset includes a title, body, and classification label. The Twitter dataset includes text, hashtags, and labels. The Twitter non-advertisement dataset includes only text and a label. These three dataframes are merged by dropping the columns they don't have in common and joining what remains into a single combined dataset. Null values are then checked for and dropped.
Cleaning and preparing the text
Text preprocessing follows a standard pipeline: converting all characters to lowercase, removing punctuation, tokenizing the text, removing English stop words, filtering the tokens, and finally joining everything back together into a new "clean text" column. For model training, the combined dataset is split into training and test sets using an 80/20 ratio, with a split index marking where the training data ends and the test data begins.
Vectorizing and training the model
After the train/test split, the text is transformed using a TF-IDF vectorizer, a numerical statistic that reflects how important a word is to a document relative to the collection of documents it belongs to. Logistic regression is then applied to the vectorized data, with L2 regularization used to improve the model's performance and reduce overfitting. After fitting the model, predictions are generated and evaluated for accuracy.
The result
The final model reached an accuracy of around 95%, notably higher than the K Nearest Neighbors approach covered later in the same project series, suggesting logistic regression handled this particular text classification task especially well.
Key takeaways
- Logistic regression predicts the probability of binary class membership using the sigmoid function rather than a direct label.
- The project combines three separate Reddit and Twitter datasets, standardizing their differing columns before merging.
- Text preprocessing includes lowercasing, punctuation removal, tokenization, and stop word filtering before vectorization.
- TF-IDF vectorization converts cleaned text into numerical features suitable for the model.
- L2-regularized logistic regression achieved approximately 95% accuracy on the depressed-post classification task.
Who this is for
This video suits learners who want a clear, math-grounded introduction to binary text classification. It is part of a Humanitarians AI series that compares logistic regression against K Nearest Neighbors on the same depressed-post detection task, useful for anyone weighing which algorithm fits a given text classification problem.
Full transcript(auto-generated, with timestamps)
[0:01]Hello everyone today let us understand the project post classification into depressed post and non depressed post using logistic regression so what is logistic regression it is a statistical method used for binary classification which is it is used when the predicted classes are binary and which are either simple yes or no or zero or one classes and here instead of giving the direct values it predicts the probability of an instance belonging to one of two classes the hypothesis function used in logistic regression to predict the probability of an instance belonging to the class is Sigma of Z = 1 y 1 + e powerus z where Z is the
[1:05]Combination of input features weed by coefficients where c = b + X1 W1 that's X2 W2 so on xn w n where X1 X2 and so on xn are the input features W1 W2 and and so on are the weights of the respective input features and B is the bias term now let us understand what is post classification it usually refers to the process of categorizing or labeling the content and today our goal is to classify the post into depressed or not topress post and the data is collected from the post that that are made on credit and on Twitter here is the implementation of
[2:07]The project the first step is the data collection the data is collected from various data sets that is the Reddit data set the Twitter data set and the Twitter non add data set where each data set has different features like one has the title body and body title and its classified label the Twitter data set has the text the hashtags or and the labels and the Twitter non-ad data set has only the text and the label Now using these three data frames I am dropping the uncommon uh columns and joining all the three data sets into one data set and from here I'm checking for
[3:04]The null values since the data already has null values I am dropping those null values and here in the pre-processed text we are cleaning the text that is converting all the characters into lower and removing few punctuations tokenizing the text removing the stop words in English and filtering the tokens and finally joining everything and adding it in the column clean the text now for the model training I want to split the data set into training set and uh test set for this I'm using a split index and all the values before the split index are the train uh data and the data after the split index is the
[4:03]Test data here for the train and test I'm using 8020 ratio now after splitting the data into train and test data I want to transform the data for that I'm using TF ID factorer which is a numerical statistic that reflects the importance of a word in a document related to collection of documents and after that I'm using the logistic regression and here uh in order to increase the performance of our model I'm using L2 regularization and uh fitting the model later I made the predictions and found the accuracy of the predictions which is around 95% one6 thank you
More videos
2:08Bridging the Pixel Gap in Browser Automation.
2:23How One Narrow Safety Rule Can Make an AI Less Safe Everywhere Else.
2:04Why splitting a chunk from its document makes it retrieve for the wrong question
4:20Three You Can Take Back. One You Can't.
2:21Why a 50-turn agent pays for the same screenshot 35 times unless it caches the pixels
1:53