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

# In[1]:


import numpy as np


# In[2]:


import pandas as pd


# In[3]:


import matplotlib.pyplot as plt


# In[4]:


import seaborn as sns


# In[5]:


import datetime as dt


# In[6]:


import sklearn


# In[7]:


from sklearn.preprocessing import StandardScaler


# In[8]:


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[9]:


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


# In[10]:


unpopular.shape


# In[11]:


unpopular.info()


# In[12]:


unpopular.describe()


# In[13]:


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


# In[14]:


df_null


# In[15]:


unpopular = unpopular.dropna()


# In[16]:


unpopular.shape


# In[17]:


max_date = max(unpopular['OrderDate'])
max_date


# In[18]:


unpopular['Item No'] = unpopular['Item No'].astype(str)


# In[19]:


rfm_m = unpopular.groupby('Brand')['Item No'].count()
rfm_m = rfm_m.reset_index()
rfm_m.columns = ['Brand', 'Item']
rfm_m.head()


# In[20]:


unpopular['OrderDate'] = pd.to_datetime(unpopular['OrderDate'],format='%Y-%m-%d')


# In[21]:


max_date = max(unpopular['OrderDate'])
max_date


# In[22]:


unpopular['date'] = max_date - unpopular['OrderDate']
unpopular.head()


# In[23]:


rfm_p = unpopular.groupby('Brand')['date'].min()
rfm_p = rfm_p.reset_index()
rfm_p.head()


# In[24]:


rfm_p['date'] = rfm_p['date'].dt.days
rfm_p.head()


# In[25]:


rfm = pd.merge(rfm_p, rfm_m, on='Brand', how='inner')
rfm.head()


# In[26]:


attributes = ['Item','date']
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[27]:


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


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


# In[28]:


rfm_df = rfm[['Item', 'date']]


# In[29]:


scaler = StandardScaler()


# In[30]:


rfm_df_scaled = scaler.fit_transform(rfm_df)
rfm_df_scaled.shape


# In[31]:


rfm_df_scaled = pd.DataFrame(rfm_df_scaled)
rfm_df_scaled.columns = ['Item', 'date']
rfm_df_scaled.head()


# In[32]:


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


# In[33]:


kmeans.labels_


# In[34]:


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[35]:


plt.plot(ssd)


# In[36]:


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


# In[37]:


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


# In[38]:


kmeans.fit(rfm_df_scaled)


# In[39]:


cluster_labels =  kmeans.labels_


# In[40]:


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[41]:


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


# In[42]:


kmeans.labels_


# In[43]:


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


# In[44]:


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


# In[45]:


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


# In[46]:


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


# In[47]:


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


# In[48]:


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


# In[49]:


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


# In[50]:


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


# In[51]:


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


# In[52]:


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


# In[ ]:




