Python實(shí)現(xiàn)k-means算法
本文實(shí)例為大家分享了Python實(shí)現(xiàn)k-means算法的具體代碼,供大家參考,具體內(nèi)容如下
這也是周志華《機(jī)器學(xué)習(xí)》的習(xí)題9.4。
數(shù)據(jù)集是西瓜數(shù)據(jù)集4.0,如下
編號(hào),密度,含糖率
1,0.697,0.46
2,0.774,0.376
3,0.634,0.264
4,0.608,0.318
5,0.556,0.215
6,0.403,0.237
7,0.481,0.149
8,0.437,0.211
9,0.666,0.091
10,0.243,0.267
11,0.245,0.057
12,0.343,0.099
13,0.639,0.161
14,0.657,0.198
15,0.36,0.37
16,0.593,0.042
17,0.719,0.103
18,0.359,0.188
19,0.339,0.241
20,0.282,0.257
21,0.784,0.232
22,0.714,0.346
23,0.483,0.312
24,0.478,0.437
25,0.525,0.369
26,0.751,0.489
27,0.532,0.472
28,0.473,0.376
29,0.725,0.445
30,0.446,0.459
算法很簡(jiǎn)單,就不解釋了,代碼也不復(fù)雜,直接放上來(lái):
# -*- coding: utf-8 -*- """Excercise 9.4""" import numpy as np import pandas as pd import matplotlib.pyplot as plt import sys import random data = pd.read_csv(filepath_or_buffer = '../dataset/watermelon4.0.csv', sep = ',')[["密度","含糖率"]].values ########################################## K-means ####################################### k = int(sys.argv[1]) #Randomly choose k samples from data as mean vectors mean_vectors = random.sample(data,k) def dist(p1,p2): return np.sqrt(sum((p1-p2)*(p1-p2))) while True: print mean_vectors clusters = map ((lambda x:[x]), mean_vectors) for sample in data: distances = map((lambda m: dist(sample,m)), mean_vectors) min_index = distances.index(min(distances)) clusters[min_index].append(sample) new_mean_vectors = [] for c,v in zip(clusters,mean_vectors): new_mean_vector = sum(c)/len(c) #If the difference betweenthe new mean vector and the old mean vector is less than 0.0001 #then do not updata the mean vector if all(np.divide((new_mean_vector-v),v) < np.array([0.0001,0.0001]) ): new_mean_vectors.append(v) else: new_mean_vectors.append(new_mean_vector) if np.array_equal(mean_vectors,new_mean_vectors): break else: mean_vectors = new_mean_vectors #Show the clustering result total_colors = ['r','y','g','b','c','m','k'] colors = random.sample(total_colors,k) for cluster,color in zip(clusters,colors): density = map(lambda arr:arr[0],cluster) sugar_content = map(lambda arr:arr[1],cluster) plt.scatter(density,sugar_content,c = color) plt.show()
運(yùn)行方式:在命令行輸入 python k_means.py 4。其中4就是k。
下面是k分別等于3,4,5的運(yùn)行結(jié)果,因?yàn)橐婚_(kāi)始的均值向量是隨機(jī)的,所以每次運(yùn)行結(jié)果會(huì)有不同。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- k-means 聚類(lèi)算法與Python實(shí)現(xiàn)代碼
- 在Python中使用K-Means聚類(lèi)和PCA主成分分析進(jìn)行圖像壓縮
- 解決python -m pip install --upgrade pip 升級(jí)不成功問(wèn)題
- python實(shí)現(xiàn)鳶尾花三種聚類(lèi)算法(K-means,AGNES,DBScan)
- Python機(jī)器學(xué)習(xí)之K-Means聚類(lèi)實(shí)現(xiàn)詳解
- 詳解K-means算法在Python中的實(shí)現(xiàn)
- Python -m參數(shù)原理及使用方法解析
相關(guān)文章
python+selenium+PhantomJS抓取網(wǎng)頁(yè)動(dòng)態(tài)加載內(nèi)容
一般我們使用python的第三方庫(kù)requests及框架scrapy來(lái)爬取網(wǎng)上的資源,但是設(shè)計(jì)javascript渲染的頁(yè)面卻不能抓取,此 時(shí),我們使用web自動(dòng)化測(cè)試化工具Selenium+無(wú)界面瀏覽器PhantomJS來(lái)抓取javascript渲染的頁(yè)面,下面實(shí)現(xiàn)一個(gè)簡(jiǎn)單的爬取2020-02-02Python3 + Appium + 安卓模擬器實(shí)現(xiàn)APP自動(dòng)化測(cè)試并生成測(cè)試報(bào)告
這篇文章主要介紹了Python3 + Appium + 安卓模擬器實(shí)現(xiàn)APP自動(dòng)化測(cè)試并生成測(cè)試報(bào)告,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01淺談pytorch池化maxpool2D注意事項(xiàng)
今天小編就為大家分享一篇淺談pytorch池化maxpool2D注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02python 實(shí)現(xiàn)刪除文件或文件夾實(shí)例詳解
這篇文章主要介紹了python 實(shí)現(xiàn)刪除文件或文件夾實(shí)例詳解的相關(guān)資料,這里附有實(shí)例代碼,需要的朋友可以參考下2016-12-12Python Pandas模塊實(shí)現(xiàn)數(shù)據(jù)的統(tǒng)計(jì)分析的方法
在上一篇講了幾個(gè)常用的“Pandas”函數(shù)之后,今天小編就為大家介紹一下在數(shù)據(jù)統(tǒng)計(jì)分析當(dāng)中經(jīng)常用到的“Pandas”函數(shù)方法,希望能對(duì)大家有所收獲,需要的朋友可以參考下2021-06-06postman模擬訪問(wèn)具有Session的post請(qǐng)求方法
今天小編就為大家分享一篇postman模擬訪問(wèn)具有Session的post請(qǐng)求方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07matplotlib繪制多子圖共享鼠標(biāo)光標(biāo)的方法示例
這篇文章主要介紹了matplotlib繪制多子圖共享鼠標(biāo)光標(biāo)的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01