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
    点赞
  • 106
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
聚类算法是一常用的无监督学习方法,可以将数据集分成若干个组,每个组内的数据具有相似性。下面是一些常用的聚类算法及对应实例代码。 1. K-Means算法 K-Means算法是一基于距离的聚类算法,其核心思想是将数据集分成K个簇,使得每个数据点都属于离其最近的簇。K-Means算法的步骤如下: 1. 随机选择K个质心(簇中心)。 2. 将每个数据点分配到距离其最近的质心所在的簇中。 3. 重新计算每个簇的质心。 4. 重复步骤2和3,直到簇中心不再改变或达到最大迭代次数。 下面是K-Means算法的Python实现代码: ```python from sklearn.cluster import KMeans # 创建数据集 X = [[1, 2], [1, 4], [1, 0], [4, 2], [4, 4], [4, 0]] # 创建K-Means模型 kmeans = KMeans(n_clusters=2) # 训练模型 kmeans.fit(X) # 预测结果 y_pred = kmeans.predict(X) # 输出聚类结果 print(y_pred) ``` 2. 层次聚类算法 层次聚类算法是一划分聚类算法,其核心思想是从单个数据点开始,将最相似的点组合成一个簇,逐步合并成更大的簇,直到所有数据点都被合并到同一个簇中。层次聚类算法有两方式:自下而上的聚合和自上而下的分裂。下面是自下而上的聚合层次聚类算法Python实现代码: ```python from scipy.cluster.hierarchy import dendrogram, linkage import matplotlib.pyplot as plt # 创建数据集 X = [[1, 2], [1, 4], [1, 0], [4, 2], [4, 4], [4, 0]] # 计算距离矩阵 Z = linkage(X, 'ward') # 绘制树状图 plt.figure(figsize=(10, 5)) dendrogram(Z) plt.show() ``` 3. DBSCAN算法 DBSCAN算法是一基于密度的聚类算法,其核心思想是将密度相连的数据点划分为同一个簇。DBSCAN算法的步骤如下: 1. 选择一个未访问的数据点。 2. 如果该点周围的密度达到预设的阈值,则将其作为一个新的簇的中心点,并将其密度可达的所有点加入该簇。 3. 重复步骤2,直到所有数据点都被访问。 下面是DBSCAN算法的Python实现代码: ```python from sklearn.cluster import DBSCAN # 创建数据集 X = [[1, 2], [1, 4], [1, 0], [4, 2], [4, 4], [4, 0]] # 创建DBSCAN模型 dbscan = DBSCAN(eps=1, min_samples=2) # 训练模型 dbscan.fit(X) # 预测结果 y_pred = dbscan.labels_ # 输出聚类结果 print(y_pred) ``` 以上就是几常用的聚类算法及对应实例代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值