text_html_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.metrics import silhouette_score

from sklearn.preprocessing import StandardScaler

from sklearn.preprocessing import StandardScaler

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

import warnings

import warnings
warnings.simplefilter('ignore')
plt.style.use('seaborn')

Data = pd.read_csv('/kaggle/input/country-dataset/Country_Dataset.csv')
df = pd.DataFrame(Data)
df

# Noise detection
df1 = pd.DataFrame(Data, columns = ['child_mort','exports','health','imports','income','inflation','life_expec','total_fer','gdpp'])
columns = ['child_mort','exports','health','imports','income','inflation','life_expec','total_fer','gdpp']
plt.figure(figsize=(15,20))
for i, col in enumerate (df1.columns):
    ax= plt.subplot(5,2,i+1)
    sns.boxplot(x= columns[i], data=df1, color='magenta' )
    plt.title(df1.columns[i])
    plt.tight_layout()
plt.show()

df1 = pd.DataFrame(Data, columns=['child_mort', 'exports', 'health', 'imports', 'income', 'inflation', 'life_expec', 'total_fer', 'gdpp'])
i = 1
for col in df1.columns:
    i = i + 1

plt.tight_layout()

df1 = Data.drop(['Country'], axis=1)

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaled_features = scaler.fit_transform(df1)

from sklearn.cluster import KMeans
List = []
for k in range(1, 11):
    kmeans = KMeans(n_clusters=k, init='random', max_iter=300, random_state=1, n_init=10)
    kmeans.fit(scaled_features)
    List.append(kmeans.inertia_)
from kneed import KneeLocator
kl = KneeLocator(range(1, 11), List, curve='convex', direction='decreasing')
kl.elbow
plt.style.use('fivethirtyeight')
plt.xticks(range(1, 11))

from sklearn.metrics import silhouette_score
silhouette_coefficients = []
for k in range(2, 11):
    kmeans = KMeans(n_clusters=k, init='random', random_state=1)
    kmeans.fit(scaled_features)
    score = silhouette_score(scaled_features, kmeans.labels_)
    silhouette_coefficients.append(score)
plt.style.use('fivethirtyeight')
plt.plot(range(2, 11), silhouette_coefficients)
plt.xticks(range(2, 11))
plt.xlabel('Number of Clusters')
plt.ylabel('silhouette coefficients')
plt.show()
print('max silhouette score:', max(silhouette_coefficients))