Machine Learning-Iris

1). The k-means clustering process of manual playing of playing cards:> 30 cards, 3 types

 

Randomly choose three centers

 

 

 

 

 

After clustering, three new centers are selected

 

 

 

 

 After passing the clustering, three new centers were re-selected, which can no longer be divided, and the final result is 6, 17, 26

 

 

2). Use sklearn.cluster.KMeans and iris petal length data for clustering and display with scatter plot.

#-*-coding: utf-8-*- 
# File name: machine learning-iris.py

import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import load_iris from sklearn.cluster import KMeans iris = load_iris () # import data sj = iris.data [:, 2] # third column petal length x = sj. reshape (-1, 1) km_model = KMeans (n_clusters = 3) km_model.fit (x) #Training y = km_model.predict (x) #prediction plt.scatter (x [:, 0], x [:, 0] , c = y, s = 50, cmap = "rainbow") plt.show ()

 

The prediction results are as follows

 

 

Use the iris petal length data to make a cluster scatter plot as follows

 

 

3). Complete data of iris is clustered and displayed with scatter plot.

#-*-coding: utf-8-*- 
# File name: machine learning-iris.py 
import matplotlib.pyplot as plt 
import numpy as np 
from sklearn.datasets import load_iris 
from sklearn.cluster import KMeans 


iris = load_iris () 
#Import data x = iris.data 
km_model = KMeans (n_clusters = 3) 
km_model.fit (x) 
#Training y = km_model.predict (x) 
#predict 
y plt.scatter (x [:, 2], x [:, 3], c = y, s = 50, cmap = "rainbow") 
plt.show ()

  Predictive value

 

 The scatter plot is as follows:

 

 

 

4). Think about what is used in the k-means algorithm?

The k-means algorithm is used for pixel processing. The color of each pixel of the image represents the training sample x. Through the k-means algorithm, 16 colors are used to represent the colors of all pixels in the image, that is, 16 cluster centers. Finally, replace all pixel colors with the colors corresponding to the 16 cluster centers.

 

 

 

 

Guess you like

Origin www.cnblogs.com/zhff/p/12695403.html