You are on page 1of 25

5/16/23, 10:43 AM Cluster

K means Clustering Algorithms


In [1]: import matplotlib.pyplot as plt

In [2]: from sklearn.datasets import make_blobs


import pandas as pd
import numpy as np
%matplotlib inline

In [3]: X,y = make_blobs(n_samples=1000,centers=3,n_features=2,random_state=23)

In [4]: X.shape

Out[4]: (1000, 2)

In [5]: plt.scatter(X[:,0],X[:,1])

Out[5]: <matplotlib.collections.PathCollection at 0x7fc72e5b6440>

In [6]: from sklearn.model_selection import train_test_split

In [7]: X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.33,random_state

In [8]: from sklearn.cluster import KMeans

In [9]: ## Manual process


## Elbow mathod select the K value
wcss=[]
for k in range(1,11):
kmeans=KMeans(n_clusters=k,init='k-means++')

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 1/25
5/16/23, 10:43 AM Cluster

kmeans.fit(X_train)
wcss.append(kmeans.inertia_)

/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureW
arning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set
the value of `n_init` explicitly to suppress the warning
warnings.warn(
/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureW
arning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set
the value of `n_init` explicitly to suppress the warning
warnings.warn(
/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureW
arning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set
the value of `n_init` explicitly to suppress the warning
warnings.warn(
/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureW
arning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set
the value of `n_init` explicitly to suppress the warning
warnings.warn(
/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureW
arning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set
the value of `n_init` explicitly to suppress the warning
warnings.warn(
/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureW
arning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set
the value of `n_init` explicitly to suppress the warning
warnings.warn(
/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureW
arning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set
the value of `n_init` explicitly to suppress the warning
warnings.warn(
/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureW
arning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set
the value of `n_init` explicitly to suppress the warning
warnings.warn(
/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureW
arning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set
the value of `n_init` explicitly to suppress the warning
warnings.warn(
/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureW
arning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set
the value of `n_init` explicitly to suppress the warning
warnings.warn(

In [10]: wcss

Out[10]: [34827.576825520206,
7935.4372861454185,
1319.2730531585612,
1140.4677884655127,
992.062417853196,
856.571213969391,
754.6316185628222,
678.0328625638764,
579.2311601011486,
536.0385915007653]

In [11]: plt.plot(range(1,11),wcss)
plt.xticks(range(1,11))
plt.xlabel('Number of Clusters')

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 2/25
5/16/23, 10:43 AM Cluster

plt.ylabel('WCSS')
plt.show()

In [12]: kmeans=KMeans(n_clusters=k,init='k-means++')

In [13]: x_pred=kmeans.fit_predict(X_train)

/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureW
arning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set
the value of `n_init` explicitly to suppress the warning
warnings.warn(

In [14]: plt.scatter(X_train[:,0],X_train[:,1],c=x_pred)

Out[14]: <matplotlib.collections.PathCollection at 0x7fc725595c00>

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 3/25
5/16/23, 10:43 AM Cluster

In [ ]:

In [ ]:

In [17]: ## Knee Locator


!pip install kneed

Collecting kneed
Downloading kneed-0.8.3-py3-none-any.whl (10 kB)
Requirement already satisfied: numpy>=1.14.2 in /opt/conda/lib/python3.10/site-
packages (from kneed) (1.23.5)
Requirement already satisfied: scipy>=1.0.0 in /opt/conda/lib/python3.10/site-p
ackages (from kneed) (1.9.3)
Installing collected packages: kneed
Successfully installed kneed-0.8.3

In [18]: from kneed import KneeLocator

In [19]: kl=KneeLocator(range(1,11),wcss,curve='convex',direction='decreasing')
kl.elbow

Out[19]: 3

In [20]: ## performance metrics


from sklearn.metrics import silhouette_score
silhouette_coefficients = []
for k in range(2,11):
kmeans = KMeans(n_clusters=k,init="k-means++")
kmeans.fit(X_train)
score=silhouette_score(X_train,kmeans.labels_)
silhouette_coefficients.append(score)

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 4/25
5/16/23, 10:43 AM Cluster

/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureW
arning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set
the value of `n_init` explicitly to suppress the warning
warnings.warn(
/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureW
arning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set
the value of `n_init` explicitly to suppress the warning
warnings.warn(
/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureW
arning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set
the value of `n_init` explicitly to suppress the warning
warnings.warn(
/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureW
arning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set
the value of `n_init` explicitly to suppress the warning
warnings.warn(
/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureW
arning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set
the value of `n_init` explicitly to suppress the warning
warnings.warn(
/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureW
arning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set
the value of `n_init` explicitly to suppress the warning
warnings.warn(
/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureW
arning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set
the value of `n_init` explicitly to suppress the warning
warnings.warn(
/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureW
arning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set
the value of `n_init` explicitly to suppress the warning
warnings.warn(
/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureW
arning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set
the value of `n_init` explicitly to suppress the warning
warnings.warn(

In [21]: silhouette_coefficients

Out[21]: [0.7281443868598331,
0.8071181203797672,
0.6357733426488265,
0.4814001336222496,
0.3363323500678605,
0.34729847490834725,
0.34291649535460605,
0.33941124191836647,
0.34256414842349187]

In [22]: plt.plot(range(2,11),silhouette_coefficients)
plt.xticks(range(2,11))
plt.xlabel('Number of Clusters')
plt.ylabel('Silhoutte Coeffecient')
plt.show()

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 5/25
5/16/23, 10:43 AM Cluster

Hierarichal Clustering
In [23]: from sklearn import datasets

In [24]: iris = datasets.load_iris()

In [25]: iris_data = pd.DataFrame(iris.data)

In [26]: iris_data.columns=iris.feature_names

In [27]: iris_data

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 6/25
5/16/23, 10:43 AM Cluster

Out[27]: sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)

0 5.1 3.5 1.4 0.2

1 4.9 3.0 1.4 0.2

2 4.7 3.2 1.3 0.2

3 4.6 3.1 1.5 0.2

4 5.0 3.6 1.4 0.2

... ... ... ... ...

145 6.7 3.0 5.2 2.3

146 6.3 2.5 5.0 1.9

147 6.5 3.0 5.2 2.0

148 6.2 3.4 5.4 2.3

149 5.9 3.0 5.1 1.8

150 rows × 4 columns

In [28]: from sklearn.preprocessing import StandardScaler


scaler = StandardScaler()

In [37]: X_scaled = scaler.fit_transform(iris_data)

In [39]: X_scaled

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 7/25
5/16/23, 10:43 AM Cluster

Out[39]: array([[-9.00681170e-01, 1.01900435e+00, -1.34022653e+00,


-1.31544430e+00],
[-1.14301691e+00, -1.31979479e-01, -1.34022653e+00,
-1.31544430e+00],
[-1.38535265e+00, 3.28414053e-01, -1.39706395e+00,
-1.31544430e+00],
[-1.50652052e+00, 9.82172869e-02, -1.28338910e+00,
-1.31544430e+00],
[-1.02184904e+00, 1.24920112e+00, -1.34022653e+00,
-1.31544430e+00],
[-5.37177559e-01, 1.93979142e+00, -1.16971425e+00,
-1.05217993e+00],
[-1.50652052e+00, 7.88807586e-01, -1.34022653e+00,
-1.18381211e+00],
[-1.02184904e+00, 7.88807586e-01, -1.28338910e+00,
-1.31544430e+00],
[-1.74885626e+00, -3.62176246e-01, -1.34022653e+00,
-1.31544430e+00],
[-1.14301691e+00, 9.82172869e-02, -1.28338910e+00,
-1.44707648e+00],
[-5.37177559e-01, 1.47939788e+00, -1.28338910e+00,
-1.31544430e+00],
[-1.26418478e+00, 7.88807586e-01, -1.22655167e+00,
-1.31544430e+00],
[-1.26418478e+00, -1.31979479e-01, -1.34022653e+00,
-1.44707648e+00],
[-1.87002413e+00, -1.31979479e-01, -1.51073881e+00,
-1.44707648e+00],
[-5.25060772e-02, 2.16998818e+00, -1.45390138e+00,
-1.31544430e+00],
[-1.73673948e-01, 3.09077525e+00, -1.28338910e+00,
-1.05217993e+00],
[-5.37177559e-01, 1.93979142e+00, -1.39706395e+00,
-1.05217993e+00],
[-9.00681170e-01, 1.01900435e+00, -1.34022653e+00,
-1.18381211e+00],
[-1.73673948e-01, 1.70959465e+00, -1.16971425e+00,
-1.18381211e+00],
[-9.00681170e-01, 1.70959465e+00, -1.28338910e+00,
-1.18381211e+00],
[-5.37177559e-01, 7.88807586e-01, -1.16971425e+00,
-1.31544430e+00],
[-9.00681170e-01, 1.47939788e+00, -1.28338910e+00,
-1.05217993e+00],
[-1.50652052e+00, 1.24920112e+00, -1.56757623e+00,
-1.31544430e+00],
[-9.00681170e-01, 5.58610819e-01, -1.16971425e+00,
-9.20547742e-01],
[-1.26418478e+00, 7.88807586e-01, -1.05603939e+00,
-1.31544430e+00],
[-1.02184904e+00, -1.31979479e-01, -1.22655167e+00,
-1.31544430e+00],
[-1.02184904e+00, 7.88807586e-01, -1.22655167e+00,
-1.05217993e+00],
[-7.79513300e-01, 1.01900435e+00, -1.28338910e+00,
-1.31544430e+00],
[-7.79513300e-01, 7.88807586e-01, -1.34022653e+00,
-1.31544430e+00],
[-1.38535265e+00, 3.28414053e-01, -1.22655167e+00,
-1.31544430e+00],

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 8/25
5/16/23, 10:43 AM Cluster

[-1.26418478e+00, 9.82172869e-02, -1.22655167e+00,


-1.31544430e+00],
[-5.37177559e-01, 7.88807586e-01, -1.28338910e+00,
-1.05217993e+00],
[-7.79513300e-01, 2.40018495e+00, -1.28338910e+00,
-1.44707648e+00],
[-4.16009689e-01, 2.63038172e+00, -1.34022653e+00,
-1.31544430e+00],
[-1.14301691e+00, 9.82172869e-02, -1.28338910e+00,
-1.31544430e+00],
[-1.02184904e+00, 3.28414053e-01, -1.45390138e+00,
-1.31544430e+00],
[-4.16009689e-01, 1.01900435e+00, -1.39706395e+00,
-1.31544430e+00],
[-1.14301691e+00, 1.24920112e+00, -1.34022653e+00,
-1.44707648e+00],
[-1.74885626e+00, -1.31979479e-01, -1.39706395e+00,
-1.31544430e+00],
[-9.00681170e-01, 7.88807586e-01, -1.28338910e+00,
-1.31544430e+00],
[-1.02184904e+00, 1.01900435e+00, -1.39706395e+00,
-1.18381211e+00],
[-1.62768839e+00, -1.74335684e+00, -1.39706395e+00,
-1.18381211e+00],
[-1.74885626e+00, 3.28414053e-01, -1.39706395e+00,
-1.31544430e+00],
[-1.02184904e+00, 1.01900435e+00, -1.22655167e+00,
-7.88915558e-01],
[-9.00681170e-01, 1.70959465e+00, -1.05603939e+00,
-1.05217993e+00],
[-1.26418478e+00, -1.31979479e-01, -1.34022653e+00,
-1.18381211e+00],
[-9.00681170e-01, 1.70959465e+00, -1.22655167e+00,
-1.31544430e+00],
[-1.50652052e+00, 3.28414053e-01, -1.34022653e+00,
-1.31544430e+00],
[-6.58345429e-01, 1.47939788e+00, -1.28338910e+00,
-1.31544430e+00],
[-1.02184904e+00, 5.58610819e-01, -1.34022653e+00,
-1.31544430e+00],
[ 1.40150837e+00, 3.28414053e-01, 5.35408562e-01,
2.64141916e-01],
[ 6.74501145e-01, 3.28414053e-01, 4.21733708e-01,
3.95774101e-01],
[ 1.28034050e+00, 9.82172869e-02, 6.49083415e-01,
3.95774101e-01],
[-4.16009689e-01, -1.74335684e+00, 1.37546573e-01,
1.32509732e-01],
[ 7.95669016e-01, -5.92373012e-01, 4.78571135e-01,
3.95774101e-01],
[-1.73673948e-01, -5.92373012e-01, 4.21733708e-01,
1.32509732e-01],
[ 5.53333275e-01, 5.58610819e-01, 5.35408562e-01,
5.27406285e-01],
[-1.14301691e+00, -1.51316008e+00, -2.60315415e-01,
-2.62386821e-01],
[ 9.16836886e-01, -3.62176246e-01, 4.78571135e-01,
1.32509732e-01],
[-7.79513300e-01, -8.22569778e-01, 8.07091462e-02,
2.64141916e-01],

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 9/25
5/16/23, 10:43 AM Cluster

[-1.02184904e+00, -2.43394714e+00, -1.46640561e-01,


-2.62386821e-01],
[ 6.86617933e-02, -1.31979479e-01, 2.51221427e-01,
3.95774101e-01],
[ 1.89829664e-01, -1.97355361e+00, 1.37546573e-01,
-2.62386821e-01],
[ 3.10997534e-01, -3.62176246e-01, 5.35408562e-01,
2.64141916e-01],
[-2.94841818e-01, -3.62176246e-01, -8.98031345e-02,
1.32509732e-01],
[ 1.03800476e+00, 9.82172869e-02, 3.64896281e-01,
2.64141916e-01],
[-2.94841818e-01, -1.31979479e-01, 4.21733708e-01,
3.95774101e-01],
[-5.25060772e-02, -8.22569778e-01, 1.94384000e-01,
-2.62386821e-01],
[ 4.32165405e-01, -1.97355361e+00, 4.21733708e-01,
3.95774101e-01],
[-2.94841818e-01, -1.28296331e+00, 8.07091462e-02,
-1.30754636e-01],
[ 6.86617933e-02, 3.28414053e-01, 5.92245988e-01,
7.90670654e-01],
[ 3.10997534e-01, -5.92373012e-01, 1.37546573e-01,
1.32509732e-01],
[ 5.53333275e-01, -1.28296331e+00, 6.49083415e-01,
3.95774101e-01],
[ 3.10997534e-01, -5.92373012e-01, 5.35408562e-01,
8.77547895e-04],
[ 6.74501145e-01, -3.62176246e-01, 3.08058854e-01,
1.32509732e-01],
[ 9.16836886e-01, -1.31979479e-01, 3.64896281e-01,
2.64141916e-01],
[ 1.15917263e+00, -5.92373012e-01, 5.92245988e-01,
2.64141916e-01],
[ 1.03800476e+00, -1.31979479e-01, 7.05920842e-01,
6.59038469e-01],
[ 1.89829664e-01, -3.62176246e-01, 4.21733708e-01,
3.95774101e-01],
[-1.73673948e-01, -1.05276654e+00, -1.46640561e-01,
-2.62386821e-01],
[-4.16009689e-01, -1.51316008e+00, 2.38717193e-02,
-1.30754636e-01],
[-4.16009689e-01, -1.51316008e+00, -3.29657076e-02,
-2.62386821e-01],
[-5.25060772e-02, -8.22569778e-01, 8.07091462e-02,
8.77547895e-04],
[ 1.89829664e-01, -8.22569778e-01, 7.62758269e-01,
5.27406285e-01],
[-5.37177559e-01, -1.31979479e-01, 4.21733708e-01,
3.95774101e-01],
[ 1.89829664e-01, 7.88807586e-01, 4.21733708e-01,
5.27406285e-01],
[ 1.03800476e+00, 9.82172869e-02, 5.35408562e-01,
3.95774101e-01],
[ 5.53333275e-01, -1.74335684e+00, 3.64896281e-01,
1.32509732e-01],
[-2.94841818e-01, -1.31979479e-01, 1.94384000e-01,
1.32509732e-01],
[-4.16009689e-01, -1.28296331e+00, 1.37546573e-01,
1.32509732e-01],

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 10/25
5/16/23, 10:43 AM Cluster

[-4.16009689e-01, -1.05276654e+00, 3.64896281e-01,


8.77547895e-04],
[ 3.10997534e-01, -1.31979479e-01, 4.78571135e-01,
2.64141916e-01],
[-5.25060772e-02, -1.05276654e+00, 1.37546573e-01,
8.77547895e-04],
[-1.02184904e+00, -1.74335684e+00, -2.60315415e-01,
-2.62386821e-01],
[-2.94841818e-01, -8.22569778e-01, 2.51221427e-01,
1.32509732e-01],
[-1.73673948e-01, -1.31979479e-01, 2.51221427e-01,
8.77547895e-04],
[-1.73673948e-01, -3.62176246e-01, 2.51221427e-01,
1.32509732e-01],
[ 4.32165405e-01, -3.62176246e-01, 3.08058854e-01,
1.32509732e-01],
[-9.00681170e-01, -1.28296331e+00, -4.30827696e-01,
-1.30754636e-01],
[-1.73673948e-01, -5.92373012e-01, 1.94384000e-01,
1.32509732e-01],
[ 5.53333275e-01, 5.58610819e-01, 1.27429511e+00,
1.71209594e+00],
[-5.25060772e-02, -8.22569778e-01, 7.62758269e-01,
9.22302838e-01],
[ 1.52267624e+00, -1.31979479e-01, 1.21745768e+00,
1.18556721e+00],
[ 5.53333275e-01, -3.62176246e-01, 1.04694540e+00,
7.90670654e-01],
[ 7.95669016e-01, -1.31979479e-01, 1.16062026e+00,
1.31719939e+00],
[ 2.12851559e+00, -1.31979479e-01, 1.61531967e+00,
1.18556721e+00],
[-1.14301691e+00, -1.28296331e+00, 4.21733708e-01,
6.59038469e-01],
[ 1.76501198e+00, -3.62176246e-01, 1.44480739e+00,
7.90670654e-01],
[ 1.03800476e+00, -1.28296331e+00, 1.16062026e+00,
7.90670654e-01],
[ 1.64384411e+00, 1.24920112e+00, 1.33113254e+00,
1.71209594e+00],
[ 7.95669016e-01, 3.28414053e-01, 7.62758269e-01,
1.05393502e+00],
[ 6.74501145e-01, -8.22569778e-01, 8.76433123e-01,
9.22302838e-01],
[ 1.15917263e+00, -1.31979479e-01, 9.90107977e-01,
1.18556721e+00],
[-1.73673948e-01, -1.28296331e+00, 7.05920842e-01,
1.05393502e+00],
[-5.25060772e-02, -5.92373012e-01, 7.62758269e-01,
1.58046376e+00],
[ 6.74501145e-01, 3.28414053e-01, 8.76433123e-01,
1.44883158e+00],
[ 7.95669016e-01, -1.31979479e-01, 9.90107977e-01,
7.90670654e-01],
[ 2.24968346e+00, 1.70959465e+00, 1.67215710e+00,
1.31719939e+00],
[ 2.24968346e+00, -1.05276654e+00, 1.78583195e+00,
1.44883158e+00],
[ 1.89829664e-01, -1.97355361e+00, 7.05920842e-01,
3.95774101e-01],

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 11/25
5/16/23, 10:43 AM Cluster

[ 1.28034050e+00, 3.28414053e-01, 1.10378283e+00,


1.44883158e+00],
[-2.94841818e-01, -5.92373012e-01, 6.49083415e-01,
1.05393502e+00],
[ 2.24968346e+00, -5.92373012e-01, 1.67215710e+00,
1.05393502e+00],
[ 5.53333275e-01, -8.22569778e-01, 6.49083415e-01,
7.90670654e-01],
[ 1.03800476e+00, 5.58610819e-01, 1.10378283e+00,
1.18556721e+00],
[ 1.64384411e+00, 3.28414053e-01, 1.27429511e+00,
7.90670654e-01],
[ 4.32165405e-01, -5.92373012e-01, 5.92245988e-01,
7.90670654e-01],
[ 3.10997534e-01, -1.31979479e-01, 6.49083415e-01,
7.90670654e-01],
[ 6.74501145e-01, -5.92373012e-01, 1.04694540e+00,
1.18556721e+00],
[ 1.64384411e+00, -1.31979479e-01, 1.16062026e+00,
5.27406285e-01],
[ 1.88617985e+00, -5.92373012e-01, 1.33113254e+00,
9.22302838e-01],
[ 2.49201920e+00, 1.70959465e+00, 1.50164482e+00,
1.05393502e+00],
[ 6.74501145e-01, -5.92373012e-01, 1.04694540e+00,
1.31719939e+00],
[ 5.53333275e-01, -5.92373012e-01, 7.62758269e-01,
3.95774101e-01],
[ 3.10997534e-01, -1.05276654e+00, 1.04694540e+00,
2.64141916e-01],
[ 2.24968346e+00, -1.31979479e-01, 1.33113254e+00,
1.44883158e+00],
[ 5.53333275e-01, 7.88807586e-01, 1.04694540e+00,
1.58046376e+00],
[ 6.74501145e-01, 9.82172869e-02, 9.90107977e-01,
7.90670654e-01],
[ 1.89829664e-01, -1.31979479e-01, 5.92245988e-01,
7.90670654e-01],
[ 1.28034050e+00, 9.82172869e-02, 9.33270550e-01,
1.18556721e+00],
[ 1.03800476e+00, 9.82172869e-02, 1.04694540e+00,
1.58046376e+00],
[ 1.28034050e+00, 9.82172869e-02, 7.62758269e-01,
1.44883158e+00],
[-5.25060772e-02, -8.22569778e-01, 7.62758269e-01,
9.22302838e-01],
[ 1.15917263e+00, 3.28414053e-01, 1.21745768e+00,
1.44883158e+00],
[ 1.03800476e+00, 5.58610819e-01, 1.10378283e+00,
1.71209594e+00],
[ 1.03800476e+00, -1.31979479e-01, 8.19595696e-01,
1.44883158e+00],
[ 5.53333275e-01, -1.28296331e+00, 7.05920842e-01,
9.22302838e-01],
[ 7.95669016e-01, -1.31979479e-01, 8.19595696e-01,
1.05393502e+00],
[ 4.32165405e-01, 7.88807586e-01, 9.33270550e-01,
1.44883158e+00],
[ 6.86617933e-02, -1.31979479e-01, 7.62758269e-01,
7.90670654e-01]])

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 12/25
5/16/23, 10:43 AM Cluster

In [40]: X_scaled.shape

Out[40]: (150, 4)

Apply PCA
In [41]: from sklearn.decomposition import PCA

In [42]: pca=PCA(n_components=2)

In [43]: pca

Out[43]: ▾ PCA

PCA(n_components=2)

In [44]: pca_scaled = pca.fit_transform(X_scaled)

In [45]: pca_scaled

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 13/25
5/16/23, 10:43 AM Cluster

Out[45]: array([[-2.26470281, 0.4800266 ],


[-2.08096115, -0.67413356],
[-2.36422905, -0.34190802],
[-2.29938422, -0.59739451],
[-2.38984217, 0.64683538],
[-2.07563095, 1.48917752],
[-2.44402884, 0.0476442 ],
[-2.23284716, 0.22314807],
[-2.33464048, -1.11532768],
[-2.18432817, -0.46901356],
[-2.1663101 , 1.04369065],
[-2.32613087, 0.13307834],
[-2.2184509 , -0.72867617],
[-2.6331007 , -0.96150673],
[-2.1987406 , 1.86005711],
[-2.26221453, 2.68628449],
[-2.2075877 , 1.48360936],
[-2.19034951, 0.48883832],
[-1.898572 , 1.40501879],
[-2.34336905, 1.12784938],
[-1.914323 , 0.40885571],
[-2.20701284, 0.92412143],
[-2.7743447 , 0.45834367],
[-1.81866953, 0.08555853],
[-2.22716331, 0.13725446],
[-1.95184633, -0.62561859],
[-2.05115137, 0.24216355],
[-2.16857717, 0.52714953],
[-2.13956345, 0.31321781],
[-2.26526149, -0.3377319 ],
[-2.14012214, -0.50454069],
[-1.83159477, 0.42369507],
[-2.61494794, 1.79357586],
[-2.44617739, 2.15072788],
[-2.10997488, -0.46020184],
[-2.2078089 , -0.2061074 ],
[-2.04514621, 0.66155811],
[-2.52733191, 0.59229277],
[-2.42963258, -0.90418004],
[-2.16971071, 0.26887896],
[-2.28647514, 0.44171539],
[-1.85812246, -2.33741516],
[-2.5536384 , -0.47910069],
[-1.96444768, 0.47232667],
[-2.13705901, 1.14222926],
[-2.0697443 , -0.71105273],
[-2.38473317, 1.1204297 ],
[-2.39437631, -0.38624687],
[-2.22944655, 0.99795976],
[-2.20383344, 0.00921636],
[ 1.10178118, 0.86297242],
[ 0.73133743, 0.59461473],
[ 1.24097932, 0.61629765],
[ 0.40748306, -1.75440399],
[ 1.0754747 , -0.20842105],
[ 0.38868734, -0.59328364],
[ 0.74652974, 0.77301931],
[-0.48732274, -1.85242909],
[ 0.92790164, 0.03222608],
[ 0.01142619, -1.03401828],

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 14/25
5/16/23, 10:43 AM Cluster

[-0.11019628, -2.65407282],
[ 0.44069345, -0.06329519],
[ 0.56210831, -1.76472438],
[ 0.71956189, -0.18622461],
[-0.0333547 , -0.43900321],
[ 0.87540719, 0.50906396],
[ 0.35025167, -0.19631173],
[ 0.15881005, -0.79209574],
[ 1.22509363, -1.6222438 ],
[ 0.1649179 , -1.30260923],
[ 0.73768265, 0.39657156],
[ 0.47628719, -0.41732028],
[ 1.2341781 , -0.93332573],
[ 0.6328582 , -0.41638772],
[ 0.70266118, -0.06341182],
[ 0.87427365, 0.25079339],
[ 1.25650912, -0.07725602],
[ 1.35840512, 0.33131168],
[ 0.66480037, -0.22592785],
[-0.04025861, -1.05871855],
[ 0.13079518, -1.56227183],
[ 0.02345269, -1.57247559],
[ 0.24153827, -0.77725638],
[ 1.06109461, -0.63384324],
[ 0.22397877, -0.28777351],
[ 0.42913912, 0.84558224],
[ 1.04872805, 0.5220518 ],
[ 1.04453138, -1.38298872],
[ 0.06958832, -0.21950333],
[ 0.28347724, -1.32932464],
[ 0.27907778, -1.12002852],
[ 0.62456979, 0.02492303],
[ 0.33653037, -0.98840402],
[-0.36218338, -2.01923787],
[ 0.28858624, -0.85573032],
[ 0.09136066, -0.18119213],
[ 0.22771687, -0.38492008],
[ 0.57638829, -0.1548736 ],
[-0.44766702, -1.54379203],
[ 0.25673059, -0.5988518 ],
[ 1.84456887, 0.87042131],
[ 1.15788161, -0.69886986],
[ 2.20526679, 0.56201048],
[ 1.44015066, -0.04698759],
[ 1.86781222, 0.29504482],
[ 2.75187334, 0.8004092 ],
[ 0.36701769, -1.56150289],
[ 2.30243944, 0.42006558],
[ 2.00668647, -0.71143865],
[ 2.25977735, 1.92101038],
[ 1.36417549, 0.69275645],
[ 1.60267867, -0.42170045],
[ 1.8839007 , 0.41924965],
[ 1.2601151 , -1.16226042],
[ 1.4676452 , -0.44227159],
[ 1.59007732, 0.67624481],
[ 1.47143146, 0.25562182],
[ 2.42632899, 2.55666125],
[ 3.31069558, 0.01778095],
[ 1.26376667, -1.70674538],

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 15/25
5/16/23, 10:43 AM Cluster

[ 2.0377163 , 0.91046741],
[ 0.97798073, -0.57176432],
[ 2.89765149, 0.41364106],
[ 1.33323218, -0.48181122],
[ 1.7007339 , 1.01392187],
[ 1.95432671, 1.0077776 ],
[ 1.17510363, -0.31639447],
[ 1.02095055, 0.06434603],
[ 1.78834992, -0.18736121],
[ 1.86364755, 0.56229073],
[ 2.43595373, 0.25928443],
[ 2.30492772, 2.62632347],
[ 1.86270322, -0.17854949],
[ 1.11414774, -0.29292262],
[ 1.2024733 , -0.81131527],
[ 2.79877045, 0.85680333],
[ 1.57625591, 1.06858111],
[ 1.3462921 , 0.42243061],
[ 0.92482492, 0.0172231 ],
[ 1.85204505, 0.67612817],
[ 2.01481043, 0.61388564],
[ 1.90178409, 0.68957549],
[ 1.15788161, -0.69886986],
[ 2.04055823, 0.8675206 ],
[ 1.9981471 , 1.04916875],
[ 1.87050329, 0.38696608],
[ 1.56458048, -0.89668681],
[ 1.5211705 , 0.26906914],
[ 1.37278779, 1.01125442],
[ 0.96065603, -0.02433167]])

In [46]: plt.scatter(pca_scaled[:,0],pca_scaled[:,1])

Out[46]: <matplotlib.collections.PathCollection at 0x7fc6f03f6a70>

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 16/25
5/16/23, 10:43 AM Cluster

In [49]: ## Aglomerative Clustering


## To Construst a dendogram
import scipy.cluster.hierarchy as sc
plt.figure(figsize=(20,7))
plt.title("Dendogram")

sc.dendrogram(sc.linkage(pca_scaled,method='ward'))
plt.title('dendogram')
plt.xlabel('Sample Index')
plt.ylabel('Eucledian Distance')

Out[49]: Text(0, 0.5, 'Eucledian Distance')

In [52]: from sklearn.cluster import AgglomerativeClustering


clust=AgglomerativeClustering(n_clusters=2,affinity='euclidean',linkage='ward')
clust.fit(pca_scaled)

/opt/conda/lib/python3.10/site-packages/sklearn/cluster/_agglomerative.py:983:
FutureWarning: Attribute `affinity` was deprecated in version 1.2 and will be r
emoved in 1.4. Use `metric` instead
warnings.warn(
Out[52]: ▾ AgglomerativeClustering

AgglomerativeClustering(affinity='euclidean')

In [53]: clust.labels_

Out[53]: array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

In [54]: plt.scatter(pca_scaled[:,0],pca_scaled[:,1],c=clust.labels_)

Out[54]: <matplotlib.collections.PathCollection at 0x7fc69472dfc0>

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 17/25
5/16/23, 10:43 AM Cluster

DBSCAN Clustering
In [56]: from sklearn.cluster import DBSCAN
from sklearn.datasets import make_moons

In [57]: X,y = make_moons(n_samples=250,noise=0.05)

In [58]: X

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 18/25
5/16/23, 10:43 AM Cluster

Out[58]: array([[ 1.61880645e+00, -3.39110975e-01],


[ 1.36500887e-01, 1.09023991e+00],
[ 7.91881078e-01, -5.24569212e-01],
[ 1.87363195e+00, -6.39083913e-02],
[-3.44862597e-01, 9.04535288e-01],
[ 1.18212070e+00, -4.94118220e-01],
[ 1.02934666e+00, 1.35371465e-01],
[ 4.27999785e-01, 9.17812441e-01],
[ 7.21089513e-02, -3.57122235e-02],
[ 1.03725704e+00, 4.09021267e-01],
[ 1.79824790e+00, -8.76968931e-02],
[ 5.60380342e-01, 8.59904557e-01],
[ 1.68903379e+00, -1.52762641e-01],
[ 1.02436868e+00, -4.55717553e-01],
[ 1.31770903e+00, -3.71361310e-01],
[-6.45753170e-01, 7.34038972e-01],
[ 5.70059412e-01, 7.43188577e-01],
[ 5.89653514e-01, 8.72162708e-01],
[ 8.93599138e-02, 6.56540277e-02],
[ 9.30614625e-01, 3.72138458e-01],
[ 1.22544643e+00, -4.53628330e-01],
[ 1.03566713e+00, -5.26091091e-01],
[ 4.25641751e-02, 4.89167547e-01],
[ 8.98507167e-01, 3.87445169e-01],
[ 3.31450777e-01, 8.98097102e-01],
[-9.00997421e-01, 4.98142674e-01],
[ 7.34000823e-01, 6.96821630e-01],
[ 1.30756919e+00, -4.34060167e-01],
[ 8.09850082e-02, 3.50000138e-01],
[ 2.63977865e-01, -2.53593848e-01],
[-7.66752014e-01, 4.54507888e-01],
[ 4.98541269e-01, -3.54338253e-01],
[-9.47039762e-01, -9.60231494e-04],
[ 5.18906299e-01, -4.25212824e-01],
[ 1.54475092e-01, 1.19736695e-01],
[ 1.50365718e+00, -3.45807989e-01],
[ 1.93852254e+00, 3.24329037e-02],
[ 1.08569334e+00, 1.88437978e-01],
[-7.26205451e-01, 6.89285239e-01],
[ 1.38970677e+00, -3.91161424e-01],
[ 1.70113705e+00, -1.61691413e-01],
[-7.34146778e-01, 4.51162592e-01],
[ 2.54140887e-01, -1.40251454e-01],
[ 4.35554694e-02, 4.72020517e-01],
[ 1.91514368e+00, 3.85614405e-02],
[-9.32935170e-01, 3.99155053e-01],
[ 4.05912570e-01, 8.80950420e-01],
[ 2.12418261e-01, -7.22710854e-02],
[ 7.71779455e-01, 6.08277273e-01],
[ 8.37236121e-01, 4.88081498e-01],
[-6.25975833e-01, 7.79588706e-01],
[-1.14101952e-01, 9.72709189e-01],
[ 1.77555092e-01, -7.56140733e-02],
[ 8.85875176e-01, -5.30724076e-01],
[-9.09735468e-01, 8.14916273e-02],
[ 8.92710137e-01, 2.22680763e-01],
[-1.66393445e-01, 9.75414881e-01],
[ 1.89475253e-01, 9.64163284e-01],
[ 1.79033053e+00, -1.95020957e-01],
[ 1.53829032e+00, -3.45554101e-01],

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 19/25
5/16/23, 10:43 AM Cluster

[ 7.24266554e-01, 6.92680347e-01],
[ 1.21634731e-01, 9.30404890e-02],
[ 4.21262139e-02, 2.37564882e-01],
[ 1.15780297e-01, -7.30918734e-02],
[ 1.33737702e-01, 2.11449260e-01],
[ 1.72135046e+00, -2.24110683e-01],
[ 3.25772468e-01, -2.66588270e-01],
[ 6.37575131e-01, 7.56795127e-01],
[ 7.44322761e-02, 2.15498067e-02],
[-5.70050817e-01, 8.84004839e-01],
[ 1.56417906e+00, -1.83720064e-01],
[-3.66987936e-02, 2.63033894e-01],
[-7.30172521e-01, 6.42699522e-01],
[-5.82044132e-03, 1.04880348e+00],
[ 9.63917703e-02, 3.55587779e-01],
[ 1.03970524e+00, -4.45199434e-01],
[ 8.57299223e-01, 4.68914704e-01],
[ 1.89830550e+00, 1.78574447e-01],
[ 1.90990954e+00, 3.33948075e-01],
[ 2.07939392e-02, 1.03883592e+00],
[ 1.07427925e+00, 3.58030546e-02],
[ 7.71732566e-01, 5.32829094e-01],
[ 1.55951371e-01, -4.52664262e-02],
[ 2.02105751e+00, 4.96574463e-01],
[-3.61394952e-01, 9.49655790e-01],
[-1.03745111e+00, 1.24971679e-01],
[-3.38079824e-01, 1.04116199e+00],
[ 7.31662771e-01, -4.50390790e-01],
[-7.25186655e-01, 7.03045601e-01],
[ 2.01539932e+00, 5.34134860e-01],
[ 6.55628517e-02, 9.50969914e-01],
[ 4.29107608e-02, 9.90747130e-01],
[ 1.69441307e+00, -1.01198346e-01],
[-3.75970995e-01, 9.94521191e-01],
[ 1.86528532e+00, 8.05528698e-02],
[ 1.24272497e+00, -3.69806007e-01],
[ 4.97763351e-01, 7.90458310e-01],
[-7.94048001e-01, 6.73050876e-01],
[-8.46004127e-01, 5.11530725e-01],
[-7.87566819e-01, 5.84815403e-01],
[ 7.02602129e-01, 8.12957706e-01],
[ 4.60165251e-01, -2.98693295e-01],
[ 1.84846390e+00, 7.82512128e-02],
[-6.81902245e-01, 6.98815867e-01],
[ 1.77366957e+00, -1.02170234e-01],
[ 2.60579738e-01, 9.50648345e-01],
[ 1.08324298e+00, -5.17410328e-01],
[ 9.79232718e-01, 2.66430582e-01],
[ 1.81692155e-01, -1.27199912e-01],
[ 4.05571578e-01, -3.17528715e-01],
[-1.04131133e+00, 1.04426593e-01],
[-1.85045071e-01, 9.99071992e-01],
[ 7.54964645e-01, 6.78322469e-01],
[ 2.08544163e+00, 3.22330015e-01],
[ 5.90797596e-01, 6.56676610e-01],
[-8.65983041e-01, 3.38849924e-01],
[-5.87028027e-01, 7.72251813e-01],
[ 1.96226332e+00, 2.94941187e-01],
[ 7.63591394e-01, 6.83861616e-01],
[ 1.95103027e+00, 4.12982172e-01],

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 20/25
5/16/23, 10:43 AM Cluster

[ 1.95279010e+00, 2.55408000e-01],
[ 1.84628507e+00, 9.88600979e-03],
[-9.76941661e-01, 5.74579292e-02],
[-3.34347825e-01, 9.83835016e-01],
[-9.92740762e-01, -3.88102835e-02],
[ 5.87099587e-01, -4.13193007e-01],
[ 1.81626771e+00, -1.85518512e-01],
[ 1.16771704e-01, 9.37847231e-01],
[-9.43804730e-01, 5.24177074e-01],
[ 7.62386187e-01, 6.05567678e-01],
[-5.65302880e-01, 9.16277118e-01],
[ 2.43186511e-01, -2.27119631e-01],
[ 3.43639154e-01, 8.31575776e-01],
[-5.63384675e-01, 7.34948317e-01],
[ 4.52790582e-01, -3.85960503e-01],
[ 1.95730706e-01, 8.68016172e-01],
[ 2.08497910e+00, 1.61055904e-01],
[ 1.53168388e-01, 9.85313101e-01],
[ 9.58773575e-01, 2.69961253e-01],
[-1.20856947e-01, 1.02337678e+00],
[ 3.95304751e-01, 8.62810609e-01],
[-6.82279662e-01, 8.02497243e-01],
[-5.00504641e-01, 8.26181152e-01],
[ 6.10153776e-01, -4.38942691e-01],
[ 1.10302938e+00, -5.08340947e-01],
[ 7.87734822e-01, 6.03430884e-01],
[-1.05244131e+00, 2.05281095e-01],
[ 2.06188358e+00, 5.50006335e-01],
[-2.18878919e-01, 9.93832613e-01],
[ 6.68947302e-01, -5.18707159e-01],
[ 1.84527166e+00, 4.45748916e-02],
[ 1.44853174e+00, -3.93052568e-01],
[ 9.96484921e-01, 7.19014134e-03],
[-9.39056645e-01, 1.61486268e-01],
[-9.96757063e-01, 2.19053423e-01],
[ 8.97551063e-01, 2.55538904e-01],
[-2.50454718e-01, 9.47546646e-01],
[ 9.06341969e-01, 3.37178865e-01],
[ 1.84796926e+00, 1.17738793e-01],
[ 1.05666819e+00, 1.30051496e-01],
[ 1.97331602e+00, 2.42658371e-01],
[ 1.32957738e+00, -4.71839925e-01],
[ 1.82940130e+00, -8.31417492e-02],
[-9.49669297e-01, 3.11594908e-01],
[ 1.54636964e+00, -3.41135556e-01],
[ 8.28491859e-03, 1.68508959e-01],
[ 9.32169301e-01, 2.54945789e-01],
[ 2.00952690e+00, 2.14880666e-01],
[ 4.93195416e-02, 4.56160288e-01],
[ 1.05988833e+00, -5.08264925e-01],
[-9.49342151e-02, 9.45309869e-01],
[ 2.06683683e+00, 3.52005289e-01],
[ 1.31585748e+00, -4.08230457e-01],
[ 5.69175627e-02, 3.76259084e-01],
[ 7.18320682e-01, -5.89027255e-01],
[ 9.42791083e-01, -5.29447338e-01],
[ 1.21130785e+00, -4.90887778e-01],
[ 1.45424281e+00, -4.17026452e-01],
[ 1.10218356e-03, 5.19870076e-01],
[ 5.54775534e-01, 7.63106492e-01],

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 21/25
5/16/23, 10:43 AM Cluster

[ 8.46637340e-01, -4.89618593e-01],
[-7.26363719e-01, 6.48739519e-01],
[ 8.76494009e-01, 4.27026345e-01],
[ 1.63963422e+00, -3.16621992e-01],
[ 1.53605855e+00, -2.99218497e-01],
[-1.02055345e+00, 2.73209266e-01],
[-8.54127196e-01, 2.86460949e-01],
[ 4.61670676e-01, -3.49515623e-01],
[-4.43268251e-01, 9.25211676e-01],
[-2.83223607e-01, 9.96638601e-01],
[ 7.50521753e-01, 7.75529459e-01],
[ 1.58538067e+00, -3.60697388e-01],
[ 8.19368951e-01, 5.26441691e-01],
[-1.78268743e-01, 9.48529958e-01],
[ 6.02116378e-01, -5.39192976e-01],
[ 5.03041540e-01, 7.74439578e-01],
[ 3.43850917e-01, -3.41810712e-01],
[-5.18853924e-01, 9.60104045e-01],
[ 5.70388978e-02, 1.29001724e-01],
[ 4.72846255e-01, 8.86440606e-01],
[ 1.82288310e+00, 9.13526948e-02],
[-9.53547555e-01, 4.14187304e-01],
[ 2.48229936e-01, -1.22469501e-01],
[ 3.72115114e-01, -2.61889017e-01],
[-5.73926335e-01, 9.47134195e-01],
[ 3.71174090e-01, 9.79294379e-01],
[ 1.39532794e+00, -4.24969666e-01],
[ 1.34892611e+00, -4.34726587e-01],
[ 9.46140374e-01, 4.26270627e-01],
[ 9.51417867e-01, -5.34783107e-01],
[-7.90805780e-01, 5.88305094e-01],
[ 5.29866334e-01, -3.04457484e-01],
[ 7.22169416e-01, 6.32541551e-01],
[ 1.82897632e+00, -4.97598399e-02],
[-6.35639005e-01, 7.80221474e-01],
[ 1.06706901e+00, -3.78636333e-02],
[-7.11004048e-04, 3.45230398e-01],
[ 4.84878695e-02, 4.81952418e-01],
[ 3.99153199e-02, 6.03088331e-02],
[ 1.37836783e-01, 1.01676381e+00],
[ 6.77536110e-01, -4.38316892e-01],
[ 1.49528739e-01, 1.96468968e-01],
[ 2.77771682e-01, -1.53816591e-01],
[ 7.90590254e-01, -4.49010539e-01],
[ 2.58899557e-01, 9.42329742e-01],
[ 1.82821313e+00, -1.15637755e-01],
[ 9.46310495e-01, 7.21416747e-02],
[ 8.16486578e-01, -4.83215659e-01],
[ 1.01444643e+00, -4.46330295e-01],
[-8.73275805e-01, 3.78437856e-01],
[ 3.42056562e-01, -2.24876593e-01],
[-9.05153181e-01, 2.66636532e-01],
[ 1.33975377e+00, -4.77204476e-01],
[ 4.53781497e-01, 1.01164676e+00],
[-1.47690036e-01, 9.79544812e-01],
[ 7.89843706e-01, -4.74145330e-01],
[ 3.66524915e-03, 3.89887811e-01],
[-9.48023037e-01, 2.41923213e-01],
[ 9.53618519e-01, -4.98718209e-01],
[-6.20369013e-01, 7.97646504e-01],

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 22/25
5/16/23, 10:43 AM Cluster

[ 1.95418003e+00, 3.52615752e-01],
[ 6.32652502e-01, -4.54536075e-01],
[ 9.73259781e-01, 9.79232981e-02],
[ 9.58438846e-01, 4.86456857e-01],
[ 6.64592287e-01, -3.85480834e-01],
[ 5.00080312e-01, 9.49523277e-01],
[-1.02015176e+00, 2.83512601e-01],
[-8.14134296e-01, 4.85407213e-01],
[ 1.51581144e-01, 1.04592925e+00],
[ 5.19236788e-01, -3.90957687e-01]])

In [60]: plt.scatter(X[:,0],X[:,1])

Out[60]: <matplotlib.collections.PathCollection at 0x7fc6c05a3580>

In [61]: from sklearn.preprocessing import StandardScaler


scaler= StandardScaler()

In [62]: x_scaled = scaler.fit_transform(X)

In [63]: ## DBSCAN Algo

In [64]: dbs = DBSCAN(eps=0.5)

In [66]: dbs.fit(x_scaled)

Out[66]: ▾ DBSCAN

DBSCAN()

In [68]: dbs.labels_

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 23/25
5/16/23, 10:43 AM Cluster

Out[68]: array([0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0,
0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0,
0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0,
1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0,
1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0,
1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1,
1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1,
0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1,
0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0,
1, 1, 0, 1, 1, 1, 1, 0])

In [69]: plt.scatter(X[:,0],X[:,1],c=dbs.labels_)

Out[69]: <matplotlib.collections.PathCollection at 0x7fc694143250>

In [71]: plt.scatter(X[:,0],X[:,1],c=y)

Out[71]: <matplotlib.collections.PathCollection at 0x7fc6483f02e0>

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 24/25
5/16/23, 10:43 AM Cluster

In [ ]:

https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 25/25

You might also like