application_vnd.jupyter.stderr_output_1.png
from kneed import KneeLocator

from sklearn.cluster import KMeans

from sklearn.cluster import KMeans

from sklearn.metrics import silhouette_score

from sklearn.preprocessing import StandardScaler

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import seaborn as sns

df = pd.read_csv('/kaggle/input/ulabox-orders-with-categories-partials-2017/ulabox_orders_with_categories_partials_2017.csv')

df.shape

df.columns

df.isnull().sum()

df.columns = ['customer', 'order', 'total_items', 'discount_percent', 'weekday', 'hour', 'food_percent', 'fresh_percent', 'drinks_percent', 'home_percent', 'beauty_percent', 'health_percent', 'baby_percent', 'pets_percent']

df.discount_percent.mean()

df[df['total_items'] == df.total_items.max()]

len(df) - len(df.drop_duplicates())

df_customer = df.drop_duplicates('customer', keep='first')

df_customer.drop(['customer', 'order'], inplace=True, axis=1)

scaler = StandardScaler()
scaled_features = scaler.fit_transform(df_customer)

import matplotlib.pyplot as plt
df_corr = df_customer.corr()
fig, ax = plt.subplots(figsize=(16, 16))
ax = sns.heatmap(df_corr, annot=True)

dff = scaled_features
from sklearn.cluster import KMeans
wcss = []
for k in range(2, 11):
    kmeanModel = KMeans(n_clusters=k, init='k-means++')
    kmeanModel.fit(dff)
    wcss.append(kmeanModel.inertia_)

silhouette_coefficients = []
for k in range(2, 11):
    kmeans = KMeans(n_clusters=k, init='random', n_init=10, max_iter=300, random_state=42)
    kmeans.fit(dff)
    score = silhouette_score(scaled_features, kmeans.labels_)
    silhouette_coefficients.append(score)

from kneed import KneeLocator
kl = KneeLocator(x=range(2, 11), y=silhouette_coefficients, curve='convex', direction='decreasing')

kl.elbow