SlideShare a Scribd company logo
Prepared by Volkan OBAN
k-means Clustering in Python.
scikit-learn
Machine Learning in Python
from sklearn.cluster import KMeans
ref: http://scikit-learn.org/stable/auto_examples/cluster/plot_cluster_iris.html
Code:
print(__doc__)
# Code source: Gaƫl Varoquaux
# Modified for documentation by Jaques Grobler
# License: BSD 3 clause
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.cluster import KMeans
from sklearn import datasets
np.random.seed(5)
centers = [[1, 1], [-1, -1], [1, -1]]
iris = datasets.load_iris()
X = iris.data
y = iris.target
estimators = {'k_means_iris_3': KMeans(n_clusters=3),
'k_means_iris_8': KMeans(n_clusters=8),
'k_means_iris_bad_init': KMeans(n_clusters=3, n_init=1,
init='random')}
fignum = 1
for name, est in estimators.items():
fig = plt.figure(fignum, figsize=(4, 3))
plt.clf()
ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)
plt.cla()
est.fit(X)
labels = est.labels_
ax.scatter(X[:, 3], X[:, 0], X[:, 2], c=labels.astype(np.float))
ax.w_xaxis.set_ticklabels([])
ax.w_yaxis.set_ticklabels([])
ax.w_zaxis.set_ticklabels([])
ax.set_xlabel('Petal width')
ax.set_ylabel('Sepal length')
ax.set_zlabel('Petal length')
fignum = fignum + 1
# Plot the ground truth
fig = plt.figure(fignum, figsize=(4, 3))
plt.clf()
ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)
plt.cla()
for name, label in [('Setosa', 0),
('Versicolour', 1),
('Virginica', 2)]:
ax.text3D(X[y == label, 3].mean(),
X[y == label, 0].mean() + 1.5,
X[y == label, 2].mean(), name,
horizontalalignment='center',
bbox=dict(alpha=.5, edgecolor='w', facecolor='w'))
# Reorder the labels to have colors matching the cluster results
y = np.choose(y, [1, 2, 0]).astype(np.float)
ax.scatter(X[:, 3], X[:, 0], X[:, 2], c=y)
ax.w_xaxis.set_ticklabels([])
ax.w_yaxis.set_ticklabels([])
ax.w_zaxis.set_ticklabels([])
ax.set_xlabel('Petal width')
ax.set_ylabel('Sepal length')
ax.set_zlabel('Petal length')
plt.show()
k-means Clustering in Python
k-means Clustering in Python

More Related Content

What's hot (20)

PPTX
Intoduction to dynamic memory allocation
Utsav276
Ā 
PPT
NS2: Binding C++ and OTcl variables
Teerawat Issariyakul
Ā 
PPTX
Optimizing Tcl Bytecode
Donal Fellows
Ā 
PPT
Dynamic memory allocation
Naveen Gupta
Ā 
PDF
Performance Profiling in Rust
InfluxData
Ā 
PPTX
Beyond Lists - Functional Kats Conf Dublin 2015
Phillip Trelford
Ā 
PPTX
TensorFlow Studying Part II for GPU
Te-Yen Liu
Ā 
PDF
Powered by Python - PyCon Germany 2016
Steffen Wenz
Ā 
PPTX
Tensorflow internal
Hyunghun Cho
Ā 
PDF
PyCon KR 2019 sprint - RustPython by example
YunWon Jeong
Ā 
PDF
Big Data Day LA 2015 - Large Scale Distinct Count -- The HyperLogLog algorith...
Data Con LA
Ā 
PPS
Making an Object System with Tcl 8.5
Donal Fellows
Ā 
PDF
Monitoring Your ISP Using InfluxDB Cloud and Raspberry Pi
InfluxData
Ā 
PPT
Dynamic Memory Allocation
vaani pathak
Ā 
PDF
Spark Meetup TensorFrames
Jen Aman
Ā 
PDF
HyperLogLog in Hive - How to count sheep efficiently?
bzamecnik
Ā 
PDF
Optimizing the Grafana Platform for Flux
InfluxData
Ā 
DOC
Devry gsp 215 week 6 i lab virtual memory new
williamethan912
Ā 
PPTX
TensorFlow Study Part I
Te-Yen Liu
Ā 
PDF
C c++-meetup-1nov2017-autofdo
Kim Phillips
Ā 
Intoduction to dynamic memory allocation
Utsav276
Ā 
NS2: Binding C++ and OTcl variables
Teerawat Issariyakul
Ā 
Optimizing Tcl Bytecode
Donal Fellows
Ā 
Dynamic memory allocation
Naveen Gupta
Ā 
Performance Profiling in Rust
InfluxData
Ā 
Beyond Lists - Functional Kats Conf Dublin 2015
Phillip Trelford
Ā 
TensorFlow Studying Part II for GPU
Te-Yen Liu
Ā 
Powered by Python - PyCon Germany 2016
Steffen Wenz
Ā 
Tensorflow internal
Hyunghun Cho
Ā 
PyCon KR 2019 sprint - RustPython by example
YunWon Jeong
Ā 
Big Data Day LA 2015 - Large Scale Distinct Count -- The HyperLogLog algorith...
Data Con LA
Ā 
Making an Object System with Tcl 8.5
Donal Fellows
Ā 
Monitoring Your ISP Using InfluxDB Cloud and Raspberry Pi
InfluxData
Ā 
Dynamic Memory Allocation
vaani pathak
Ā 
Spark Meetup TensorFrames
Jen Aman
Ā 
HyperLogLog in Hive - How to count sheep efficiently?
bzamecnik
Ā 
Optimizing the Grafana Platform for Flux
InfluxData
Ā 
Devry gsp 215 week 6 i lab virtual memory new
williamethan912
Ā 
TensorFlow Study Part I
Te-Yen Liu
Ā 
C c++-meetup-1nov2017-autofdo
Kim Phillips
Ā 

Similar to k-means Clustering in Python (20)

PPTX
K-means Clustering with Scikit-Learn
Sarah Guido
Ā 
PPTX
Clustering: A Scikit Learn Tutorial
Damian R. Mingle, MBA
Ā 
PDF
RDataMining slides-clustering-with-r
Yanchang Zhao
Ā 
PDF
Pyclustering tutorial - K-means
Andrei Novikov
Ā 
PPTX
K Means Clustering in ML.pptx
Ramakrishna Reddy Bijjam
Ā 
PDF
maXbox starter68 machine learning VI
Max Kleiner
Ā 
PDF
5. Types of Clustering Algorithms in ML.pdf
Jyoti Yadav
Ā 
PDF
Data Clustering with R
Yanchang Zhao
Ā 
PPTX
Classification Aalgorithms KNN and Protype-based classifiers.pptx
DrMTayyabChaudhry1
Ā 
PPTX
Unsupervised Learning: Clustering
Experfy
Ā 
PPTX
Dimensionality Reduction in machinelearning.pptx
chaitudec2005
Ā 
PPTX
K-Means Clustering Algorithm.pptx
JebaRaj26
Ā 
PDF
Best data science training, best data science training institute in Chennai
hrhrenurenu
Ā 
PDF
business analytics course in delhi
devipatnala1
Ā 
PDF
data science training
devipatnala1
Ā 
PDF
Data science training
prathyusha1234
Ā 
PDF
data science institute in bangalore
devipatnala1
Ā 
PDF
Best data science training, best data science training institute in hyderabad.
hrhrenurenu
Ā 
PDF
Data science certification
prathyusha1234
Ā 
PDF
Data scientist course in hyderabad
prathyusha1234
Ā 
K-means Clustering with Scikit-Learn
Sarah Guido
Ā 
Clustering: A Scikit Learn Tutorial
Damian R. Mingle, MBA
Ā 
RDataMining slides-clustering-with-r
Yanchang Zhao
Ā 
Pyclustering tutorial - K-means
Andrei Novikov
Ā 
K Means Clustering in ML.pptx
Ramakrishna Reddy Bijjam
Ā 
maXbox starter68 machine learning VI
Max Kleiner
Ā 
5. Types of Clustering Algorithms in ML.pdf
Jyoti Yadav
Ā 
Data Clustering with R
Yanchang Zhao
Ā 
Classification Aalgorithms KNN and Protype-based classifiers.pptx
DrMTayyabChaudhry1
Ā 
Unsupervised Learning: Clustering
Experfy
Ā 
Dimensionality Reduction in machinelearning.pptx
chaitudec2005
Ā 
K-Means Clustering Algorithm.pptx
JebaRaj26
Ā 
Best data science training, best data science training institute in Chennai
hrhrenurenu
Ā 
business analytics course in delhi
devipatnala1
Ā 
data science training
devipatnala1
Ā 
Data science training
prathyusha1234
Ā 
data science institute in bangalore
devipatnala1
Ā 
Best data science training, best data science training institute in hyderabad.
hrhrenurenu
Ā 
Data science certification
prathyusha1234
Ā 
Data scientist course in hyderabad
prathyusha1234
Ā 
Ad

More from Dr. Volkan OBAN (20)

PDF
Conference Paper:IMAGE PROCESSING AND OBJECT DETECTION APPLICATION: INSURANCE...
Dr. Volkan OBAN
Ā 
PDF
Covid19py Python Package - Example
Dr. Volkan OBAN
Ā 
PDF
Object detection with Python
Dr. Volkan OBAN
Ā 
PDF
Python - Rastgele Orman(Random Forest) Parametreleri
Dr. Volkan OBAN
Ā 
DOCX
Linear Programming wi̇th R - Examples
Dr. Volkan OBAN
Ā 
DOCX
"optrees" package in R and examples.(optrees:finds optimal trees in weighted ...
Dr. Volkan OBAN
Ā 
DOCX
Naive Bayes Example using R
Dr. Volkan OBAN
Ā 
DOCX
R forecasting Example
Dr. Volkan OBAN
Ā 
DOCX
k-means Clustering and Custergram with R
Dr. Volkan OBAN
Ā 
PDF
Data Science and its Relationship to Big Data and Data-Driven Decision Making
Dr. Volkan OBAN
Ā 
DOCX
Data Visualization with R.ggplot2 and its extensions examples.
Dr. Volkan OBAN
Ā 
PDF
Scikit-learn Cheatsheet-Python
Dr. Volkan OBAN
Ā 
PDF
Python Pandas for Data Science cheatsheet
Dr. Volkan OBAN
Ā 
PDF
Pandas,scipy,numpy cheatsheet
Dr. Volkan OBAN
Ā 
PPTX
ReporteRs package in R. forming powerpoint documents-an example
Dr. Volkan OBAN
Ā 
PPTX
ReporteRs package in R. forming powerpoint documents-an example
Dr. Volkan OBAN
Ā 
DOCX
R-ggplot2 package Examples
Dr. Volkan OBAN
Ā 
DOCX
R Machine Learning packages( generally used)
Dr. Volkan OBAN
Ā 
DOCX
treemap package in R and examples.
Dr. Volkan OBAN
Ā 
DOCX
Mosaic plot in R.
Dr. Volkan OBAN
Ā 
Conference Paper:IMAGE PROCESSING AND OBJECT DETECTION APPLICATION: INSURANCE...
Dr. Volkan OBAN
Ā 
Covid19py Python Package - Example
Dr. Volkan OBAN
Ā 
Object detection with Python
Dr. Volkan OBAN
Ā 
Python - Rastgele Orman(Random Forest) Parametreleri
Dr. Volkan OBAN
Ā 
Linear Programming wi̇th R - Examples
Dr. Volkan OBAN
Ā 
"optrees" package in R and examples.(optrees:finds optimal trees in weighted ...
Dr. Volkan OBAN
Ā 
Naive Bayes Example using R
Dr. Volkan OBAN
Ā 
R forecasting Example
Dr. Volkan OBAN
Ā 
k-means Clustering and Custergram with R
Dr. Volkan OBAN
Ā 
Data Science and its Relationship to Big Data and Data-Driven Decision Making
Dr. Volkan OBAN
Ā 
Data Visualization with R.ggplot2 and its extensions examples.
Dr. Volkan OBAN
Ā 
Scikit-learn Cheatsheet-Python
Dr. Volkan OBAN
Ā 
Python Pandas for Data Science cheatsheet
Dr. Volkan OBAN
Ā 
Pandas,scipy,numpy cheatsheet
Dr. Volkan OBAN
Ā 
ReporteRs package in R. forming powerpoint documents-an example
Dr. Volkan OBAN
Ā 
ReporteRs package in R. forming powerpoint documents-an example
Dr. Volkan OBAN
Ā 
R-ggplot2 package Examples
Dr. Volkan OBAN
Ā 
R Machine Learning packages( generally used)
Dr. Volkan OBAN
Ā 
treemap package in R and examples.
Dr. Volkan OBAN
Ā 
Mosaic plot in R.
Dr. Volkan OBAN
Ā 
Ad

Recently uploaded (20)

PPTX
Feb 2021 Ransomware Recovery presentation.pptx
enginsayin1
Ā 
PPTX
apidays Singapore 2025 - The Quest for the Greenest LLM , Jean Philippe Ehre...
apidays
Ā 
PPTX
SHREYAS25 INTERN-I,II,III PPT (1).pptx pre
swapnilherage
Ā 
PPTX
big data eco system fundamentals of data science
arivukarasi
Ā 
PDF
A GraphRAG approach for Energy Efficiency Q&A
Marco Brambilla
Ā 
PPTX
apidays Singapore 2025 - Designing for Change, Julie Schiller (Google)
apidays
Ā 
PPTX
Aict presentation on dpplppp sjdhfh.pptx
vabaso5932
Ā 
PPTX
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
Ā 
PDF
Driving Employee Engagement in a Hybrid World.pdf
Mia scott
Ā 
PPTX
apidays Helsinki & North 2025 - API access control strategies beyond JWT bear...
apidays
Ā 
PDF
JavaScript - Good or Bad? Tips for Google Tag Manager
šŸ“Š Markus Baersch
Ā 
PDF
InformaticsPractices-MS - Google Docs.pdf
seshuashwin0829
Ā 
PDF
apidays Singapore 2025 - Trustworthy Generative AI: The Role of Observability...
apidays
Ā 
PPTX
05_Jelle Baats_Tekst.pptx_AI_Barometer_Release_Event
FinTech Belgium
Ā 
PPTX
SlideEgg_501298-Agentic AI.pptx agentic ai
530BYManoj
Ā 
PPTX
How to Add Columns and Rows in an R Data Frame
subhashenia
Ā 
PPTX
apidays Singapore 2025 - Generative AI Landscape Building a Modern Data Strat...
apidays
Ā 
PPTX
BinarySearchTree in datastructures in detail
kichokuttu
Ā 
PDF
apidays Singapore 2025 - How APIs can make - or break - trust in your AI by S...
apidays
Ā 
PPTX
thid ppt defines the ich guridlens and gives the information about the ICH gu...
shaistabegum14
Ā 
Feb 2021 Ransomware Recovery presentation.pptx
enginsayin1
Ā 
apidays Singapore 2025 - The Quest for the Greenest LLM , Jean Philippe Ehre...
apidays
Ā 
SHREYAS25 INTERN-I,II,III PPT (1).pptx pre
swapnilherage
Ā 
big data eco system fundamentals of data science
arivukarasi
Ā 
A GraphRAG approach for Energy Efficiency Q&A
Marco Brambilla
Ā 
apidays Singapore 2025 - Designing for Change, Julie Schiller (Google)
apidays
Ā 
Aict presentation on dpplppp sjdhfh.pptx
vabaso5932
Ā 
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
Ā 
Driving Employee Engagement in a Hybrid World.pdf
Mia scott
Ā 
apidays Helsinki & North 2025 - API access control strategies beyond JWT bear...
apidays
Ā 
JavaScript - Good or Bad? Tips for Google Tag Manager
šŸ“Š Markus Baersch
Ā 
InformaticsPractices-MS - Google Docs.pdf
seshuashwin0829
Ā 
apidays Singapore 2025 - Trustworthy Generative AI: The Role of Observability...
apidays
Ā 
05_Jelle Baats_Tekst.pptx_AI_Barometer_Release_Event
FinTech Belgium
Ā 
SlideEgg_501298-Agentic AI.pptx agentic ai
530BYManoj
Ā 
How to Add Columns and Rows in an R Data Frame
subhashenia
Ā 
apidays Singapore 2025 - Generative AI Landscape Building a Modern Data Strat...
apidays
Ā 
BinarySearchTree in datastructures in detail
kichokuttu
Ā 
apidays Singapore 2025 - How APIs can make - or break - trust in your AI by S...
apidays
Ā 
thid ppt defines the ich guridlens and gives the information about the ICH gu...
shaistabegum14
Ā 

k-means Clustering in Python

  • 1. Prepared by Volkan OBAN k-means Clustering in Python. scikit-learn Machine Learning in Python from sklearn.cluster import KMeans ref: http://scikit-learn.org/stable/auto_examples/cluster/plot_cluster_iris.html Code: print(__doc__) # Code source: GaĆ«l Varoquaux # Modified for documentation by Jaques Grobler # License: BSD 3 clause import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from sklearn.cluster import KMeans from sklearn import datasets np.random.seed(5) centers = [[1, 1], [-1, -1], [1, -1]]
  • 2. iris = datasets.load_iris() X = iris.data y = iris.target estimators = {'k_means_iris_3': KMeans(n_clusters=3), 'k_means_iris_8': KMeans(n_clusters=8), 'k_means_iris_bad_init': KMeans(n_clusters=3, n_init=1, init='random')} fignum = 1 for name, est in estimators.items(): fig = plt.figure(fignum, figsize=(4, 3)) plt.clf() ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134) plt.cla() est.fit(X) labels = est.labels_ ax.scatter(X[:, 3], X[:, 0], X[:, 2], c=labels.astype(np.float)) ax.w_xaxis.set_ticklabels([]) ax.w_yaxis.set_ticklabels([]) ax.w_zaxis.set_ticklabels([]) ax.set_xlabel('Petal width') ax.set_ylabel('Sepal length') ax.set_zlabel('Petal length') fignum = fignum + 1 # Plot the ground truth fig = plt.figure(fignum, figsize=(4, 3)) plt.clf() ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)
  • 3. plt.cla() for name, label in [('Setosa', 0), ('Versicolour', 1), ('Virginica', 2)]: ax.text3D(X[y == label, 3].mean(), X[y == label, 0].mean() + 1.5, X[y == label, 2].mean(), name, horizontalalignment='center', bbox=dict(alpha=.5, edgecolor='w', facecolor='w')) # Reorder the labels to have colors matching the cluster results y = np.choose(y, [1, 2, 0]).astype(np.float) ax.scatter(X[:, 3], X[:, 0], X[:, 2], c=y) ax.w_xaxis.set_ticklabels([]) ax.w_yaxis.set_ticklabels([]) ax.w_zaxis.set_ticklabels([]) ax.set_xlabel('Petal width') ax.set_ylabel('Sepal length') ax.set_zlabel('Petal length') plt.show()