240718 均值漂移聚类

结果

均值漂移:这些“模式”就对应于一群群局部最密集(local maxima) 分布的点。 均值
漂移算法的优点是它不需要事先确定集群的数量。

代码

# -*- coding: utf-8 -*-
import numpy as np
from sklearn.cluster import MeanShift, estimate_bandwidth

import utilities

# 加载数据
X = utilities.load_data('data_multivar.txt')

# 带宽估计
bandwidth = estimate_bandwidth(X, quantile=0.1, n_samples=len(X))

# 计算均值
meanshift_estimator = MeanShift(bandwidth=bandwidth, bin_seeding=True)
meanshift_estimator.fit(X)
labels = meanshift_estimator.labels_
centroids = meanshift_estimator.cluster_centers_
num_clusters = len(np.unique(labels))

print "Number of clusters in input data =", num_clusters

###########################################################
# 画出节点

import matplotlib.pyplot as plt
from itertools import cycle

plt.figure()

# 为不同的簇指定标记形状
markers = '.*xv'

for i, marker in zip(range(num_clusters), markers):
    # plot the points belong to the current cluster
    plt.scatter(X[labels==i, 0], X[labels==i, 1], marker=marker, color='k')

    # plot the centroid of the current cluster
    centroid = centroids[i]
    plt.plot(centroid[0], centroid[1], marker='o', markerfacecolor='k',
             markeredgecolor='k', markersize=15)

plt.title('Clusters and their centroids')
plt.show()
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值