preloader

Clustering - KMeans Elbow method - Práctica con dataset IRIS

Índice de contenido
python
from sklearn import datasets
from sklearn.cluster import KMeans
import pandas as pd
import matplotlib.pyplot as plt
python
iris = datasets.load_iris()
python
iris.feature_names
['sepal length (cm)',
 'sepal width (cm)',
 'petal length (cm)',
 'petal width (cm)']
python
df = pd.DataFrame(iris.data, columns = iris.feature_names)
df

sepal length (cm)sepal width (cm)petal length (cm)petal width (cm)
05.13.51.40.2
14.93.01.40.2
24.73.21.30.2
34.63.11.50.2
45.03.61.40.2
...............
1456.73.05.22.3
1466.32.55.01.9
1476.53.05.22.0
1486.23.45.42.3
1495.93.05.11.8

150 rows × 4 columns

python
inertias = []
n_clusters = range(1,10)
for n in n_clusters:
    print("Evaluando n_clusters = ", n)
    knn_model = KMeans(n_clusters = n)
    knn_model.fit(df)
    inertias.append(knn_model.inertia_)
Evaluando n_clusters =  1
Evaluando n_clusters =  2
Evaluando n_clusters =  3
Evaluando n_clusters =  4
Evaluando n_clusters =  5
Evaluando n_clusters =  6
Evaluando n_clusters =  7
Evaluando n_clusters =  8
Evaluando n_clusters =  9
python
inertias
[681.3705999999996,
 152.34795176035797,
 78.851441426146,
 57.22847321428572,
 46.44618205128204,
 39.066035353535376,
 34.42019178628389,
 30.859221634503605,
 28.042265254353516]
python
plt.figure(figsize=(16,8))
plt.plot(n_clusters, inertias)
plt.xlabel("Nº de clusters")
plt.ylabel("Inercia")
plt.show()

png

python
iris.target
array([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, 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, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
python
knn_final = KMeans(n_clusters=3)
knn_final.fit(df)
KMeans(n_clusters=3)
python
df["cluster_id"] = knn_final.predict(df)
df

sepal length (cm)sepal width (cm)petal length (cm)petal width (cm)cluster_id
05.13.51.40.21
14.93.01.40.21
24.73.21.30.21
34.63.11.50.21
45.03.61.40.21
..................
1456.73.05.22.30
1466.32.55.01.92
1476.53.05.22.00
1486.23.45.42.30
1495.93.05.11.82

150 rows × 5 columns

python
df["iris_class"] = iris.target
df

sepal length (cm)sepal width (cm)petal length (cm)petal width (cm)cluster_idiris_class
05.13.51.40.210
14.93.01.40.210
24.73.21.30.210
34.63.11.50.210
45.03.61.40.210
.....................
1456.73.05.22.302
1466.32.55.01.922
1476.53.05.22.002
1486.23.45.42.302
1495.93.05.11.822

150 rows × 6 columns

python
fig, axes = plt.subplots(1, 2, figsize=(16,8))
axes[0].scatter(df["sepal length (cm)"], df["sepal width (cm)"], c = df.iris_class)
axes[0].set_title("Iris class")

axes[1].scatter(df["sepal length (cm)"], df["sepal width (cm)"], c = df.cluster_id)
axes[1].set_title("Cluster ID")
Text(0.5, 1.0, 'Cluster ID')

png

comments powered by Disqus