10 种 Python 聚类算法及python实现

10 种 Python 聚类算法及python实现

10 种 Python 聚类算法及python实现

聚类分析的定义

聚类分析是一种无监督的机器学习任务,从现有的数据实现对数据的自然分组,在特征空间中找到群组,只解释输入变量,不对数据进行预测。
聚类的结果往往是特征空间的密度区域,来自于群组的示例比其他样本点更接近于质心,可以有边界或者范围。

聚类分析解决的问题

1、基于行为发现客户群;
2、将正常数据与异常值和异常行为分开;
3、可以用作市场细分或者用户细分;
4、聚类还可用作特征工程的类型,其中现有的和新的示例可被映射并标记为属于数据中所标识的群集之一。

聚类算法的实现

聚类算法通过计算特征空间的示例之间的相似度或者距离,来发现密集的观测区域,实现对群集的识别;
聚类分析是一个迭代过程,在该过程中,对所识别的群集的主观评估被反馈回算法配置的改变中,直到达到期望的或适当的结果。
scikit-learn 提供一套不同的聚类算法供选择。其中10种比较流行的聚类算法如下:
1、亲和力传播
2、聚合聚类
3、BIRCH
4、DBSCAN
5、K-均值
6、Mini-Batch K-均值
7、Mean Shift
8、OPTICS
9、光谱聚类
10、高斯混合

创建聚类数据集

使用make_classification()创建二分类的数据集

#创建数据集
from numpy import where
from sklearn.datasets import make_classification
from matplotlib import pyplot
#定义数据集
x,y=make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4)
#为每个类的样本创建散点图
for class_value in range(2):
    row_ix=where(y==class_value)#创建这些散点图的分布
    pyplot.scatter(x[row_ix,0],x[row_ix,1])
#绘制散点图
pyplot.show()

亲和力传播

#亲和力传播聚类
from numpy import unique
from numpy import where
from sklearn.datasets import make_classification
from sklearn.cluster import AffinityPropagation
from matplotlib import pyplot
#定义数据集
x,_=make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4)
#定义模型
model = AffinityPropagation(damping=0.9)
#匹配模型
model.fit(x)
#为每个示例分配一个集群
yhat = model.predict(x)
#检索唯一集群
clusters=unique(yhat)
#为每个集群的样本创建散点图
for cluster in clusters:
    row_ix = where(yhat==cluster)
    pyplot.scatter(x[row_ix,0],x[row_ix,1])
#绘制散点图
pyplot.show()

聚合聚类

#聚合聚类
from numpy import unique
from numpy import where
from sklearn.datasets import make_classification
from sklearn.cluster import AgglomerativeClustering
from matplotlib import pyplot
#定义数据集
x,_=make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4)
#定义模型
model = AgglomerativeClustering(n_clusters=2)
#匹配模型
model.fit(x)
#为每个示例分配一个集群
yhat = model.fit_predict(x)
#检索唯一集群
clusters=unique(yhat)
#为每个集群的样本创建散点图
for cluster in clusters:
    row_ix = where(yhat==cluster)
    pyplot.scatter(x[row_ix,0],x[row_ix,1])
#绘制散点图
pyplot.show()

BIRCH聚类,迭代的动态的形成聚类的层次结构

递增地和动态地群集传入的多维度量数据点,以尝试利用可用资源(即可用内存和时间约束)产生最佳质量的聚类.
#bitch聚类,迭代的动态调整聚类的层次结构
from numpy import unique
from numpy import where
from sklearn.datasets import make_classification
from sklearn.cluster import Birch
from matplotlib import pyplot
#定义数据集
x,_=make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4)
#定义模型
model = Birch(threshold=0.01,n_clusters=2)
#匹配模型
model.fit(x)
#为每个示例分配一个集群
yhat = model.predict(x)
#检索唯一集群
clusters=unique(yhat)
#为每个集群的样本创建散点图
for cluster in clusters:
    row_ix = where(yhat==cluster)
    pyplot.scatter(x[row_ix,0],x[row_ix,1])
#绘制散点图
pyplot.show()

DBSCAN:基于密度的空间聚类

在域中寻找高密度区域,并将其周围的特征空间区域扩展为群集。
#DBSCAN聚类,基于密度的空间聚类
from numpy import unique
from numpy import where
from sklearn.datasets import make_classification
from sklearn.cluster import DBSCAN
from matplotlib import pyplot
#定义数据集
x,_=make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4)
#定义模型
model = DBSCAN(eps=0.3,min_samples=10)
#为每个示例分配一个集群
yhat = model.fit_predict(x)
#检索唯一集群
clusters=unique(yhat)
#为每个集群的样本创建散点图
for cluster in clusters:
    row_ix = where(yhat==cluster)
    pyplot.scatter(x[row_ix,0],x[row_ix,1])
#绘制散点图
pyplot.show()

KMeans聚类

#KMeans聚类
from numpy import unique
from numpy import where
from sklearn.datasets import make_classification
from sklearn.cluster import KMeans
from matplotlib import pyplot
#定义数据集
x,_=make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4)
#定义模型
model = KMeans(n_clusters=2)
#匹配模型
model.fit(x)
#为每个示例分配一个集群
yhat = model.predict(x)
#检索唯一集群
clusters=unique(yhat)
#为每个集群的样本创建散点图
for cluster in clusters:
    row_ix = where(yhat==cluster)
    pyplot.scatter(x[row_ix,0],x[row_ix,1])
#绘制散点图
pyplot.show()

mini-batch KMeans聚类

#mini-batch KMeans聚类
from numpy import unique
from numpy import where
from sklearn.datasets import make_classification
from sklearn.cluster import MiniBatchKMeans
from matplotlib import pyplot
#定义数据集
x,_=make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4)
#定义模型
model = MiniBatchKMeans(n_clusters=2)
#匹配模型
model.fit(x)
#为每个示例分配一个集群
yhat = model.predict(x)
#检索唯一集群
clusters=unique(yhat)
#为每个集群的样本创建散点图
for cluster in clusters:
    row_ix = where(yhat==cluster)
    pyplot.scatter(x[row_ix,0],x[row_ix,1])
#绘制散点图
pyplot.show()

均值漂移聚类:根据特征空间中的实例密度来寻找和调整质心。

#均值漂移聚类
from numpy import unique
from numpy import where
from sklearn.datasets import make_classification
from sklearn.cluster import MeanShift
from matplotlib import pyplot
#定义数据集
x,_=make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4)
#定义模型
model = MeanShift()
#匹配模型
model.fit(x)
#为每个示例分配一个集群
yhat = model.predict(x)
#检索唯一集群
clusters=unique(yhat)
#为每个集群的样本创建散点图
for cluster in clusters:
    row_ix = where(yhat==cluster)
    pyplot.scatter(x[row_ix,0],x[row_ix,1])
#绘制散点图
pyplot.show()

OPTICS聚类,基于密度的聚类结构的数据库的增强排序

#OPTICS聚类,基于密度的聚类结构的数据库的增强排序
from numpy import unique
from numpy import where
from sklearn.datasets import make_classification
from sklearn.cluster import OPTICS
from matplotlib import pyplot
#定义数据集
x,_=make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4)
#定义模型
model = OPTICS(eps=0.8,min_samples=9)
#为每个示例分配一个集群
yhat = model.fit_predict(x)
#检索唯一集群
clusters=unique(yhat)
#为每个集群的样本创建散点图
for cluster in clusters:
    row_ix = where(yhat==cluster)
    pyplot.scatter(x[row_ix,0],x[row_ix,1])
#绘制散点图
pyplot.show()

光谱聚类

#光谱聚类,
from numpy import unique
from numpy import where
from sklearn.datasets import make_classification
from sklearn.cluster import SpectralClustering
from matplotlib import pyplot
#定义数据集
x,_=make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4)
#定义模型
model = SpectralClustering(n_clusters=2)
#为每个示例分配一个集群
yhat = model.fit_predict(x)
#检索唯一集群
clusters=unique(yhat)
#为每个集群的样本创建散点图
for cluster in clusters:
    row_ix = where(yhat==cluster)
    pyplot.scatter(x[row_ix,0],x[row_ix,1])
#绘制散点图
pyplot.show()

高斯混合模型,总结了一个多变量概率密度函数,混合了高斯概率分布

#高斯混合模型,总结了一个多变量概率密度函数,顾名思义就是混合了高斯概率分布
from numpy import unique
from numpy import where
from sklearn.datasets import make_classification
from sklearn.mixture import GaussianMixture
from matplotlib import pyplot
#定义数据集
x,_=make_classification(n_samples=1000,n_features=2,n_informative=2,n_redundant=0,n_clusters_per_class=1,random_state=4)
#定义模型
model = GaussianMixture(n_components=2)
#为每个示例分配一个集群
yhat = model.fit_predict(x)
#检索唯一集群
clusters=unique(yhat)
#为每个集群的样本创建散点图
for cluster in clusters:
    row_ix = where(yhat==cluster)
    pyplot.scatter(x[row_ix,0],x[row_ix,1])
#绘制散点图
pyplot.show()
  • 5
    点赞
  • 103
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值