You have been hired as a consultant to a start-up that is running a targetted marketing ads on facebook. The company wants to anaylze customer behaviour by predicting which customer clicks on the advertisement. Customer data is as follows:
Inputs:
Outputs:
source: Dr. Ryan @STEMplicity
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
url = "https://datascienceschools.github.io/Machine_Learning/Classification_Models_CaseStudies/Facebook_Ads.csv"
dataset = pd.read_csv(url, encoding = 'latin_1')
dataset.head()
click = dataset[dataset['Clicked'] == 1]
not_click = dataset[dataset['Clicked'] == 0]
print("Total Click =", len(dataset))
print("\nNumber of customers who clicked on Ad =", len(click))
print("Percentage Clicked =", 1.*len(click)/len(dataset)*100.0, "%\n")
print("Did not Click =", len(not_click))
print("Percentage who did not Click =", 1.*len(not_click)/len(dataset)*100.0, "%")
sns.scatterplot(dataset['Time Spent on Site'],dataset['Salary'], hue = dataset['Clicked'])
plt.title('Facebook Ad: Customer Click')
plt.show()
plt.figure(figsize =(5,5))
sns.boxplot(dataset['Clicked'], dataset['Salary'])
plt.show()
plt.figure(figsize =(5,5))
sns.boxplot(dataset['Clicked'], dataset['Time Spent on Site'])
plt.show()
plt.hist(dataset['Salary'], bins=40)
plt.title('Distribution of Salary')
plt.show()
plt.hist(dataset['Time Spent on Site'], bins=20)
plt.title('Distribution of Time Spent on Site')
plt.show()
- Drop unnecessary columns (Names, emails, Country)
- dataset.drop(['Names', 'emails', 'Country'], axis=1, inplace=True)
- Declaring the Dependent & the Independent Variables
- X = dataset.drop('Clicked',axis=1).values
- y = dataset['Clicked'].values
X = dataset.iloc[:, 3:-1].values
y = dataset.iloc[:, -1].values
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 7)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(random_state = 0)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy is: {:.2f}%".format(accuracy*100))
sns.heatmap(cm, annot = True, fmt="d")
plt.show()
from sklearn.metrics import classification_report
print(classification_report(y_test, y_pred))
from sklearn.model_selection import cross_val_score
accuracies = cross_val_score(estimator = model, X = X_train, y = y_train, cv = 10)
print("Accuracy: {:.2f} %".format(accuracies.mean()*100))
print("Standard Deviation: {:.2f} %".format(accuracies.std()*100))
from matplotlib.colors import ListedColormap
X_set, y_set = X_train, y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, model.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(('magenta', 'blue')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
color = ListedColormap(('magenta', 'blue'))(i), label = j)
plt.title('Logistic Regression (Training set)')
plt.xlabel('Time Spent on Site')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()
from matplotlib.colors import ListedColormap
X_set, y_set = X_test, y_test
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, model.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(('magenta', 'blue')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
color = ListedColormap(('magenta', 'blue'))(i), label = j)
plt.title('Logistic Regression (Test set)')
plt.xlabel('Time Spent on Site')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()