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

热点内容
越南离北京源码 浏览:639
服装展示网站源码 浏览:325
编译器过度优化线 浏览:689
安卓怎么边浏览边录视频 浏览:653
分支限界java 浏览:389
phpdiscuz登录 浏览:182
epr服务器50人要什么配置 浏览:780
云服务器ecs如何转移 浏览:558
android仿应用宝 浏览:379
php连接mysql数据库的方法 浏览:109
螺杆制冷压缩机油镜怎样判断 浏览:126
本地电脑如何做服务器配置 浏览:860
如何对app的版权进行保护 浏览:822
小孩子用什么app软件 浏览:733
androidksoap上传 浏览:464
服务器掉线是什么原因 浏览:33
迷你世界怎么看服务器被炸 浏览:324
和平精英上线服务器是什么 浏览:941
云服务器显存分配 浏览:453
信捷plc编程电缆 浏览:145