text_html_output_4.png text_html_output_6.png text_html_output_2.png text_html_output_5.png text_html_output_9.png text_html_output_1.png text_html_output_8.png text_html_output_3.png text_html_output_7.png
from kneed import KneeLocator

from sklearn.cluster import KMeans

from sklearn.cluster import KMeans

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.plot(range(1, 11), List)
plt.xticks(range(1, 11))
plt.xlabel('Number of Clusters', labelpad=20)
plt.ylabel('Interia')
plt.axvline(x=kl.elbow, color='b', label='axvline - full height', ls='--')
plt.show()