網站首頁 編程語言 正文
聚類屬于無監督學習,K-means
算法是很典型的基于距離的聚類算法,采用距離作為相似性的評價指標,即認為兩個對象的距離越近,其相似度就越大。該算法認為簇是由距離靠近的對象組成的,因此把得到緊湊且獨立的簇作為最終目標。
下面來看看python實現k-means算法的詳細代碼吧:
# -*- coding:utf-8 -*- import random import numpy as np from matplotlib import pyplot class K_Means(object): ? ? # k是分組數;tolerance‘中心點誤差';max_iter是迭代次數 ? ? def __init__(self, k=2, tolerance=0.0001, max_iter=300): ? ? ? ? self.k_ = k ? ? ? ? self.tolerance_ = tolerance ? ? ? ? self.max_iter_ = max_iter ? ? def fit(self, data): ? ? ? ? self.centers_ = {} ? ? ? ? for i in range(self.k_): ? ? ? ? ? ? self.centers_[i] = data[random.randint(0,len(data))] ? ? ? ? # print('center', self.centers_) ? ? ? ? for i in range(self.max_iter_): ? ? ? ? ? ? self.clf_ = {} #用于裝歸屬到每個類中的點[k,len(data)] ? ? ? ? ? ? for i in range(self.k_): ? ? ? ? ? ? ? ? self.clf_[i] = [] ? ? ? ? ? ? # print("質點:",self.centers_) ? ? ? ? ? ? for feature in data: ? ? ? ? ? ? ? ? distances = [] #裝中心點到每個點的距離[k] ? ? ? ? ? ? ? ? for center in self.centers_: ? ? ? ? ? ? ? ? ? ? # 歐拉距離 ? ? ? ? ? ? ? ? ? ? distances.append(np.linalg.norm(feature - self.centers_[center])) ? ? ? ? ? ? ? ? classification = distances.index(min(distances)) ? ? ? ? ? ? ? ? self.clf_[classification].append(feature) ? ? ? ? ? ? # print("分組情況:",self.clf_) ? ? ? ? ? ? prev_centers = dict(self.centers_) ? ? ? ? ? ? for c in self.clf_: ? ? ? ? ? ? ? ? self.centers_[c] = np.average(self.clf_[c], axis=0) ? ? ? ? ? ? # '中心點'是否在誤差范圍 ? ? ? ? ? ? optimized = True ? ? ? ? ? ? for center in self.centers_: ? ? ? ? ? ? ? ? org_centers = prev_centers[center] ? ? ? ? ? ? ? ? cur_centers = self.centers_[center] ? ? ? ? ? ? ? ? if np.sum((cur_centers - org_centers) / org_centers * 100.0) > self.tolerance_: ? ? ? ? ? ? ? ? ? ? optimized = False ? ? ? ? ? ? if optimized: ? ? ? ? ? ? ? ? break ? ? def predict(self, p_data): ? ? ? ? distances = [np.linalg.norm(p_data - self.centers_[center]) for center in self.centers_] ? ? ? ? index = distances.index(min(distances)) ? ? ? ? return index if __name__ == '__main__': ? ? x = np.array([[1, 2], [1.5, 1.8], [5, 8], [8, 8], [1, 0.6], [9, 11]]) ? ? k_means = K_Means(k=2) ? ? k_means.fit(x) ? ? for center in k_means.centers_: ? ? ? ? pyplot.scatter(k_means.centers_[center][0], k_means.centers_[center][1], marker='*', s=150) ? ? for cat in k_means.clf_: ? ? ? ? for point in k_means.clf_[cat]: ? ? ? ? ? ? pyplot.scatter(point[0], point[1], c=('r' if cat == 0 else 'b')) ? ? predict = [[2, 1], [6, 9]] ? ? for feature in predict: ? ? ? ? cat = k_means.predict(feature) ? ? ? ? pyplot.scatter(feature[0], feature[1], c=('r' if cat == 0 else 'b'), marker='x') ? ? pyplot.show()
原文鏈接:https://blog.csdn.net/EMIvv/article/details/122403531
相關推薦
- 2022-11-05 關于Linux下動態查看實時日志的命令_linux shell
- 2022-03-15 antd-mobile 請求時Loading組件
- 2022-06-01 C#的通用DbHelper類(支持數據連接池)示例詳解_C#教程
- 2023-11-16 nvidia jetson設備(Jetson Nano, TX1/TX2,Xavier NX/AGX
- 2022-06-30 利用Python刪除電腦中重復文件的方法_python
- 2022-12-11 React?狀態的不變性實例詳解_React
- 2022-04-25 C語言實現線索二叉樹的前中后創建和遍歷詳解_C 語言
- 2022-06-09 ASP.NET?Core記錄日志_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支