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()
相关推荐
uncle_ll5 小时前
PyTorch图像预处理:计算均值和方差以实现标准化
图像处理·人工智能·pytorch·均值算法·标准化
敲代码不忘补水1 天前
Python Matplotlib 数据可视化全面解析:选择它的七大理由与入门简介
开发语言·python·信息可视化·numpy·pandas·matplotlib
曼城周杰伦2 天前
表格不同类型的数据如何向量化?
人工智能·机器学习·分类·数据挖掘·sklearn·word2vec
沙度灬2 天前
python之sklearn--鸢尾花数据集之数据降维(PCA主成分分析)
开发语言·python·sklearn
Taverry2 天前
机器学习杂笔记1:类型-数据集-效果评估-sklearn-机器学习算法分类
算法·机器学习·sklearn
取个名字真难呐3 天前
2、PyTorch张量的运算API(上)
pytorch·python·numpy
敲代码不忘补水3 天前
pandas 机器学习数据预处理:从缺失值到特征切分的全面解析
人工智能·后端·python·机器学习·numpy·pandas·matplotlib
baijin_cha3 天前
机器学习基础07_sklearn梯度下降&岭回归&拉索回归&逻辑回归
笔记·机器学习·回归·sklearn
颹蕭蕭5 天前
均值方差增量计算
算法·均值算法·概率论
两千连弹7 天前
机器学习 ---线性回归
机器学习·回归·线性回归·sklearn