Logo elodees  elodees

A caring AI for a better world













Only alphabetic characters accented or not as well as the space are accepted

Logo IA




K-means clustering





No account yet ?

Sign up to access all content




Tested in Anaconda and Python 3.7

import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
import numpy as np
from sklearn.cluster import KMeans
 
from sklearn.datasets import make_blobs
 
X, y_true = make_blobs(n_samples = 500, centers = 4,
            cluster_std = 0.40, random_state = 0)
 
plt.scatter(X[:, 0], X[:, 1], s = 50);
plt.show()
 
kmeans = KMeans(n_clusters = 4)
 
kmeans.fit(X)
y_kmeans = kmeans.predict(X)
plt.scatter(X[:, 0], X[:, 1], c = y_kmeans, s = 50, cmap = 'viridis')
 
centers = kmeans.cluster_centers_
 
plt.scatter(centers[:, 0], centers[:, 1], c = 'black', s = 200, alpha = 0.5);
plt.show()
 


Source : https://tutoriels.edu.lat/pub/artificial-intelligence-with-python/artificial-intelligence-with-python-unsupervised-learning-clustering/ai-avec-python-apprentissage-non-supervise-clustering



Free image provided by pexel.com
Free image provided by pexel.com








Iris.txt



"Sepal Length" "Sepal Width" "Petal Length" "Petal Width" "Species"
5.1 3.5 1.4 0.2 setosa
4.9 3 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa
4.6 3.4 1.4 0.3 setosa
5 3.4 1.5 0.2 setosa
4.4 2.9 1.4 0.2 setosa
4.9 3.1 1.5 0.1 setosa
5.4 3.7 1.5 0.2 setosa
4.8 3.4 1.6 0.2 setosa
4.8 3 1.4 0.1 setosa
4.3 3 1.1 0.1 setosa
5.8 4 1.2 0.2 setosa
5.7 4.4 1.5 0.4 setosa
5.4 3.9 1.3 0.4 setosa
5.1 3.5 1.4 0.3 setosa
5.7 3.8 1.7 0.3 setosa
5.1 3.8 1.5 0.3 setosa
5.4 3.4 1.7 0.2 setosa
5.1 3.7 1.5 0.4 setosa
4.6 3.6 1 0.2 setosa
5.1 3.3 1.7 0.5 setosa
4.8 3.4 1.9 0.2 setosa
5 3 1.6 0.2 setosa
5 3.4 1.6 0.4 setosa
5.2 3.5 1.5 0.2 setosa
5.2 3.4 1.4 0.2 setosa
4.7 3.2 1.6 0.2 setosa
4.8 3.1 1.6 0.2 setosa
5.4 3.4 1.5 0.4
...



Tested in Anaconda and Python 3.7

# -*- coding: utf-8 -*-
"""
Created on Wed Jun 22 12:21:06 2022
 
@author: https://github.com/fxjollois/cours-2021-2022/blob/main/insa-ms-esd--ml/kmeans-python.ipynb
"""
#Librairies utilisées
import pandas
import numpy
import matplotlib.pyplot as plt
import seaborn
seaborn.set_style("white")
 
from sklearn.cluster import KMeans
from sklearn.preprocessing import scale
 
#Données utilisées
iris = pandas.read_table("https://fxjollois.github.io/donnees/Iris.txt", sep = "\t")
iris.head()
print(iris)
 
iris2 = iris.drop("Species", axis = 1)
iris2.head()
print(iris2)
 
#Réalisation de la CAH
kmeans = KMeans(n_clusters = 3)
kmeans.fit(scale(iris2))
print(kmeans)
 
#Informations sur la partition
pandas.Series(kmeans.labels_).value_counts()
print(pandas.Series(kmeans.labels_).value_counts())
 
#Centre des classes
kmeans.cluster_centers_
print(kmeans.cluster_centers_)
 
iris2.assign(classe = kmeans.labels_).groupby("classe").mean()
print(iris2.assign(classe = kmeans.labels_).groupby("classe").mean())
 
#Choix du nombre de classes
inertia = []
for k in range(1, 11):
    kmeans = KMeans(n_clusters = k, init = "random", n_init = 20).fit(scale(iris2))
    inertia = inertia + [kmeans.inertia_]
inertia = pandas.DataFrame({"k": range(1, 11), "inertia": inertia})
seaborn.lineplot(data = inertia, x = "k", y = "inertia")
plt.scatter(2, inertia.query('k == 2')["inertia"], c = "red")
plt.scatter(3, inertia.query('k == 3')["inertia"], c = "red")
plt.show()
 


k-means - Mastère ESD - Introduction au Machine Learning - GitHub



Sepal Length Sepal Width Petal Length Petal Width Species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
.. ... ... ... ... ...
145 6.7 3.0 5.2 2.3 virginica
146 6.3 2.5 5.0 1.9 virginica
147 6.5 3.0 5.2 2.0 virginica
148 6.2 3.4 5.4 2.3 virginica
149 5.9 3.0 5.1 1.8 virginica

[150 rows x 5 columns]
Sepal Length Sepal Width Petal Length Petal Width
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 x 4 columns]
KMeans(n_clusters=3)
2 53
1 50
0 47
dtype: int64
[[ 1.13597027 0.08842168 0.99615451 1.01752612]
[-1.01457897 0.85326268 -1.30498732 -1.25489349]
[-0.05021989 -0.88337647 0.34773781 0.2815273 ]]
Sepal Length Sepal Width Petal Length Petal Width
classe
0 6.780851 3.095745 5.510638 1.972340
1 5.006000 3.428000 1.462000 0.246000
2 5.801887 2.673585 4.369811 1.413208



Free image provided by pexel.com


Comparison of K-Means and MiniBatchKMeans clustering algorithms



Tested in Anaconda and Python 3.7

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
from sklearn.cluster import MiniBatchKMeans, KMeans
 
data = pd.read_csv("housing.csv")
 
data = data.loc[:, ["median_income", "latitude", "longitude"]]
 
kmeans = MiniBatchKMeans(n_clusters=6, random_state=0, batch_size=6)
data["Cluster"] = kmeans.fit_predict(data)
data["Cluster"] = data["Cluster"].astype("int")
print(data.head())
 
plt.style.use('seaborn-whitegrid')
plt.rc("figure", autolayout=True)
plt.rc("axes", labelweight='bold', labelsize='large', titleweight='bold', titlesize=14, titlepad=10)
sns.relplot(x='longitude', y='latitude', hue='Cluster', data=data, height=6)
plt.title('MiniBatchKMeans')
plt.show()
 
kmeans = KMeans(n_clusters=6, random_state=0)
data["Cluster"] = kmeans.fit_predict(data)
data["Cluster"] = data["Cluster"].astype("int")
print(data.head())
 
plt.style.use('seaborn-whitegrid')
plt.rc("figure", autolayout=True)
plt.rc("axes", labelweight='bold', labelsize='large', titleweight='bold', titlesize=14, titlepad=10)
sns.relplot(x='longitude', y='latitude', hue='Cluster', data=data, height=6)
plt.title('K-Means')
plt.show()
 


Source : https://thecleverprogrammer.com/2021/09/10/mini-batch-k-means-clustering-in-machine-learning/
housing.csv : https://raw.githubusercontent.com/ageron/handson-ml/master/datasets/housing/housing.csv



Free image provided by pexel.com
Free image provided by pexel.com




Mini-batch K-means Clustering


Clustering

Data engineering


Deep learning

Machine learning










Welcome, my name is Eric Soupet and I am the administrator of the site elodees.com. elodees.com is a state of the art of Artificial Intelligence and aims to be collaborative, you can now offer content such as articles, events, tutorials, ... so don't hesitate !

Platform images credit : Pixabay - Pixabay License | Pexels - Pexels License