#!/usr/bin/env python
# coding: utf-8

# In[59]:


import numpy as np


# In[60]:


import pandas as pd


# In[61]:


import matplotlib.pyplot as plt


# In[62]:


import seaborn as sns
plt.style.use('seaborn')


# In[63]:


import datetime as dt


# In[64]:


import sklearn


# In[65]:


from sklearn.preprocessing import StandardScaler


# In[66]:


from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
from scipy.cluster.hierarchy import linkage
from scipy.cluster.hierarchy import dendrogram
from scipy.cluster.hierarchy import cut_tree


# In[67]:


inven = pd.read_csv("Data set .csv", sep=",", encoding="ISO-8859-1", header=0)
inven.head()


# In[68]:


inven.shape


# In[69]:


inven.info()


# In[70]:


inven.describe()


# In[71]:


df_null = round(100*(inven.isnull().sum())/len(inven), 2)


# In[72]:


df_null


# In[73]:


rfm_f = inven.groupby('Brand')['Item No'].count()
rfm_f = rfm_f.reset_index()
rfm_f.columns = ['Brand', 'Frequency']
rfm_f.head()


# In[74]:


inven['OrderAmount'].value_counts().head()


# In[75]:



fig, ax = plt.subplots(figsize=(20,8))
sns.set(font_scale=1.5)
ax = sns.countplot(x=inven['OrderAmount'], palette='spring')
ax.axhline(y=inven['OrderAmount'].value_counts().max(), linestyle='--',color='#c90404', label=f'Max OrderAmount Count ({inven.OrderAmount.value_counts().max()})')
ax.axhline(y=inven['OrderAmount'].value_counts().mean(), linestyle='--',color='#eb50db', label=f'Average OrderAmount Count ({inven.OrderAmount.value_counts().mean():.1f})')
ax.axhline(y=inven['OrderAmount'].value_counts().min(), linestyle='--',color='#046ebf', label=f'Min OrderAmount Count ({inven.OrderAmount.value_counts().min()})')
ax.legend(loc ='right')
ax.set_ylabel('No. of Items')

plt.title('Order Amount Distribution', fontsize = 20)
plt.show()


# In[76]:


rfm_f


# In[77]:


rfm_o = inven.groupby('Brand')['OrderAmount'].sum()
rfm_o = rfm_o.reset_index()
rfm_o.columns = ['Brand', 'Frequency2']
rfm_o.head()


# In[78]:


rfm = pd.merge(rfm_o, rfm_f, on='Brand', how='inner')
rfm.head()


# In[79]:


attributes = ['Frequency','Frequency2']
plt.rcParams['figure.figsize'] = [10,8]
sns.boxplot(data = rfm[attributes], orient="v", palette="Set2" ,whis=1.5,saturation=1, width=0.7)
plt.title("Outliers Variable Distribution", fontsize = 14, fontweight = 'bold')
plt.ylabel("Range", fontweight = 'bold')
plt.xlabel("Attributes", fontweight = 'bold')


# In[80]:


Q1 = rfm.Frequency2.quantile(0.05)
Q3 = rfm.Frequency2.quantile(0.95)
IQR = Q3 - Q1
rfm = rfm[(rfm.Frequency2 >= Q1 - 1.5*IQR) & (rfm.Frequency2 <= Q3 + 1.5*IQR)]


# Removing (statistical) outliers for Frequency
Q1 = rfm.Frequency.quantile(0.05)
Q3 = rfm.Frequency.quantile(0.95)
IQR = Q3 - Q1
rfm = rfm[(rfm.Frequency >= Q1 - 1.5*IQR) & (rfm.Frequency <= Q3 + 1.5*IQR)]


# In[81]:


rfm_df = rfm[['Frequency', 'Frequency2']]


# In[82]:


scaler = StandardScaler()


# In[83]:


rfm_df_scaled = scaler.fit_transform(rfm_df)
rfm_df_scaled.shape


# In[84]:


rfm_df_scaled = pd.DataFrame(rfm_df_scaled)
rfm_df_scaled.columns = ['Frequency', 'Frequency2']
rfm_df_scaled.head()


# In[85]:


kmeans = KMeans(n_clusters=4, max_iter=50)
kmeans.fit(rfm_df_scaled)


# In[86]:


kmeans.labels_


# In[87]:


ssd = []
range_n_clusters = [2, 3, 4, 5, 6, 7, 8]
for num_clusters in range_n_clusters:
    kmeans = KMeans(n_clusters=num_clusters, max_iter=50)
    kmeans.fit(rfm_df_scaled)
    
    ssd.append(kmeans.inertia_)


# In[88]:


plt.plot(ssd)


# In[89]:


range_n_clusters = [2, 3, 4, 5, 6, 7, 8]


# In[90]:


kmeans = KMeans(n_clusters=num_clusters, max_iter=50)


# In[91]:


kmeans.fit(rfm_df_scaled)


# In[92]:


cluster_labels =  kmeans.labels_


# In[93]:



    silhouette_avg = silhouette_score(rfm_df_scaled, cluster_labels)
    print("For n_clusters={0}, the silhouette score is {1}".format(num_clusters, silhouette_avg))


# In[94]:


kmeans = KMeans(n_clusters=3, max_iter=50)
kmeans.fit(rfm_df_scaled)


# In[95]:


kmeans.labels_


# In[96]:


rfm['Cluster_Id'] = kmeans.labels_
rfm.head()


# In[97]:


sns.boxplot(x='Cluster_Id', y='Frequency2', data=rfm)


# In[98]:


sns.boxplot(x='Cluster_Id', y='Frequency', data=rfm)


# In[99]:


mergings = linkage(rfm_df_scaled, method="single", metric='euclidean')
dendrogram(mergings)
plt.show()


# In[100]:


mergings = linkage(rfm_df_scaled, method="complete", metric='euclidean')
dendrogram(mergings)
plt.show()


# In[101]:


mergings = linkage(rfm_df_scaled, method="average", metric='euclidean')
dendrogram(mergings)
plt.show()


# In[108]:


cluster_labels = cut_tree(mergings, n_clusters=2).reshape(-1, )
cluster_labels


# In[109]:


rfm['Cluster_Labels'] = cluster_labels
rfm.head()


# In[110]:


sns.boxplot(x='Cluster_Labels', y='Frequency', data=rfm)


# In[111]:


sns.boxplot(x='Cluster_Labels', y='Frequency2', data=rfm)


# In[ ]:




