SlideShare a Scribd company logo
1 of 17
Download to read offline
PyClustering: K-Means Tutorial
Pyclustering Library Tutorial
Theme: K-Means
Library Version: 0.8.1
Author: Andrei Novikov
E-Mail: pyclustering@yandex.ru
Agenda
● PyClustering K-Means Features;
● Input format for K-Means;
● Data clustering using K-Means;
● Additional parameters of the algorithm;
● Data clustering result visualization;
● Image segmentation by K-Meas;
PyClustering K-Means Features
● Python implementation based on numpy.
● C/C++ implementation – core library that is supported for
32, 64-bit Windows, Linux.
● K-Means observer to collect information about clustering
process on each iteration.
● K-Means visualizer to display and animate K-Means
specific results.
C/C++ implementation can be used without python if integration with C/C++
project is required. Use sources from „pyclustering/ccore“.
Import K-Means
K-Means algorithm and its features can be imported from
„pyclustering.cluster.kmeans“:
from pyclustering.cluster.kmeans import kmeans
from pyclustering.cluster.kmeans import kmeans_observer
from pyclustering.cluster.kmeans import kmeans_visualizer
Input data format for K-Means
K-Means algorithm uses array_like data format, for
example, list where each element is point that is
represented by coordinates. Here is an example of 1-D
data:
dataset = [ [0.25], [0.43], [1.34], [-0.56] ]
Example of 2-D data:
dataset = [ [0.1, 0.2], [0.1, 0.6], [0.2, -1.4] ]
Example of 3-D data:
dataset = [ [0.6, 0.1, 0.4], [0.2, 0.1, 0.9] ]
Clustering by K-Means algorithm
K-Means uses two general parameters for clustering: input
data and initial cluster centers.
from pyclustering.cluster.kmeans import kmeans
from pyclustering.cluster.center_initializer import kmeans_plusplus_initializer;
from pyclustering.utils import read_sample;
data = [ [0.1], [0.2], [0.5], [0.3], [1.5], [1.8], [1.3], [1.6], [1.5] ]
initial_centers = kmeans_plusplus_initializer(data, 2).initialize()
instance = kmeans(data, initial_centers)
instance.process() # perform processing
clusters = instance.get_clusters(); # allocated clusters
centers = instance.get_centers(); # cluster centers
Output result of K-Means
K-Means algorithm returns allocated clusters and
corresponding centers:
● Clusters are represented by list of clusters and each
cluster contains object indexes from dataset, for example:
[ [0, 1, 4], [2, 3, 5] ], where 0, 1, 4 – object indexes that
forms the first cluster, and 2, 3, 5 – forms the second.
● Centers are represented by list of centers where each
center is a point represented by coordinates.
Library core usage
By default core of pyclustering library that is called „ccore“
used for processing (C/C++ implementation of the
algorithm). If platform is not supported then python
implementation is used. There is special parameter that
can be used to switch on/off core usage:
# switch on core (switch on by default, and it is ignored if impossible to use core
instance = kmeans(data, initial_centers, ccore=True)
# switch off core (python implementation is used instead)
instance = kmeans(data, initial_centers, ccore=False)
K-Means result visualization
Output result can be shown by common visualizer
„cluster_visualizer“ from „pyclustering.cluster“ or by K-
Means visualizer:
instance = kmeans(sample, start_centers, 0.0001)
instance.process()
clusters = instance.get_clusters()
centers = instance.get_centers()
# visualize clustering results
kmeans_visualizer.show_clusters(sample, clusters, centers, start_centers);
Stop condition – tolerance
K-Means calculates center changes on current and
previous iterations and if difference is less than threshold
value defined by parameter „tolerance“ than clustering
process is over.
Example with small data points:
data = [ [0.0000001], [0.00000023], [0.00000051], [0.0000002] ]
tolerance = 0.000000001
instance = kmeans(data, initial_centers, tolerance)
But in case of very big or very small data values
normalization should be used.
K-Means visualizer
Output result can be shown by common visualizer
„cluster_visualizer“ from „pyclustering.cluster“ or by K-
Means visualizer:
instance = kmeans(sample, start_centers, 0.0001)
instance.process()
clusters = instance.get_clusters()
centers = instance.get_centers()
# visualize clustering results
kmeans_visualizer.show_clusters(sample, clusters, centers, start_centers);
Example of result visualization
Here several examples of K-Means visualization:
K-Means result animation
K-Means clustering process can be visualized as an
animation and saved to file. „kmeans_observer“ is used to
collect information of each step of processing and the
observer is used by „kmeans_visualizer“ then.
observer = kmeans_visualizer()
instance = kmeans(data, start_centers, observer=observer)
instance.process()
# animate clustering process
kmeans_visualizer.animate_cluster_allocation(sample, observer);
# or save animation to file
kmeans_visualizer.animate_cluster_allocation(data, observer, save_movie=file.mp4);
K-Means for segmentation
K-Means can consider image as a data where each point
represent pixel with three coordinate (RGB), or with four
coordinate (RGBA). Here is an example of image
segmentation using K-Means algorithm:
from pyclustering.utils import draw_image_mask_segments, read_image
from pyclustering.cluster.kmeans import kmeans;
data = read_image(file_path_to_image);
kmeans_instance = kmeans(data, start_centers);
kmeans_instance.process();
clusters = kmeans_instance.get_clusters();
draw_image_mask_segments(source, clusters);
Image segmentation results
Here several examples of image segmentation by K-
Means algorithm using pyclustering.
References and Links
● Official pyclustering github repository:
https://github.com/annoviko/pyclustering
● Official pypi pyclustering page:
https://pypi.python.org/pypi/pyclustering/0.8.0
● Official pyclustering web-site:
https://pyclustering.github.io/
Thank you for your attention
Thank you!

More Related Content

What's hot

Introduction to Clustering algorithm
Introduction to Clustering algorithmIntroduction to Clustering algorithm
Introduction to Clustering algorithmhadifar
 
Wrapper feature selection method
Wrapper feature selection methodWrapper feature selection method
Wrapper feature selection methodAmir Razmjou
 
K means clustering
K means clusteringK means clustering
K means clusteringAhmedasbasb
 
Anomaly detection with machine learning at scale
Anomaly detection with machine learning at scaleAnomaly detection with machine learning at scale
Anomaly detection with machine learning at scaleImpetus Technologies
 
Mean shift and Hierarchical clustering
Mean shift and Hierarchical clustering Mean shift and Hierarchical clustering
Mean shift and Hierarchical clustering Yan Xu
 
(2022年3月版)深層学習によるImage Classificaitonの発展
(2022年3月版)深層学習によるImage Classificaitonの発展(2022年3月版)深層学習によるImage Classificaitonの発展
(2022年3月版)深層学習によるImage Classificaitonの発展Takumi Ohkuma
 
Spectral clustering
Spectral clusteringSpectral clustering
Spectral clusteringSOYEON KIM
 
Nearest Neighbor Algorithm Zaffar Ahmed
Nearest Neighbor Algorithm  Zaffar AhmedNearest Neighbor Algorithm  Zaffar Ahmed
Nearest Neighbor Algorithm Zaffar AhmedZaffar Ahmed Shaikh
 
introduction to data mining tutorial
introduction to data mining tutorial introduction to data mining tutorial
introduction to data mining tutorial Salah Amean
 
Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...
Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...
Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...Salah Amean
 
U-Netpresentation.pptx
U-Netpresentation.pptxU-Netpresentation.pptx
U-Netpresentation.pptxNoorUlHaq47
 
Machine learning introduction
Machine learning introductionMachine learning introduction
Machine learning introductionAnas Jamil
 
Data Visualization in Exploratory Data Analysis
Data Visualization in Exploratory Data AnalysisData Visualization in Exploratory Data Analysis
Data Visualization in Exploratory Data AnalysisEva Durall
 

What's hot (20)

Introduction to Clustering algorithm
Introduction to Clustering algorithmIntroduction to Clustering algorithm
Introduction to Clustering algorithm
 
K means Clustering Algorithm
K means Clustering AlgorithmK means Clustering Algorithm
K means Clustering Algorithm
 
Wrapper feature selection method
Wrapper feature selection methodWrapper feature selection method
Wrapper feature selection method
 
K means clustering
K means clusteringK means clustering
K means clustering
 
Anomaly detection with machine learning at scale
Anomaly detection with machine learning at scaleAnomaly detection with machine learning at scale
Anomaly detection with machine learning at scale
 
Deep Learning for Video: Action Recognition (UPC 2018)
Deep Learning for Video: Action Recognition (UPC 2018)Deep Learning for Video: Action Recognition (UPC 2018)
Deep Learning for Video: Action Recognition (UPC 2018)
 
Presentation on K-Means Clustering
Presentation on K-Means ClusteringPresentation on K-Means Clustering
Presentation on K-Means Clustering
 
Mean shift and Hierarchical clustering
Mean shift and Hierarchical clustering Mean shift and Hierarchical clustering
Mean shift and Hierarchical clustering
 
Kmeans
KmeansKmeans
Kmeans
 
(2022年3月版)深層学習によるImage Classificaitonの発展
(2022年3月版)深層学習によるImage Classificaitonの発展(2022年3月版)深層学習によるImage Classificaitonの発展
(2022年3月版)深層学習によるImage Classificaitonの発展
 
Chapter8
Chapter8Chapter8
Chapter8
 
K mean-clustering
K mean-clusteringK mean-clustering
K mean-clustering
 
Spectral clustering
Spectral clusteringSpectral clustering
Spectral clustering
 
Nearest Neighbor Algorithm Zaffar Ahmed
Nearest Neighbor Algorithm  Zaffar AhmedNearest Neighbor Algorithm  Zaffar Ahmed
Nearest Neighbor Algorithm Zaffar Ahmed
 
introduction to data mining tutorial
introduction to data mining tutorial introduction to data mining tutorial
introduction to data mining tutorial
 
Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...
Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...
Data Mining Concepts and Techniques, Chapter 10. Cluster Analysis: Basic Conc...
 
U-Netpresentation.pptx
U-Netpresentation.pptxU-Netpresentation.pptx
U-Netpresentation.pptx
 
Anomaly detection
Anomaly detectionAnomaly detection
Anomaly detection
 
Machine learning introduction
Machine learning introductionMachine learning introduction
Machine learning introduction
 
Data Visualization in Exploratory Data Analysis
Data Visualization in Exploratory Data AnalysisData Visualization in Exploratory Data Analysis
Data Visualization in Exploratory Data Analysis
 

Similar to Pyclustering tutorial - K-means

Unsupervised Aspect Based Sentiment Analysis at Scale
Unsupervised Aspect Based Sentiment Analysis at ScaleUnsupervised Aspect Based Sentiment Analysis at Scale
Unsupervised Aspect Based Sentiment Analysis at ScaleAaron (Ari) Bornstein
 
Lab 2: Classification and Regression Prediction Models, training and testing ...
Lab 2: Classification and Regression Prediction Models, training and testing ...Lab 2: Classification and Regression Prediction Models, training and testing ...
Lab 2: Classification and Regression Prediction Models, training and testing ...Yao Yao
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxSandeep Singh
 
Python for Data Analysis.pdf
Python for Data Analysis.pdfPython for Data Analysis.pdf
Python for Data Analysis.pdfJulioRecaldeLara1
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxtangadhurai
 
Azure machine learning service
Azure machine learning serviceAzure machine learning service
Azure machine learning serviceRuth Yakubu
 
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...Yao Yao
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using PythonNishantKumar1179
 
Viktor Tsykunov: Azure Machine Learning Service
Viktor Tsykunov: Azure Machine Learning ServiceViktor Tsykunov: Azure Machine Learning Service
Viktor Tsykunov: Azure Machine Learning ServiceLviv Startup Club
 
K-Means Algorithm Implementation In python
K-Means Algorithm Implementation In pythonK-Means Algorithm Implementation In python
K-Means Algorithm Implementation In pythonAfzal Ahmad
 
Python-for-Data-Analysis.pdf
Python-for-Data-Analysis.pdfPython-for-Data-Analysis.pdf
Python-for-Data-Analysis.pdfssuser598883
 
Autoscaling in kubernetes v1
Autoscaling in kubernetes v1Autoscaling in kubernetes v1
Autoscaling in kubernetes v1JurajHantk
 
Introduction to Chainer 11 may,2018
Introduction to Chainer 11 may,2018Introduction to Chainer 11 may,2018
Introduction to Chainer 11 may,2018Preferred Networks
 
maxbox starter60 machine learning
maxbox starter60 machine learningmaxbox starter60 machine learning
maxbox starter60 machine learningMax Kleiner
 
I want my model to be deployed ! (another story of MLOps)
I want my model to be deployed ! (another story of MLOps)I want my model to be deployed ! (another story of MLOps)
I want my model to be deployed ! (another story of MLOps)AZUG FR
 
Machine learning Experiments report
Machine learning Experiments report Machine learning Experiments report
Machine learning Experiments report AlmkdadAli
 
Introduction to Chainer
Introduction to ChainerIntroduction to Chainer
Introduction to ChainerSeiya Tokui
 

Similar to Pyclustering tutorial - K-means (20)

Unsupervised Aspect Based Sentiment Analysis at Scale
Unsupervised Aspect Based Sentiment Analysis at ScaleUnsupervised Aspect Based Sentiment Analysis at Scale
Unsupervised Aspect Based Sentiment Analysis at Scale
 
Lab 2: Classification and Regression Prediction Models, training and testing ...
Lab 2: Classification and Regression Prediction Models, training and testing ...Lab 2: Classification and Regression Prediction Models, training and testing ...
Lab 2: Classification and Regression Prediction Models, training and testing ...
 
Python for data analysis
Python for data analysisPython for data analysis
Python for data analysis
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptx
 
Python for Data Analysis.pdf
Python for Data Analysis.pdfPython for Data Analysis.pdf
Python for Data Analysis.pdf
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptx
 
Azure machine learning service
Azure machine learning serviceAzure machine learning service
Azure machine learning service
 
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using Python
 
Viktor Tsykunov: Azure Machine Learning Service
Viktor Tsykunov: Azure Machine Learning ServiceViktor Tsykunov: Azure Machine Learning Service
Viktor Tsykunov: Azure Machine Learning Service
 
K-Means Algorithm Implementation In python
K-Means Algorithm Implementation In pythonK-Means Algorithm Implementation In python
K-Means Algorithm Implementation In python
 
Python-for-Data-Analysis.pdf
Python-for-Data-Analysis.pdfPython-for-Data-Analysis.pdf
Python-for-Data-Analysis.pdf
 
Ceilometer + Heat = Alarming
Ceilometer + Heat = Alarming Ceilometer + Heat = Alarming
Ceilometer + Heat = Alarming
 
Autoscaling in kubernetes v1
Autoscaling in kubernetes v1Autoscaling in kubernetes v1
Autoscaling in kubernetes v1
 
Introduction to Chainer 11 may,2018
Introduction to Chainer 11 may,2018Introduction to Chainer 11 may,2018
Introduction to Chainer 11 may,2018
 
maxbox starter60 machine learning
maxbox starter60 machine learningmaxbox starter60 machine learning
maxbox starter60 machine learning
 
I want my model to be deployed ! (another story of MLOps)
I want my model to be deployed ! (another story of MLOps)I want my model to be deployed ! (another story of MLOps)
I want my model to be deployed ! (another story of MLOps)
 
Machine learning Experiments report
Machine learning Experiments report Machine learning Experiments report
Machine learning Experiments report
 
Xgboost
XgboostXgboost
Xgboost
 
Introduction to Chainer
Introduction to ChainerIntroduction to Chainer
Introduction to Chainer
 

Recently uploaded

Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxchumtiyababu
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersMairaAshraf6
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Call Girls Mumbai
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesRAJNEESHKUMAR341697
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxSCMS School of Architecture
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilVinayVitekari
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 

Recently uploaded (20)

Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 

Pyclustering tutorial - K-means

  • 1. PyClustering: K-Means Tutorial Pyclustering Library Tutorial Theme: K-Means Library Version: 0.8.1 Author: Andrei Novikov E-Mail: pyclustering@yandex.ru
  • 2. Agenda ● PyClustering K-Means Features; ● Input format for K-Means; ● Data clustering using K-Means; ● Additional parameters of the algorithm; ● Data clustering result visualization; ● Image segmentation by K-Meas;
  • 3. PyClustering K-Means Features ● Python implementation based on numpy. ● C/C++ implementation – core library that is supported for 32, 64-bit Windows, Linux. ● K-Means observer to collect information about clustering process on each iteration. ● K-Means visualizer to display and animate K-Means specific results. C/C++ implementation can be used without python if integration with C/C++ project is required. Use sources from „pyclustering/ccore“.
  • 4. Import K-Means K-Means algorithm and its features can be imported from „pyclustering.cluster.kmeans“: from pyclustering.cluster.kmeans import kmeans from pyclustering.cluster.kmeans import kmeans_observer from pyclustering.cluster.kmeans import kmeans_visualizer
  • 5. Input data format for K-Means K-Means algorithm uses array_like data format, for example, list where each element is point that is represented by coordinates. Here is an example of 1-D data: dataset = [ [0.25], [0.43], [1.34], [-0.56] ] Example of 2-D data: dataset = [ [0.1, 0.2], [0.1, 0.6], [0.2, -1.4] ] Example of 3-D data: dataset = [ [0.6, 0.1, 0.4], [0.2, 0.1, 0.9] ]
  • 6. Clustering by K-Means algorithm K-Means uses two general parameters for clustering: input data and initial cluster centers. from pyclustering.cluster.kmeans import kmeans from pyclustering.cluster.center_initializer import kmeans_plusplus_initializer; from pyclustering.utils import read_sample; data = [ [0.1], [0.2], [0.5], [0.3], [1.5], [1.8], [1.3], [1.6], [1.5] ] initial_centers = kmeans_plusplus_initializer(data, 2).initialize() instance = kmeans(data, initial_centers) instance.process() # perform processing clusters = instance.get_clusters(); # allocated clusters centers = instance.get_centers(); # cluster centers
  • 7. Output result of K-Means K-Means algorithm returns allocated clusters and corresponding centers: ● Clusters are represented by list of clusters and each cluster contains object indexes from dataset, for example: [ [0, 1, 4], [2, 3, 5] ], where 0, 1, 4 – object indexes that forms the first cluster, and 2, 3, 5 – forms the second. ● Centers are represented by list of centers where each center is a point represented by coordinates.
  • 8. Library core usage By default core of pyclustering library that is called „ccore“ used for processing (C/C++ implementation of the algorithm). If platform is not supported then python implementation is used. There is special parameter that can be used to switch on/off core usage: # switch on core (switch on by default, and it is ignored if impossible to use core instance = kmeans(data, initial_centers, ccore=True) # switch off core (python implementation is used instead) instance = kmeans(data, initial_centers, ccore=False)
  • 9. K-Means result visualization Output result can be shown by common visualizer „cluster_visualizer“ from „pyclustering.cluster“ or by K- Means visualizer: instance = kmeans(sample, start_centers, 0.0001) instance.process() clusters = instance.get_clusters() centers = instance.get_centers() # visualize clustering results kmeans_visualizer.show_clusters(sample, clusters, centers, start_centers);
  • 10. Stop condition – tolerance K-Means calculates center changes on current and previous iterations and if difference is less than threshold value defined by parameter „tolerance“ than clustering process is over. Example with small data points: data = [ [0.0000001], [0.00000023], [0.00000051], [0.0000002] ] tolerance = 0.000000001 instance = kmeans(data, initial_centers, tolerance) But in case of very big or very small data values normalization should be used.
  • 11. K-Means visualizer Output result can be shown by common visualizer „cluster_visualizer“ from „pyclustering.cluster“ or by K- Means visualizer: instance = kmeans(sample, start_centers, 0.0001) instance.process() clusters = instance.get_clusters() centers = instance.get_centers() # visualize clustering results kmeans_visualizer.show_clusters(sample, clusters, centers, start_centers);
  • 12. Example of result visualization Here several examples of K-Means visualization:
  • 13. K-Means result animation K-Means clustering process can be visualized as an animation and saved to file. „kmeans_observer“ is used to collect information of each step of processing and the observer is used by „kmeans_visualizer“ then. observer = kmeans_visualizer() instance = kmeans(data, start_centers, observer=observer) instance.process() # animate clustering process kmeans_visualizer.animate_cluster_allocation(sample, observer); # or save animation to file kmeans_visualizer.animate_cluster_allocation(data, observer, save_movie=file.mp4);
  • 14. K-Means for segmentation K-Means can consider image as a data where each point represent pixel with three coordinate (RGB), or with four coordinate (RGBA). Here is an example of image segmentation using K-Means algorithm: from pyclustering.utils import draw_image_mask_segments, read_image from pyclustering.cluster.kmeans import kmeans; data = read_image(file_path_to_image); kmeans_instance = kmeans(data, start_centers); kmeans_instance.process(); clusters = kmeans_instance.get_clusters(); draw_image_mask_segments(source, clusters);
  • 15. Image segmentation results Here several examples of image segmentation by K- Means algorithm using pyclustering.
  • 16. References and Links ● Official pyclustering github repository: https://github.com/annoviko/pyclustering ● Official pypi pyclustering page: https://pypi.python.org/pypi/pyclustering/0.8.0 ● Official pyclustering web-site: https://pyclustering.github.io/
  • 17. Thank you for your attention Thank you!