Lab - Agglomerative

1 Hierarchical Clustering

1.1 Input Data

# Import the standard module(s) to be used in this lab
import pandas as pd
import numpy as np
%matplotlib inline
cust_pd = pd.read_csv("customers.csv")
cust_pd.head()

1.2 Clustering using Hierarchical Agglomerative Clustering

让我们把这些数据画出来,看看我们是否能看到任何群集。为此,我们将使用散点图Seaborn图书馆。Seaborn是一个基于matplotlib的Python数据可视化库。首先,我们导入matplotlib和seaborn库。然后我们创建一个数字,并确定他的数字画出散点图。你能看到多少个集群?

# set the backend of matplotlib to 'inline' to display the plot within Jupyter␣
,→notebook
from matplotlib import pyplot as plt
import seaborn as sns
plt.figure(figsize=(8,5))
sns.scatterplot(x=cust_pd["Income"], y=cust_pd["Debt"])

 正如我们所看到的,属性收入和债务是在非常不同的范围,收入是关于10-20倍的债务。因此,在进行聚类之前,我们先对数据进行归一化处理(0 - 1)。在这里,我们通过使用min()和max()函数手动进行规范化。我们还可以用标准化数据的MinMaxScaler。

 cust_pd["Income"] = (cust_pd["Income"] - cust_pd["Income"].min()) /␣
,→(cust_pd["Income"].max() - cust_pd["Income"].min())
cust_pd["Debt"] = (cust_pd["Debt"] - cust_pd["Debt"].min()) / (cust_pd["Debt"].
,→max() - cust_pd["Debt"].min())
cust_pd.head()
#from sklearn.preprocessing import MinMaxScaler
#scaler = MinMaxScaler()
#cust_pd = scaler.fit_transform(cust_pd)
#cust_pd = pd.DataFrame(cust_pd, columns=["Income", "Debt"])
#cust_pd.head()

现在让我们从Scipy导入树状图来创建一个树状图。

linkage method 1. method: str - single, complete, average, ward 2. metric: str - euclidean, cityblock, cosine, hamming (refer to scipy.spatial.distance.pdist) dendrogram method 1. orientation: str, optional - The direction to plot the dendrogram, which can be any of the following strings: - ‘top’: Plots the root at the top, and plot descendent links going downwards. (default). - ‘bottom’: Plots the root at the bottom, and plot descendent links going upwards. - ‘left’: Plots the root at the left, and plot descendent links going right. - ‘right’: Plots the root at the right, and plot descendent links going left.

from scipy.cluster.hierarchy import dendrogram, linkage
linked = linkage(cust_pd, 'complete')
dendrogram(linked, orientation='top', distance_sort='ascending',
show_leaf_counts=False)
plt.show()

输出如下: 

 我们用完全连锁法建立了一个树状图。如你所见,树状图划分数据分为三组,分别用绿色、红色和蓝色表示。我们可以指定切割阈值,这样数据将被划分到所需的集群中。为了指定切割阈值,我们传递一个值给参数color_threshold。节点之间距离大于或等于的所有连接阈值为蓝色。让我们指定0.5作为切割阈值。

dendrogram(linked, orientation='top', distance_sort='ascending',
show_leaf_counts=False, color_threshold=0.5)
plt.show()

输出如下: 

 

如您所见,距离大于或等于0.5的链接是蓝色的。七个集群由绿色、红色、浅蓝色、紫色、黄色、黑色和绿色表示。试着用不同的链接,看看数据将如何聚集在一起。让我们使用Agglomerative Clustering对数据进行聚类。我们必须从ScikitLearn导入模块。我们指定6个作为集群的数量。请注意,agglomerativecluster没有提供单一的链接方法。

from sklearn.cluster import AgglomerativeClustering
model = AgglomerativeClustering(n_clusters=6) # using ward linkage by default
pred = model.fit_predict(cust_pd)
sns.scatterplot(x=cust_pd["Income"], y=cust_pd["Debt"], hue=pred)

输出如下: 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值