導航:首頁 > 源碼編譯 > 聚類演算法python實現

聚類演算法python實現

發布時間:2024-12-17 06:58:05

1. 減法聚類如何用python實現

下面是一個k-means聚類演算法在python2.7.5上面的具體實現,你需要先安裝Numpy和Matplotlib:
from numpy import *
import time
import matplotlib.pyplot as plt

# calculate Euclidean distance
def euclDistance(vector1, vector2):
return sqrt(sum(power(vector2 - vector1, 2)))
# init centroids with random samples
def initCentroids(dataSet, k):
numSamples, dim = dataSet.shape
centroids = zeros((k, dim))
for i in range(k):
index = int(random.uniform(0, numSamples))
centroids[i, :] = dataSet[index, :]
return centroids
# k-means cluster
def kmeans(dataSet, k):
numSamples = dataSet.shape[0]
# first column stores which cluster this sample belongs to,
# second column stores the error between this sample and its centroid
clusterAssment = mat(zeros((numSamples, 2)))
clusterChanged = True
## step 1: init centroids
centroids = initCentroids(dataSet, k)
while clusterChanged:
clusterChanged = False
## for each sample
for i in xrange(numSamples):
minDist = 100000.0
minIndex = 0
## for each centroid
## step 2: find the centroid who is closest
for j in range(k):
distance = euclDistance(centroids[j, :], dataSet[i, :])
if distance < minDist:
minDist = distance
minIndex = j

## step 3: update its cluster
if clusterAssment[i, 0] != minIndex:
clusterChanged = True
clusterAssment[i, :] = minIndex, minDist**2
## step 4: update centroids
for j in range(k):
pointsInCluster = dataSet[nonzero(clusterAssment[:, 0].A == j)[0]]
centroids[j, :] = mean(pointsInCluster, axis = 0)
print 'Congratulations, cluster complete!'
return centroids, clusterAssment
# show your cluster only available with 2-D data
def showCluster(dataSet, k, centroids, clusterAssment):
numSamples, dim = dataSet.shape
if dim != 2:
print "Sorry! I can not draw because the dimension of your data is not 2!"
return 1
mark = ['or', 'ob', 'og', 'ok', '^r', '+r', 'sr', 'dr', '<r', 'pr']
if k > len(mark):
print "Sorry! Your k is too large! please contact Zouxy"
return 1
# draw all samples
for i in xrange(numSamples):
markIndex = int(clusterAssment[i, 0])
plt.plot(dataSet[i, 0], dataSet[i, 1], mark[markIndex])
mark = ['Dr', 'Db', 'Dg', 'Dk', '^b', '+b', 'sb', 'db', '<b', 'pb']
# draw the centroids
for i in range(k):
plt.plot(centroids[i, 0], centroids[i, 1], mark[i], markersize = 12)
plt.show()

2. 深度盤點:一文詳解10種聚類演算法(附完整Python操作示例)

本文深入探討了多種聚類演算法,並提供了 Python 中的實現示例。聚類或聚類分析是一種無監督學習方法,用於揭示數據中的自然分組,適用於數據分析,如客戶細分。沒有單一最佳演算法,選擇取決於數據特性。


文章首先介紹了聚類的概念,解釋了其在數據分析中的應用,以及如何在 Python 中安裝和使用頂級聚類演算法。接下來,詳細介紹了10種流行的聚類演算法,並提供了每種演算法的使用示例。


以下是這10種聚類演算法的簡介:



文章還提供了每種演算法在 Python 中的實現示例,並展示了應用到合成數據集的結果。每種演算法的結果表明了它們在不同情況下的適應性。最後,文章總結了如何在 Python 中利用這些演算法進行聚類分析。


總結來說,本文提供了一個全面的指南,幫助讀者了解和應用多種聚類演算法,為數據分析和機器學習項目提供強大的工具。通過實踐示例,讀者可以更好地掌握這些演算法的使用方法,並根據具體需求選擇最適合的演算法。

閱讀全文

與聚類演算法python實現相關的資料

熱點內容
android表格demo 瀏覽:263
安卓怎麼讓相冊不顯示網頁 瀏覽:300
php文件系統源碼 瀏覽:713
易信java 瀏覽:486
北侖二級壓縮螺桿機 瀏覽:49
加密路線免費入口 瀏覽:744
計算器程序員是幹嘛的 瀏覽:362
數控編程里fo是啥鍵 瀏覽:660
javacdataxml 瀏覽:949
本機tcp伺服器地址 瀏覽:432
切換pdf 瀏覽:97
iturns下載的app在哪裡 瀏覽:646
雲起書院App在哪裡 瀏覽:620
怎麼對hex文件進行加密 瀏覽:291
pdf少年時 瀏覽:959
兩個同名文件夾同步 瀏覽:635
拍照文件夾哪個好用 瀏覽:96
文件夾和書包 瀏覽:516
解壓歸檔拿登記證 瀏覽:230
去體驗解壓房 瀏覽:618