1). The k-means clustering process of manual playing of playing cards:> 30 cards, 3 types
For the first time, randomly select three cluster centers K, 7, 6 and then randomly select 30 clusters for classification
Calculate the average value, the average value of the three piles of cards is about 12,7,4, and then re-classify the cards again, according to the smallest distance classification
Calculate the average again, the average value of the three piles of cards is about 12, 7, 3, and then re-classify the cards again
Finally, the average value is unchanged, and the classification ends
2). * K-means algorithm is independently written, clustering is performed on the iris petal length data, and displayed with a scatterplot. (Plus points)
3). Use sklearn.cluster.KMeans and iris petal length data for clustering and display with scatter plot.
sklearn.datasets from Import load_iris from sklearn.cluster Import KMeans Import matplotlib.pyplot AS PLT IRIS = load_iris () # acquired iris data set iris.keys () X = iris.data [:, 0] # acquired iris petals length data X = x.reshape (-1,1) # convert the data to a data # direct calls sklearn library implementation of iris data clustering km_model = KMeans (= n_clusters. 3) # build a model divided into three categories km_model. Fit (the X-) # training model the y-km_model.predict = (the X-) # prediction model Print ( " cluster center:" , Km_model.cluster_centers_) Print ( " prediction result: " , Y) # drawing plt.scatter (X [:, 0], X [:, 0], Y = C, S = 50, = CMap ' Rainbow ' ) plt.show ()
The results are as follows
4). Complete data of iris flowers are clustered and displayed with a scatterplot.
from sklearn.datasets Import load_iris from sklearn.cluster Import KMeans Import matplotlib.pyplot AS plt IRIS = load_iris () # get iris data set the X-iris.data = # Iris complete data # direct call sklearn library KMeans achieve Iris datasets. km_model = KMeans (= n_clusters. 3) # build models km_model.fit (X) # training model Y = km_model.predict (X) # forecast model clustering index for each sample Print ( " cluster center : " , km_model.cluster_centers_) print ( " Prediction result:", y) # 画图 plt.scatter(x[:, 2], x[:, 3], c=y, s=50, cmap='rainbow') #x,y,c plt.show()
The result is as shown
5). Think about what is used in the k-means algorithm?