2024最新分别利用sklearn和Numpy实现c均值对鸢尾花数据集进行聚类(附完整代码和注释)

C均值聚类算法(K-Means Clustering)是一种非常流行的聚类算法,用于将数据点分成多个簇,使得簇内的点尽可能相似,簇间的点尽可能不同。以下是K-Means算法的基本步骤:

  1. 初始化:随机选择K个点作为初始的簇中心(质心)

  2. 分配:将每个数据点分配到最近的质心所属的簇中。

  3. 更新:计算每个簇中所有点的均值,更新质心为这个均值。

  4. 迭代:重复步骤2和3,直到满足某个终止条件(例如,达到最大迭代次数,或者质心的变化小于某个阈值)。

  5. 终止:当满足终止条件时,算法结束,最终的簇划分就是聚类结果。

sklearn方法

import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
from sklearn.preprocessing import StandardScaler

# 加载鸢尾花数据集
iris = datasets.load_iris()
X = iris.data

# 数据标准化
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# 定义不同的K值
k_values = [2, 3, 4, 5]

# 评估不同K值的聚类效果
for k in k_values:
    kmeans = KMeans(n_clusters=k, random_state=42)
    kmeans.fit(X_scaled)
    labels = kmeans.labels_
    
    # 计算轮廓系数
    silhouette_avg = silhouette_score(X_scaled, labels)
    print(f"For n_clusters = {k}, silhouette score is {silhouette_avg}")

    # 可视化聚类效果
    plt.figure(figsize=(8, 6))
    plt.scatter(X_scaled[:, 0], X_scaled[:, 1], c=labels, cmap='viridis', marker='o', label='Cluster')
    centers = kmeans.cluster_centers_
    plt.scatter(centers[:, 0], centers[:, 1], c='red', s=200, alpha=0.75, marker='*', label='Centroids')
    plt.title(f'K-Means Clustering with n_clusters = {k}')
    plt.xlabel('Feature 1')
    plt.ylabel('Feature 2')
    plt.legend()
    plt.show()

# 评估不同初始化方法的聚类效果
k = 3
#
init_methods = ['random', 'k-means++']
for init in init_methods:
    kmeans = KMeans(n_clusters=k, init=init, random_state=42)
    kmeans.fit(X_scaled)
    labels = kmeans.labels_
    
    # 计算轮廓系数
    silhouette_avg = silhouette_score(X_scaled, labels)
    print(f"For n_clusters = {k}, init method = {init}, silhouette score is {silhouette_avg}")

    # 可视化聚类效果
    plt.figure(figsize=(8, 6))
    plt.scatter(X_scaled[:, 0], X_scaled[:, 1], c=labels, cmap='viridis', marker='o', label='Cluster')
    centers = kmeans.cluster_centers_
    plt.scatter(centers[:, 0], centers[:, 1], c='red', s=200, alpha=0.75, marker='*', label='Centroids')
    plt.title(f'K-Means Clustering with n_clusters = {k}, init method = {init}')
    plt.xlabel('Feature 1')
    plt.ylabel('Feature 2')
    plt.legend()
    plt.show()

Numpy方法

import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.preprocessing import StandardScaler

#使用 NumPy 实现 K-Means 算法
def kmeans(X, k, max_iters=100):
    n_samples, n_features = X.shape
    centroids = X[np.random.choice(n_samples, k, replace=False)]
    for _ in range(max_iters):
        distances = np.sqrt((X[:, np.newaxis] - centroids) ** 2).sum(axis=2)
        labels = np.argmin(distances, axis=1)
        new_centroids = np.array([X[labels == i].mean(axis=0) for i in range(k)])
        if np.all(centroids == new_centroids):
            break
        centroids = new_centroids
    return labels, centroids

#加载鸢尾花数据集并对其进行标准化
iris = datasets.load_iris()
X = iris.data
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

#评估不同 c 值的聚类效果
from sklearn.metrics import silhouette_score
k_values = [2, 3, 4]
for k in k_values:
    labels, centers = kmeans(X_scaled, k)
    silhouette_avg = silhouette_score(X_scaled, labels)
    print(f"For n_clusters = {k}, silhouette score is {silhouette_avg}")
#可视化每个 k 值的聚类结果。
for k in k_values:
    labels, centers = kmeans(X_scaled, k)
    plt.figure(figsize=(8, 6))
    plt.scatter(X_scaled[:, 0], X_scaled[:, 1], c=labels, cmap='viridis', marker='o', label='Cluster')
    plt.scatter(centers[:, 0], centers[:, 1], c='red', s=200, alpha=0.75, marker='*', label='Centroids')
    plt.title(f'K-Means Clustering with n_clusters = {k}')
    plt.xlabel('Feature 1')
    plt.ylabel('Feature 2')
    plt.legend()
    plt.show()
    
#评估不同初始化方法对聚类效果的影响
def kmeans_plusplus(X, k, max_iters=100):
    n_samples, n_features = X.shape
    centroids = [X[np.random.choice(n_samples)]]
    for _ in range(1, k):
        distances = np.sqrt((X[:, np.newaxis] - centroids) ** 2).sum(axis=2)
        probabilities = distances.min(axis=1) ** 2
        cumulative_probabilities = probabilities.cumsum()
        r = np.random.rand() * cumulative_probabilities[-1]
        new_centroid_index = np.searchsorted(cumulative_probabilities, r)
        centroids.append(X[new_centroid_index])
    
    centroids = np.array(centroids)
    for _ in range(max_iters):
        distances = np.sqrt((X[:, np.newaxis] - centroids) ** 2).sum(axis=2)
        labels = np.argmin(distances, axis=1)
        new_centroids = np.array([X[labels == i].mean(axis=0) for i in range(k)])
        if np.all(centroids == new_centroids):
            break
        centroids = new_centroids
    return labels, centroids

for k in k_values:
    labels, centers = kmeans_plusplus(X_scaled, k)
    silhouette_avg = silhouette_score(X_scaled, labels)
    print(f"For n_clusters = {k}, silhouette score (k-means++) is {silhouette_avg}")

    plt.figure(figsize=(8, 6))
    plt.scatter(X_scaled[:, 0], X_scaled[:, 1], c=labels, cmap='viridis', marker='o', label='Cluster')
    plt.scatter(centers[:, 0], centers[:, 1], c='red', s=200, alpha=0.75, marker='*', label='Centroids')
    plt.title(f'K-Means++ Clustering with n_clusters = {k}')
    plt.xlabel('Feature 1')
    plt.ylabel('Feature 2')
    plt.legend()
    plt.show()
相关推荐
梭七y1 天前
(自用)机器学习python代码相关笔记
笔记·机器学习·sklearn
人生の三重奏1 天前
numpy——数学运算
开发语言·python·numpy
努力减肥的小胖子51 天前
opencv - py_photo - py_non_local_means 非局部均值去噪
opencv·均值算法
小白熊_XBX1 天前
机器学习实战——基于粒子群优化算法(PSO)优化支持向量回归(SVR)模型(附完整代码)
人工智能·算法·机器学习·分类·数据挖掘·回归·sklearn
微雨盈萍cbb1 天前
ImportError: numpy.core.multiarray failed to import
numpy
lifushuo1 天前
Numpy 1.数据类型
numpy
像风一样自由20202 天前
如何使用 NumPy 和 Matplotlib 进行数据可视化
信息可视化·numpy·matplotlib
像风一样自由20203 天前
使用 NumPy 和 Matplotlib 进行高级数据可视化:实践指南
信息可视化·numpy·matplotlib
cgqyw4 天前
国内知名的几个镜像源
python·numpy·scipy
C囧囧4 天前
Python数值计算(33)——simpson 3/8积分公式
python·numpy·1024程序员节·数值积分·辛普森法则