
Marketing is crucial for the growth and sustainability of a any business. Marketers can help build the company's brand, engage customers, grow revenue, and increase sales. One of the key pain points for marketers is to know their customers and identify their needs. By understanding the customer, marketers can launch a targeted marketing campaign that is tailored for specific needs.
In this project, a targeted ad marketing campaign will be done by dividing the customers into at least 3 distinctive groups.
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler, normalize
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from tensorflow.keras.layers import Input, Add, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, AveragePooling2D, MaxPooling2D, Dropout
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.initializers import glorot_uniform
from keras.optimizers import SGD
from warnings import simplefilter
simplefilter(action='ignore', category=FutureWarning)
df = pd.read_csv('Marketing_data.csv')
pd.set_option('display.max_columns', None)
df
df.info()
df.describe()
df.columns
df.nunique()
df.duplicated().sum()
df.isna().sum()
df['MINIMUM_PAYMENTS'] = df['MINIMUM_PAYMENTS'].replace(np.nan, np.mean(df['MINIMUM_PAYMENTS']))
df['CREDIT_LIMIT'] = df['CREDIT_LIMIT'].replace(np.nan, np.mean(df['CREDIT_LIMIT']))
df.isna().sum()
df.drop(columns = ['CUST_ID'], axis = 1, inplace = True)
df
n = len(df.columns)
n
df.columns
plt.figure(figsize = (10,50))
for i in range(n):
plt.subplot(17, 1, i+1)
sns.distplot(df[df.columns[i]], kde_kws = {'color': 'g', 'lw':3, 'label':'KDE'}, hist_kws = {'color':'b'})
plt.title(df.columns[i])
plt.tight_layout()
The mean of the balance is 1500. For the Purchases Frequency it is observed that there are two distinct group of customers.Most of the users does not do oneoff purchases and purchases installment. There's only a very small number of cutomers who pay their balance in full. The credit limit average is around 4500 and most customers are around 11 years tenure.
correlation = df.corr()
f, ax = plt.subplots(figsize = (20,20))
a = sns.heatmap(correlation, annot = True, cmap = sns.cubehelix_palette(light=1, as_cmap=True))
bottom, top = a.get_ylim()
a.set_ylim(bottom + 0.5, top - 0.5)
The Purchases has strong correlation to one off purchases, installment purchases, purchase transactions, credit limit and payments. There is aslo strong positive correlation between frequency of purchases and frequency of installment purchases.
scaler = StandardScaler()
df_scaled = scaler.fit_transform(df)
df_scaled.shape
df_scaled
score_1 = []
range_values = range(1,20)
for i in range_values:
kmeans = KMeans(n_clusters = i)
kmeans.fit(df_scaled)
score_1.append(kmeans.inertia_)
plt.plot(score_1, 'bx-')
plt.title('Finding the right number of clusters')
plt.ylabel('Scores WCSS')
plt.xlabel('Clusters')
plt.show()
kmeans = KMeans(8)
kmeans.fit(df_scaled)
labels = kmeans.labels_
labels
kmeans.cluster_centers_.shape
cluster_centers = pd.DataFrame(data = kmeans.cluster_centers_, columns = [df.columns])
cluster_centers
cluster_centers = scaler.inverse_transform(cluster_centers)
cluster_centers = pd.DataFrame(data = cluster_centers, columns = [df.columns])
cluster_centers
First Customer Cluster (Transactors) : Those are customers who pay least amount of interest charges and careful with their money, Cluster with lowest balance (104 dollars), and cash advance (303 dollars)
Second customers cluster (revolvers) who use credit card as a loan (most lucrative sector): highest balance (5000) and cash advance (~5000), low purchase frequency, high cash advance frequency (0.5), high cash advance transactions (16) and low percentage of full payment (3%)
Third customer cluster (VIP/Prime): high credit limit 16K and highest percentage of full payment, target for increase credit limit and increase spending habits
Fourth customer cluster (low tenure): these are customers with low tenure (7 years), low balance
labels.shape
labels.max()
labels.min()
df
df['Clusters'] = labels
df
for i in df.columns:
plt.figure(figsize=(35,5))
for j in range(8):
plt.subplot(1,8, j+1)
cluster = df[df['Clusters'] == j]
cluster[i].hist(bins=20, color = 'mediumorchid')
plt.title('{}\n Cluster {}'.format(i,j))
pca = PCA(n_components = 2)
principal_comp = pca.fit_transform(df_scaled)
principal_comp
pca_df = pd.DataFrame(data = principal_comp, columns = ['pca1', 'pca2'])
pca_df
pca_df = pd.concat([pca_df, pd.DataFrame({'cluster':labels})], axis = 1)
pca_df
plt.figure(figsize = (10,10))
ax = sns.scatterplot(x = 'pca1', y='pca2', hue = 'cluster', data = pca_df, palette = sns.cubehelix_palette(8))
encoding_dim = 7
input_df = Input(shape=(17,))
x = Dense(encoding_dim, activation='relu')(input_df)
x = Dense(500, activation='relu', kernel_initializer = 'glorot_uniform')(x)
x = Dense(500, activation='relu', kernel_initializer = 'glorot_uniform')(x)
x = Dense(2000, activation='relu', kernel_initializer = 'glorot_uniform')(x)
encoded = Dense(10, activation='relu', kernel_initializer = 'glorot_uniform')(x)
x = Dense(2000, activation='relu', kernel_initializer = 'glorot_uniform')(encoded)
x = Dense(500, activation='relu', kernel_initializer = 'glorot_uniform')(x)
decoded = Dense(17, kernel_initializer = 'glorot_uniform')(x)
# autoencoder
autoencoder = Model(input_df, decoded)
#encoder - used for our dimention reduction
encoder = Model(input_df, encoded)
autoencoder.compile(optimizer= 'adam', loss='mean_squared_error')
df_scaled.shape
autoencoder.fit(df_scaled,df_scaled, batch_size =128, epochs = 30, verbose = 1)
autoencoder.summary()
prediction = encoder.predict(df_scaled)
prediction.shape
score_2 = []
range_values = range(1,20)
for i in range_values:
kmeans = KMeans(n_clusters = i)
kmeans.fit(prediction)
score_2.append(kmeans.inertia_)
plt.plot(score_2, 'bx-')
plt.title('Finding the right number of clusters')
plt.ylabel('Scores WCSS')
plt.xlabel('Clusters')
plt.show()
plt.plot(score_1, 'bx-', color = 'r')
plt.plot(score_2, 'bx-', color = 'g')
kmeans = KMeans(4)
kmeans.fit(prediction)
labels = kmeans.labels_
df['Clusters'] = labels
df
pca = PCA(n_components = 2)
principal_comp = pca.fit_transform(prediction)
pca_df = pd.DataFrame(data = principal_comp, columns = ['pca1', 'pca2'])
pca_df = pd.concat([pca_df, pd.DataFrame({'cluster':labels})], axis = 1)
pca_df
plt.figure(figsize = (10,10))
ax = sns.scatterplot(x = 'pca1', y='pca2', hue = 'cluster', data = pca_df, palette = sns.cubehelix_palette(4))