运用scikit-learn库进行聚类分析
聚类分析常用的方法有 k-means聚类算法、 层次聚类算法、DBSCAN(基于空间密度的聚类算法)等,本文就这三种方法对聚类分析进行简单介绍。
一、 k-means聚类算法
(一)k-means算法
# 创建数据集
from sklearn.datasets import make_blobs
# n_features = 2设置X特征数量
X,y = make_blobs(n_samples = 150, n_features = 2, centers = 3, cluster_std = 0.5, shuffle = True, random_state = 0)
import matplotlib.pyplot as plt
# 使文字可以展示
plt.rcParams['font.sans-serif'] = ['SimHei']
# 使负号可以展示
plt.rcParams['axes.unicode_minus'] = False
plt.scatter(X[:,0], X[:,1], c = 'blue', marker = 'o', s = 50)
plt.grid()
plt.show()
# 使用k-means进行聚类
from sklearn.cluster import KMeans
# tol = 1e-04设置容忍度
# n_clusters = 3设置的簇的数量
km = KMeans(n_clusters = 3, init = 'random',n_init = 10, max_iter = 300, tol = 1e-04, random_state = 0)
y_km = km.fit_predict(X)
# k-means++进行聚类
plt.scatter(X[y_km == 0, 0], X[y_km == 0, 1], s = 50, c = 'lightgreen', marker = 's', label = '簇 1')
plt.scatter(X[y_km == 1, 0], X[y_km == 1, 1], s = 50, c = 'orange', marker = 'o', label = '簇 2')
plt.scatter(X[y_km == 2, 0], X[y_km == 2, 1], s = 50, c = 'lightblue', marker = 'v', label = '簇 3')
plt.scatter(km.cluster_centers_[:,0], km.cluster_centers_[:,1], s = 250, marker = '*', c = 'red', label = '中心点')
plt.legend()<