python

15、分层聚类(与kmeans对比)以及连通性、

2020-04-13  本文已影响0人  羽天驿

一、分层聚类

1具体介绍

image.png

2距离的计算方式

3 与kmeans对比

4 连通性

image.png

代码:


import numpy as np

# 凝聚:自下而上
from sklearn.cluster import AgglomerativeClustering

from sklearn import datasets

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d.axes3d import Axes3D#三维图形
from sklearn.cluster import KMeans
d:\python3.7.4\lib\importlib\_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject
  return f(*args, **kwds)
X,y = datasets.make_swiss_roll(n_samples=1500,noise = 0.05)
fig = plt.figure()
a3 = Axes3D(fig)
a3.scatter(X[:,0],X[:,1],X[:,2],c = y)
a3.view_init(10,-80)
output_1_0.png
agg = AgglomerativeClustering(n_clusters=6,linkage='ward')# 最近的距离,作为标准,
agg.fit(X)
y_ = agg.labels_
fig = plt.figure()
a3 = Axes3D(fig)
a3.scatter(X[:,0],X[:,1],X[:,2],c = y_)
a3.view_init(10,-80)
output_2_0.png
# Kmeans只负责分类,随机性,类别是数字几,不固定
clf = KMeans(n_clusters=6)
clf.fit(X)
y_ = clf.labels_
fig = plt.figure()
a3 = Axes3D(fig)
a3.scatter(X[:,0],X[:,1],X[:,2],c = y_)
a3.view_init(10,-80)
output_3_0.png

分层聚类中连通性约束

from sklearn.neighbors import kneighbors_graph# graph图形的意思
## 调整数据
X[:,1] *= 0.5 #Y轴方向上的距离缩小了一半,距离变近、变小

什么时候,用连通性约束呢???

尝试一下,用和不用哪个好!

import warnings
warnings.filterwarnings('ignore')
# 邻居数量变少,认为,条件宽松
conn = kneighbors_graph(X,n_neighbors=10) #采用邻居,进行约束
agg = AgglomerativeClustering(n_clusters=6,connectivity=conn,linkage='ward')# 最近的距离,作为标准,
agg.fit(X)
y_ = agg.labels_
fig = plt.figure()
a3 = Axes3D(fig)
a3.scatter(X[:,0],X[:,1],X[:,2],c = y_)
a3.view_init(10,-80)
output_9_0.png
for i in range(6):
    print(i,(y_ == i).sum())
0 1489
1 2
2 2
3 3
4 2
5 2
# 没有连接性约束结果
agg = AgglomerativeClustering(n_clusters=6,linkage='ward')# 最近的距离,作为标准,
agg.fit(X)
y_ = agg.labels_
fig = plt.figure()
a3 = Axes3D(fig)
a3.scatter(X[:,0],X[:,1],X[:,2],c = y_)
a3.view_init(10,-80)
output_11_0.png
# Kmeans只负责分类,随机性,类别是数字几,不固定
clf = KMeans(n_clusters=6)
clf.fit(X)
y_ = clf.labels_
fig = plt.figure()
a3 = Axes3D(fig)
a3.scatter(X[:,0],X[:,1],X[:,2],c = y_)
a3.view_init(10,-80)
output_12_0.png
上一篇下一篇

猜你喜欢

热点阅读