# ---
# 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
# ---

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
# %matplotlib inline

data=pd.read_csv('Mall_Customers.csv')
data.head()

data.info()

sns.FacetGrid(data,hue='Genre',size=5,palette='Set2').map(plt.scatter,'Annual Income (k$)','Spending Score (1-100)').add_legend()
plt.title('Annual Income and Spending Score')
plt.show()

data=data.drop(columns='CustomerID')

sns.pairplot(data,kind='scatter')

sns.countplot(data['Genre'])


def dist(a):
    sns.FacetGrid(data,height=6).map(sns.distplot,a).add_legend()
    


data.columns

dist('Age')
plt.title('Distribution of Age')
plt.show()

dist('Annual Income (k$)')
plt.title('Annual Income Distribution')
plt.show()

dist('Spending Score (1-100)')
plt.title('Distribution ')
plt.show()

from sklearn.preprocessing import LabelEncoder
le=LabelEncoder()
data['Genre']=le.fit_transform(data['Genre'])

# Checking the optimal number of Clustures.

from sklearn.cluster import KMeans

inertias = []
for i in range(1, 15):
    km = KMeans(n_clusters=i).fit(data)
    inertias.append(km.inertia_)

plt.plot(range(1, 15), inertias, 'bx-')
plt.xlabel('Values of K')
plt.ylabel('Inertia')
plt.title('The Elbow Method using Inertia')
plt.show()

km = KMeans(n_clusters=5).fit(data)
y_km = km.fit_predict(data)
n_cluster, km_count = np.unique(y_km, return_counts=True)
plt.bar(n_cluster, km_count)
plt.ylabel('No of customer')
plt.xlabel('Clustering')
plt.title('Customer segmentation by 3 groups')

# +
plt.scatter(data['Annual Income (k$)'], 
            data['Spending Score (1-100)'], 
            c=y_km, s=100)


plt.xlabel('Annual Income')
plt.ylabel('Spending Score')
plt.title('Customer segmentation into 5 clusters')

plt.show()
# -





StarcoderdataJupyterScriptsDedupFiltered