多元统计分析实验-聚类分析

1、实验目的

通过本实验使学生能熟练应用python语言进行系统聚类分析相关软件的开发工作。

2、 实验内容

为比较10种红葡萄酒的质量,由5名品酒师对每种酒的颜色、香味、甜度、纯度和果味6项指标进行打分,最低分1分,最高分为10分,得到每种酒的每项指标的平均得分(见下图)。

多元统计分析实验-聚类分析_第1张图片

图3 10种红葡萄酒的得分数据表

使用图3数据完成以下内容。

1、使用SPSS软件对图3数据采用系统聚类法进行分析;

2、用python语言编程实现系统聚类法分析,并运行上述数据,结果与第1步骤中的结果进行比对,系统聚类法实现最小距离、最大距离、重心距离、类平均距离四种中的两种,同时距离的衡量实现欧式距离、绝对距离、切贝谢夫距离、明可夫斯基距离的两种。

  1. 使用SPSS软件对图3数据采用系统聚类法进行分析;

最小距离+欧式

多元统计分析实验-聚类分析_第2张图片

 

多元统计分析实验-聚类分析_第3张图片

 

最大距离+切贝谢夫距离

多元统计分析实验-聚类分析_第4张图片

 

多元统计分析实验-聚类分析_第5张图片

 2用python语言编程实现系统聚类法分析,并运行上述数据,结果与第1步骤中的结果进行比对,系统聚类法实现最小距离、最大距离、重心距离、类平均距离四种中的两种,同时距离的衡量实现欧式距离、绝对距离、切贝谢夫距离、明可夫斯基距离的两种。

最小距离+欧式距离

import pandas as pd
import scipy.cluster.hierarchy as sch
from sklearn.cluster import AgglomerativeClustering
from sklearn.preprocessing import MinMaxScaler
from matplotlib import pyplot as plt

data = pd.read_csv("实验三数据.csv",encoding="gbk") #读入数据
#清除‘酒’这列数据
data = data.drop(['酒'], axis=1)
df = MinMaxScaler().fit_transform(data)

# 建立模型
model = AgglomerativeClustering(n_clusters=3)
model.fit(df)
data['类别标签'] = model.labels_
print(data.head())

# 画图
#single为最近邻点算法,euclidean为欧式距离
ss = sch.linkage(df,method='single', metric='euclidean')
sch.dendrogram(ss)
plt.show()

运行结果

多元统计分析实验-聚类分析_第6张图片

 最大距离+切贝谢夫距离

import pandas as pd
import scipy.cluster.hierarchy as sch
from sklearn.cluster import AgglomerativeClustering
from sklearn.preprocessing import MinMaxScaler
from matplotlib import pyplot as plt

data = pd.read_csv("实验三数据.csv",encoding="gbk") #读入数据
#清除‘酒’这列数据
data = data.drop(['酒'], axis=1)
df = MinMaxScaler().fit_transform(data)

# 建立模型
model = AgglomerativeClustering(n_clusters=3)
model.fit(df)
data['类别标签'] = model.labels_
print(data.head())

# 画图
#complete为最远邻点算法,chebychev为切贝谢夫距离
ss = sch.linkage(df,method='complete', metric='chebychev')
sch.dendrogram(ss)
plt.show()

 

运行结果

多元统计分析实验-聚类分析_第7张图片


参考博客:机器学习之聚类算法(五)层次聚类代码实现及模型可视化 

                   scipy.cluster.hierarchy库中linkage函数的参数

相关代码及数据:蓝奏云

你可能感兴趣的:(多元统计分析,聚类,python)