<jupyter_start><jupyter_text>**Name -** <NAME> **Roll NO -** CB.EN.U4CSE18258 **MLDM Assignment -** K Means and K Medoid Clustering<jupyter_code>pip install scikit-learn-extra<jupyter_output>Collecting scikit-learn-extra
  Downloading scikit_learn_extra-0.2.0-cp37-cp37m-manylinux2010_x86_64.whl (1.7 MB)
[K     |████████████████████████████████| 1.7 MB 4.9 MB/s 
[?25hRequirement already satisfied: numpy>=1.13.3 in /usr/local/lib/python3.7/dist-packages (from scikit-learn-extra) (1.19.5)
Collecting scikit-learn>=0.23.0
  Downloading scikit_learn-1.0-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (23.1 MB)
[K     |████████████████████████████████| 23.1 MB 1.3 MB/s 
[?25hRequirement already satisfied: scipy>=0.19.1 in /usr/local/lib/python3.7/dist-packages (from scikit-learn-extra) (1.4.1)
Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.7/dist-packages (from scikit-learn>=0.23.0->scikit-learn-extra) (1.0.1)
Collecting threadpoolctl>=2.0.0
  Downloading threadpoolctl-2.2.0-py3-none-any.whl (12 kB)
Installing collected packages: threadpoolctl, scikit-learn, scikit-learn-extra
  Attempting uninstall: scikit-learn
    Found existing installatio[...]<jupyter_text>Import Statements<jupyter_code>import pandas as pd
import numpy as np 
import matplotlib.pyplot as plt 
import seaborn as sns 
from sklearn.cluster import KMeans
from sklearn import metrics
from scipy.spatial.distance import cdist
from sklearn.cluster import KMeans
from sklearn_extra.cluster import KMedoids
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Algorithm  Importing Dataset<jupyter_code>df = pd.read_csv('coimbatore.csv')
df.dtypes<jupyter_output><empty_output><jupyter_text>Exploring Dataset<jupyter_code>df
df.head()
x = df.iloc[:, [0, 1]].values
df.columns
x
df.tail()
df.nunique()
df.isnull()
len(df.isnull())
df.describe()
df['X'].mean()
df['X'].mode()
df['X'].median()
df['Y'].median()
df['Y'].mean()
df['Y'].mode()
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 4087 entries, 0 to 4086
Data columns (total 3 columns):
 #   Column      Non-Null Count  Dtype  
---  ------      --------------  -----  
 0   X           4087 non-null   float64
 1   Y           4087 non-null   float64
 2   Unnamed: 2  3 non-null      float64
dtypes: float64(3)
memory usage: 95.9 KB<jupyter_text>Preprocessing<jupyter_code>df = df.drop(columns = 'Unnamed: 2')
df.isnull().sum()
df
type(df)<jupyter_output><empty_output><jupyter_text>Elbow Plot<jupyter_code>distortions = []
inertias = []
mapping1 = {}
mapping2 = {}

for k in range(1, 100):
    kmeansModel = KMeans(n_clusters=k,init='k-means++').fit(df)
    kmeansModel.fit(df)
 
    distortions.append(sum(np.min(cdist(df, kmeansModel.cluster_centers_,
                                        'euclidean'), axis=1)) / df.shape[0])
    inertias.append(kmeansModel.inertia_)
 
    mapping1[k] = sum(np.min(cdist(df, kmeansModel.cluster_centers_,
                                   'euclidean'), axis=1)) / df.shape[0]
    mapping2[k] = kmeansModel.inertia_
plt.figure(figsize=(20,20))
plt.plot(range(1,100), distortions, 'bx-')
plt.xlabel('Values of K')
plt.ylabel('Distortion')
plt.title('The Elbow Method using Distortion')
plt.show()<jupyter_output><empty_output><jupyter_text>KMeans<jupyter_code>type(df)
kmeans = KMeans(n_clusters=4,init='k-means++', random_state=256) 
df = df.values
y_kmeans = kmeans.fit_predict(df)<jupyter_output><empty_output><jupyter_text>Algorithm<jupyter_code>plt.figure(figsize=(20,20))

sns.scatterplot(df[y_kmeans == 2, 0], df[y_kmeans == 2, 1], color = 'cyan', label = 'Cluster 1',s=100)
sns.scatterplot(df[y_kmeans == 1, 0], df[y_kmeans == 1, 1], color = 'orange', label = 'Cluster 2',s=100)
sns.scatterplot(df[y_kmeans == 0, 0], df[y_kmeans == 0, 1], color = 'red', label = 'Cluster 3',s=100)
sns.scatterplot(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], color = 'blue',label = 'Centroids',s=200,marker=',')

plt.grid(True)
plt.title('Clusters of points')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.show()<jupyter_output>/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
/usr/local/lib/python3.7/dist-package[...]<jupyter_text>K-Medoid<jupyter_code>cobj = KMedoids(n_clusters=4).fit(df)
labels = cobj.labels_
unique_labels = set(labels)
colors = [
    plt.cm.Spectral(each) for each in np.linspace(0, 1, len(unique_labels))
]
plt.figure(figsize=(20,20))
for k, col in zip(unique_labels, colors):

    class_member_mask = labels == k

    xy = df[class_member_mask]
    plt.plot(
        xy[:, 0],
        xy[:, 1],
        "o",
        markerfacecolor=tuple(col),
        markeredgecolor="g",
        markersize=6,
    )

plt.plot(
    cobj.cluster_centers_[:, 0],
    cobj.cluster_centers_[:, 1],
    "o",
    markerfacecolor="blue",
    markeredgecolor="b",
    markersize=10,
)
plt.title("KMedoids clustering. The Medoids have been represented in blue.")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15460750853242322, 'fraction_numerical': 0.040273037542662114, 'mean_word_length': 4.566001899335233, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '615769', 'license': 'Apache-2.0', 'n_tokens_mistral': 2378, 'n_tokens_neox': 2167, 'n_words': 448, 'path': 'CB_EN_U4CSE18258_MLDM_KMeans_KMedoid.ipynb', 'repo_name': 'sudar-coder321/KMeans_vs_KMedoid', 'size': '5867'}