Social Media Ads Classification with Machine Learning

The classification of social media ads is all about analyzing the ads for classifying whether your target audience will buy the product or not. It’s a great use case for data science in marketing. So, if you want to learn how to analyze social media ads to classify your target audience, then this article is for you. In this article, I will walk you through the task of social media ads classification with machine learning using Python.

Social Media Ads Classification

Classifying social media ads means analyzing your social media ads for finding the most profitable customers for your product who are more likely to buy the product. Sometimes the product you are offering is not suitable for all people when it comes to age and income. For example, a person between the ages of 20 and 25 may like to spend more on smartphone covers than a person between the ages of 40 and 45.

Likewise, a high-income person can afford to spend more on luxury goods than a low-income person. So this is how a business can determine whether a person will buy their product or not by classifying their social media ads. In the section below, I will walk you through social media ads classification with Machine Learning using Python.

Social Media Ads Classification using Python

The dataset I am using for the task of Social Media Ads Classification is downloaded from Kaggle. It contains data about a product’s social media advertising campaign. It contains features like:

  1. the age of the target audience
  2. the estimated salary of the target audience
  3. and whether the target audience has purchased the product or not

So let’s import the dataset and necessary Python libraries to start this task:

   Age  EstimatedSalary  Purchased
0   19            19000          0
1   35            20000          0
2   26            43000          0
3   27            57000          0
4   19            76000          0

Now let’s take a look at some of the insights from the data to see if we need to make any changes to the dataset:

print(data.describe())
print(data.isnull().sum())
              Age  EstimatedSalary   Purchased
count  400.000000       400.000000  400.000000
mean    37.655000     69742.500000    0.357500
std     10.482877     34096.960282    0.479864
min     18.000000     15000.000000    0.000000
25%     29.750000     43000.000000    0.000000
50%     37.000000     70000.000000    0.000000
75%     46.000000     88000.000000    1.000000
max     60.000000    150000.000000    1.000000
Age                0
EstimatedSalary    0
Purchased          0
dtype: int64

Now let’s explore some of the important patterns in the dataset. The first thing I want to explore is the ages of the people who responded to the social media ads and bought the product:

product bought through social media by people according to their age

The visualization above shows that people over 45 among the target audience are more interested in purchasing the product. Now let’s take a look at the income group of people who responded to social media ads and purchased the product:

Social Media Ads Classification

The visualization above shows that people with a monthly income of over 90,000 among the target audience are more interested in purchasing the product.

Training a Social Media Ads Classification Model

Now let’s train a model to classify social media ads. First I’ll set the “Purchased” column in the dataset as the target variable and the other two columns as the features we need to train a model:

x = np.array(data[["Age", "EstimatedSalary"]])
y = np.array(data[["Purchased"]])

Now let’s split the data and train a social media ads classification model using the decision tree classifier:

At last, let’s have a look at the classification report of the model:

print(classification_report(ytest, predictions))
              precision    recall  f1-score   support

           0       0.88      0.85      0.87        27
           1       0.71      0.77      0.74        13
    accuracy                           0.82        40
   macro avg       0.80      0.81      0.80        40
weighted avg       0.83      0.82      0.83        40

Summary

So this is how you can analyze and classify social media ads about the marketing campaign of a product. Classifying social media ads means analyzing your social media ads for finding the most profitable customers for your product who are more likely to buy the product. I hope you liked this article on classifying Social Media Ads with Machine Learning using Python. Feel free to ask your valuable questions in the comments section below.

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1538

Leave a Reply