导航:首页 > 源码编译 > 聚类算法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实现相关的资料

热点内容
base64encoderjava 浏览:482
linux视频转换 浏览:694
linux与unixshell编程指南 浏览:131
vb6反编译程序错误 浏览:120
服务器弄挂有什么影响 浏览:300
安卓软件为什么运行不了 浏览:524
什么安卓手机是四方的 浏览:477
云服务器厂商租用多少钱 浏览:353
平安车贷解压要多久 浏览:416
乐一php程序员待遇 浏览:164
ZDM命令无法识别 浏览:922
解压小手工A4纸 浏览:983
钢筋加密区是几倍 浏览:671
编译程序代码软件 浏览:808
怎么恢复加密的东西 浏览:989
程序员卖茶 浏览:699
后端程序员英文 浏览:365
滴滴程序员平均月薪 浏览:593
如何使用ftp命令 浏览:790
小书亭下载的文件在哪手机文件夹 浏览:184