Cluster Algorithm
Cluster Algorithm
In [4]: X.shape
Out[4]: (1000, 2)
In [5]: plt.scatter(X[:,0],X[:,1])
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)
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 [ ]:
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 [19]: kl=KneeLocator(range(1,11),wcss,curve='convex',direction='decreasing')
kl.elbow
Out[19]: 3
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 [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)
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
https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 8/25
5/16/23, 10:43 AM Cluster
https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 9/25
5/16/23, 10:43 AM Cluster
https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 10/25
5/16/23, 10:43 AM Cluster
https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 11/25
5/16/23, 10:43 AM Cluster
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 [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
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])
https://orange-chef-uvwtz.pwskills.app/lab/tree/work/Cluster.ipynb#K-means-Clustering-Algorithms 16/25
5/16/23, 10:43 AM Cluster
sc.dendrogram(sc.linkage(pca_scaled,method='ward'))
plt.title('dendogram')
plt.xlabel('Sample Index')
plt.ylabel('Eucledian Distance')
/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_)
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 [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
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])
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_)
In [71]: plt.scatter(X[:,0],X[:,1],c=y)
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