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

聚类_K均值

import numpy as np
import matplotlib.pyplot as plt 
from sklearn.datasets import make_blobs

1.数据预处理

#创建基于高斯分布的样本点, x是点的坐标,y是所属聚类值
x, y = make_blobs(n_samples=100, centers=6, random_state=100, cluster_std=0.6)
# 设置图形尺寸,单位英寸
plt.figure(figsize=(6,6))
plt.scatter(x[:,0], x[:,1],c = y)
plt.show
<function matplotlib.pyplot.show(close=None, block=None)>

在这里插入图片描述

2.模型实现

from scipy.spatial.distance import cdistclass KMeansModel():#参数k聚类数, 最大迭代次数,初始质心def __init__(self, k_cluster=6, max_iteration=100, centroids=[]):self.k_cluster = k_clusterself.max_iteration = max_iterationself.centroids = np.array(centroids, dtype = np.float32)def fit(self, points):# 随机选取初始质心点if(self.centroids.shape==(0,)):self.centroids = points[np.random.randint(0, points.shape[0], self.k_cluster), :]for i in range(self.max_iteration):#计算所有测试点和所有质心的距离,返回100*6的矩阵distances = cdist(points, self.centroids)#选取行方向最小的书作为测试点的质心c_index = np.argmin(distances, axis=1)if(i == 0):print("c shape", c_index.shape,c_index[0])#计算每类数据的均值作为新的质心for i in range(self.k_cluster):if i in c_index:self.centroids[i] = np.mean(points[c_index == i], axis=0)def predict(self, points):distances = cdist(points, self.centroids)#选取距离最近的质心作为分类c_index = np.argmin(distances, axis=1)return c_index

3.测试

def plot_kmeans(x, y, centroids, subplot):plt.subplot(subplot)plt.scatter(x[:,0], x[:,1], c=y)plt.scatter(centroids[:,0], centroids[:,1],s=100,c='r')# 训练
kmean_model = KMeansModel(centroids=np.array([[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]]))
plt.figure(figsize=(18,8))
plot_kmeans(x, y, kmean_model.centroids, 121)kmean_model.fit(x)
print(kmean_model.centroids)
plot_kmeans(x, y, kmean_model.centroids, 122)#预测
x_new = np.array([[10,7],[0,0]])
y_predict = kmean_model.predict(x_new)
print("predict y ", y_predict)
plt.scatter(x_new[:,0],x_new[:,1],s=100, c= "black")
c shape (100,) 0
[[ 4.343336  -5.112518 ][-1.6609049  6.7436223][-8.57988   -3.3460388][ 2.7469435  6.05025  ][ 2.490612   7.7450833][ 4.1287684  6.6914167]]
predict y  [5 3]<matplotlib.collections.PathCollection at 0x1576e5a9850>

在这里插入图片描述

http://www.lryc.cn/news/438816.html

相关文章:

  • Mac电脑剪切板在哪里找 苹果电脑剪切板打开教程【详解】
  • Python编程 - 三器一包
  • InternVL 多模态模型部署微调实践
  • Ruby Dir 类和方法
  • C++STL~~deque
  • SpringCloud的学习,Consul服务注册与发现、分布式配置,以及 服务调用和负载均衡
  • 闯关leetcode——26. Remove Duplicates from Sorted Array
  • 基于A2C与超启发式的航天器星载自主任务规划算法-笔记
  • [机器学习]决策树
  • CentOS7更换阿里云yum更新源
  • 算法参数对拥塞控制的影响
  • Go websocket
  • C# 委托与事件 观察者模式
  • K8S - 用service account 登陆kubectl
  • Redis 持久化机制详解
  • 小阿轩yx-案例:Zabbix监控kubernetes云原生环境
  • 量化交易的个人见解
  • Java集合(一)
  • 车载软件架构 --- SOA设计与应用(下)
  • 网络原理 IP协议与以太网协议
  • k8s的安装
  • Qt中样式表常用的属性名称定义
  • React源码学习(一):如何学习React源码
  • 云计算服务的底层,虚拟化技术的实现原理
  • 大数据Flink(一百一十六):Flink SQL的时间属性
  • Ansible自动化部署kubernetes集群
  • 网络通信流程
  • 数据结构一:绪论
  • 使用OpenFeign在不同微服务之间传递用户信息时失败
  • js中【Worker】相关知识点详细解读