# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## bibliotecas

import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.cluster import KMeans
from sklearn import preprocessing
from sklearn.metrics import silhouette_score

# ## DataSet

df = pd.read_csv('Mall_Customers.csv')
# https://www.kaggle.com/vjchoudhary7/customer-segmentation-tutorial-in-python
df.head(2)

# ## Exploratory Analysis

# size of the dataset

df.shape

# descriptive analysis of variables

df.describe()

# visualizing the distribution of the variables

plt.subplot(1,3,1)
plt.hist(df['Age'])
plt.title('Age')
plt.subplot(1,3,2)
plt.hist(df['Annual Income (k$)'])
plt.title('Annual Income')
plt.subplot(1,3,3)
plt.hist(df['Spending Score (1-100)'])
plt.title('Spending Score')
plt.tight_layout()

# boxplots to visualize outliers

plt.subplot(1,3,1)
plt.boxplot(df['Age'])
plt.title('Age')
plt.subplot(1,3,2)
plt.boxplot(df['Annual Income (k$)'])
plt.title('Annual Income')
plt.subplot(1,3,3)
plt.boxplot(df['Spending Score (1-100)'])
plt.title('Spending Score')
plt.tight_layout()

# Looking for missing data

df.isnull().sum()

# Finding the mean values of annual income for each age and seeing the top 5 ages with the biggest mean income

df_age = df.groupby('Age').agg({'Annual Income (k$)': 'mean'})
df_age.sort_values('Annual Income (k$)', ascending = False).head(5)

# Checking if there is a correlation between age and income

df_age.reset_index().corr()

# Finding the mean values of spending score for each age and seeing the top 5 ages with the biggest mean score

df_age2 = df.groupby('Age').agg({'Spending Score (1-100)': 'mean'})
df_age2.sort_values('Spending Score (1-100)', ascending = False).head(5)

# Checking if there is a correlation between age and income

df_age2.reset_index().corr()

# Finding if there is a correlation between spending score and annual income

df[['Annual Income (k$)', 'Spending Score (1-100)']].corr()

# ## Clustering the customers

# Visualizating each customer graphically

fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(df['Age'], df['Annual Income (k$)'], df['Spending Score (1-100)'])
ax.set_xlabel('Age')
ax.set_ylabel('Annual Income (k$)')
ax.set_zlabel('Spending Score (1-100)')

# normalizating the variables

col = ['Age','Annual Income (k$)', 'Spending Score (1-100)']
df_std = df.copy()
df_std=pd.DataFrame(preprocessing.scale(df_std[col]))
df_std.columns = col

# Elbow method to determine the optimal number of groups

wcss = []
for i in range(1, 11):
    km = KMeans(n_clusters = i, init = 'k-means++', max_iter = 300, n_init = 10, random_state = 0)
    km.fit(df_std[col])
    wcss.append(km.inertia_)

plt.plot(range(1, 11), wcss)
plt.title('The Elbow Method', fontsize = 20)
plt.xlabel('No. of Clusters')
plt.ylabel('wcss')
plt.show()

kmeans = KMeans(n_clusters=4,init='k-means++',max_iter = 100, n_init = 25, random_state = 0).fit(df_std[col])
centroids = kmeans.cluster_centers_
df["clusters"] = kmeans.fit_predict(df_std[col])

plt.scatter(df_std['Annual Income (k$)'], df_std['Spending Score (1-100)'], c= df_std['Age'])
plt.scatter(centroids[:, 0], centroids[:, 1], c='red', s=50)

fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(df_std['Age'], df_std['Annual Income (k$)'], df_std['Spending Score (1-100)'])
ax.set_xlabel('Age')
ax.set_ylabel('Annual Income (k$)')
ax.set_zlabel('Spending Score (1-100)')
ax.scatter(centroids[:, 0], centroids[:, 1], c='red', s=50)

# Visualizating each group

clusters = df.groupby('clusters')[['Age','Annual Income (k$)','Spending Score (1-100)']].mean()
clusters.plot.bar(figsize=(10,7.5))

# #### Groups Caracterization

# * Group 0: This group represents the customers that have the highest mean annual income but a low spending score, so they have money to spend, but for some reason they are not spending in the mall, we could think about some marketing strategies focusing on this group specifically.
# * Group 1: This group represents the customers that have a high spending score when compared to its annual income, they are the youngest group too, so we can think about marketing strategies focusing on young people who are used to spend more money on the mall.
# * Group 2: This group represents the customers with the highest mean age, they have a spending score higher than the group 0, but lower than the Group 1, and they are the oldest group, so we can infer that they dont spend too much time on the mall.
# * Group 3: This group represents the highest spenders with a high annual income, so they are the most important group for the mall.

StarcoderdataJupyterScriptsDedupFiltered