当前位置: 首页 > news >正文

[机器学习]基于K-means聚类算法的鸢尾花数据及分类

基于Kmeans,对鸢尾花数据集前两个特征进行聚类分析

  • 通过迭代优化,将150个样本划分到K个簇中。

  • 目标函数:最小化所有样本到其所属簇中心的距离平方和。

  • 算法步骤:

    1. 随机初始化K个簇中心。

    2. 将每个样本分配到最近的中心。

    3. 计算均值确定每个簇的中心(均值)。

    4. 重复第2和3步直到稳定收敛。

程序代码:

import mathimport numpy as np
from matplotlib import pyplot as plt
from sklearn import datasetsplt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = Falsedata = datasets.load_iris().data
labels = datasets.load_iris().target
print('数据维度',data.shape)
features = data[:,: 2]
print('特征',features)num_clusters = 6
epoch = 150
J_sum = []def J_calculate(features,divide_re,center):J = 0for s1 in range(150):distances = ((features[s1][0]-center[divide_re[s1]][0]) ** 2) + ((features[s1][1]-center[divide_re[s1]][1]) ** 2)#print(distances)J = J + distancesreturn Jdef decision(features,divide_re,center,epoch):J_best = []for _ in range(epoch):J_b = math.inffor s1 in range(150):best = Nonemin_J_now = math.inffor s2 in range(len(center)):divide_re[s1] = s2J_now = J_calculate(features,divide_re,center)if J_now < min_J_now:min_J_now = J_nowbest = s2divide_re[s1] = bestfor i in range(len(center)):xc = []yc = []for j in range(150):if (divide_re[j] == i):xc.append(features[j][0])yc.append(features[j][1])center[i] = [np.mean(xc), np.mean(yc)]if(min_J_now<J_b):J_b = min_J_nowJ_best.append(J_b)return features,divide_re,center,J_bestfor i in range(2,num_clusters+1):print(f'\n分{i}类:\n')center = features[np.random.choice(features.shape[0], i, replace=False)]print("初始中心点", center)distances = np.linalg.norm(features[:, np.newaxis, :] - center, axis=2)divide = np.argmin(distances,axis=1)divide_re = []for x in range(150):divide_re.append(divide[x])print("初始样本分类", divide_re)features,divide_re,center,J_best = decision(features,divide_re,center,epoch)print(f'{i}类最佳J值为:',J_best[epoch-1])J_sum.append(J_best[epoch-1])plt.scatter(features[:, 0], features[:, 1], c=divide_re, cmap='viridis', edgecolors='k')plt.scatter(center[:, 0], center[:, 1], marker='x', s=30, linewidths=3, color='red')plt.title(f'{i}类C均值分类法结果')plt.xlabel('第一特征')plt.ylabel('第二特征')plt.show()
plt.figure()
plt.plot(range(2, num_clusters + 1), J_sum, marker='o')
plt.title('J与类别数量关系曲线')
plt.xlabel('类别数量')
plt.ylabel('J_sum 值')
plt.show()

运行结果:

数据维度 (150, 4)
特征 [[5.1 3.5]
[4.9 3. ]
[4.7 3.2]
[4.6 3.1]
[5.  3.6]
[5.4 3.9]
[4.6 3.4]
[5.  3.4]
[4.4 2.9]
[4.9 3.1]
[5.4 3.7]
[4.8 3.4]
[4.8 3. ]
[4.3 3. ]
[5.8 4. ]
[5.7 4.4]
[5.4 3.9]
[5.1 3.5]
[5.7 3.8]
[5.1 3.8]
[5.4 3.4]
[5.1 3.7]
[4.6 3.6]
[5.1 3.3]
[4.8 3.4]
[5.  3. ]
[5.  3.4]
[5.2 3.5]
[5.2 3.4]
[4.7 3.2]
[4.8 3.1]
[5.4 3.4]
[5.2 4.1]
[5.5 4.2]
[4.9 3.1]
[5.  3.2]
[5.5 3.5]
[4.9 3.6]
[4.4 3. ]
[5.1 3.4]
[5.  3.5]
[4.5 2.3]
[4.4 3.2]
[5.  3.5]
[5.1 3.8]
[4.8 3. ]
[5.1 3.8]
[4.6 3.2]
[5.3 3.7]
[5.  3.3]
[7.  3.2]
[6.4 3.2]
[6.9 3.1]
[5.5 2.3]
[6.5 2.8]
[5.7 2.8]
[6.3 3.3]
[4.9 2.4]
[6.6 2.9]
[5.2 2.7]
[5.  2. ]
[5.9 3. ]
[6.  2.2]
[6.1 2.9]
[5.6 2.9]
[6.7 3.1]
[5.6 3. ]
[5.8 2.7]
[6.2 2.2]
[5.6 2.5]
[5.9 3.2]
[6.1 2.8]
[6.3 2.5]
[6.1 2.8]
[6.4 2.9]
[6.6 3. ]
[6.8 2.8]
[6.7 3. ]
[6.  2.9]
[5.7 2.6]
[5.5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1, 1, 5, 5, 1, 5, 4, 4, 0, 0, 5, 1, 1, 1, 1, 1, 1, 3, 4, 1, 5, 1, 1, 5, 1, 1, 1, 5, 1, 5, 1, 1, 3, 1, 0, 5, 4, 0, 2, 0, 0, 4, 4, 0, 0, 1, 1, 4, 4, 0, 0, 1, 0, 1, 0, 5, 4, 0, 5, 5, 0, 0, 0, 0, 0, 5, 1, 0, 3, 4, 5, 0, 4, 0, 1, 4, 4, 0, 1, 4, 3, 5]
6类最佳J值为: 18.150987445152886

进程已结束,退出代码0

http://www.xdnf.cn/news/1393525.html

相关文章:

  • 动态环境下的人员感知具身导航!HA-VLN:具备动态多人互动的视觉语言导航基准与排行榜
  • gpu与cpu各厂商的优劣
  • 吱吱企业通讯软件可私有化部署,构建安全可控的通讯办公平台
  • [TG开发]与Reids集成
  • 深度拆解 OpenHarmony 位置服务子系统:从 GNSS 到分布式协同定位的全链路实战
  • LeetCode - 1. 两数之和
  • 华硕主板 BIOS 提示——GPT header corruption has been detected
  • 【树论】树上启发式合并
  • RV1126的OSD模块讲解
  • 单节锂电池,升压至12V/2.5A的芯片选型指南(附FP5207X 与 FP7208参数表)
  • Zabbix-获取客户端的SNMP数据
  • CMake⼯程指南-2
  • Linux系统——EXT2 文件系统
  • 记录:HSD部署(未完成)
  • 基于 Vue + Interact.js 实现可拖拽缩放柜子设计器
  • CosyVoice win10启用记
  • 《Shell 大道:筑基篇(下)—— 流控筑根基,数据任驱驰》
  • 【K8s】整体认识K8s之与集群外部访问--service
  • 【Doris入门】Doris表模型完全指南:明细、聚合、主键模型的选择与优化技巧
  • layout版图设计学习笔记4_Virtuoso基础介绍
  • Altium Designer 22使用笔记(10)---PCB铺铜相关操作
  • 如何解决网关断网后时间不再统计的问题?无RTC子设备如何打通主网关的时间同步功能?
  • Unity EventTrigger 动态添加事件
  • 电商数据的获取方式:API、爬虫、第三方服务及更多
  • 如何使用 Graylog 连接 Easysearch
  • Nginx原理及九大常见故障解决方法
  • idea2025.1.5安装+pj
  • MemoryVLA:让机器人拥有“记忆“的视觉-语言-动作模型
  • 3021. Alice 和 Bob 玩鲜花游戏
  • JSTL!