第十章 聚类 案例:汽车款式聚类

案例:汽车款式聚类

案例背景

本案例的数据基于R语言 ISLR 包中的Auto数据集,数据集中共有392个样本, 8个特征,根据每种汽车的参数,利用聚类算法来进行聚类,识别出相似的汽车。

数据读取与划分

python 复制代码
# 忽略警告信息
import warnings
warnings.filterwarnings("ignore")
python 复制代码
import numpy as np
import pandas as pd
from plotnine import *
from sklearn import metrics
python 复制代码
# 读入数据
auto = pd.read_csv('./input/Auto.csv')        
auto.head()

| | mpg | cylinders | displacement | horsepower | weight | acceleration | year | origin |
| 0 | 18.0 | 8 | 307.0 | 130 | 3504 | 12.0 | 70 | 1 |
| 1 | 15.0 | 8 | 350.0 | 165 | 3693 | 11.5 | 70 | 1 |
| 2 | 18.0 | 8 | 318.0 | 150 | 3436 | 11.0 | 70 | 1 |
| 3 | 16.0 | 8 | 304.0 | 150 | 3433 | 12.0 | 70 | 1 |

4 17.0 8 302.0 140 3449 10.5 70 1
python 复制代码
#对连续变量用Z-score标准化
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
auto[['mpg','displacement','horsepower','weight','acceleration']] = scaler.fit_transform(auto[['mpg','displacement','horsepower','weight','acceleration']])

#对类别变量进行哑变量编码
auto_scaled = pd.get_dummies(data=auto,columns=['cylinders','year','origin'])
python 复制代码
## 再次查看数据前5行
auto_scaled.head()

| | mpg | displacement | horsepower | weight | acceleration | cylinders_3 | cylinders_4 | cylinders_5 | cylinders_6 | cylinders_8 | ... | year_76 | year_77 | year_78 | year_79 | year_80 | year_81 | year_82 | origin_1 | origin_2 | origin_3 |
| 0 | -0.698638 | 1.077290 | 0.664133 | 0.620540 | -1.285258 | 0 | 0 | 0 | 0 | 1 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
| 1 | -1.083498 | 1.488732 | 1.574594 | 0.843334 | -1.466724 | 0 | 0 | 0 | 0 | 1 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
| 2 | -0.698638 | 1.182542 | 1.184397 | 0.540382 | -1.648189 | 0 | 0 | 0 | 0 | 1 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
| 3 | -0.955212 | 1.048584 | 1.184397 | 0.536845 | -1.285258 | 0 | 0 | 0 | 0 | 1 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |

4 -0.826925 1.029447 0.924265 0.555706 -1.829655 0 0 0 0 1 ... 0 0 0 0 0 0 0 1 0 0

5 rows × 26 columns

K-Means模型搭建与评估

python 复制代码
#K-means聚类
from sklearn.cluster import KMeans
model = KMeans(n_clusters=3,random_state=42).fit(auto_scaled)    #设置随机种子为42
#样本标签和簇质心
auto_label = model.labels_
auto_cluster = model.cluster_centers_
auto_label
复制代码
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, 2, 2, 2, 2,
       2, 2, 1, 0, 0, 0, 0, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
       1, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 1, 2, 2, 2,
       2, 2, 1, 2, 0, 0, 2, 2, 2, 2, 0, 2, 1, 0, 1, 1, 1, 2, 2, 2, 2, 1,
       1, 1, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1,
       0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 2, 2, 1, 2, 2, 2, 2, 1, 2, 1, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 1,
       1, 1, 1, 2, 2, 2, 2, 2, 0, 2, 1, 1, 0, 0, 0, 0, 2, 2, 2, 2, 2, 0,
       1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2,
       2, 2, 2, 2, 2, 1, 0, 0, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0,
       2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 1, 2, 2, 1, 1, 2, 1, 1, 0, 0, 0,
       0, 0, 0, 1, 0, 2, 2, 2, 2, 1, 1, 2, 1, 2, 2, 2, 2, 2, 1, 1, 2, 2,
       2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 1, 1, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2])
python 复制代码
#画每个簇样本数的柱状图
auto_label_dataframe = pd.DataFrame({'clusters':auto_label})
auto_label_dataframe['clusters'] = auto_label_dataframe['clusters'].astype('category')
ggplot(auto_label_dataframe,aes('clusters',fill='clusters')) + geom_bar()
复制代码
<Figure Size: (460 x 345)>
python 复制代码
# 轮廓系数评估聚类效果
labels = model.labels_
print("轮廓系数(Silhouette Coefficient): %0.4f"
      % metrics.silhouette_score(auto_scaled, labels))
复制代码
轮廓系数(Silhouette Coefficient): 0.3183
python 复制代码
# 选择不同k值比较聚类效果
for i in [2,4,6]:
    model = KMeans(n_clusters=i,random_state=42).fit(auto_scaled)   #设置随机种子为111
    labels = model.labels_
    print("k=%d 轮廓系数(Silhouette Coefficient): %0.4f"% (i,metrics.silhouette_score(auto_scaled, labels)))
复制代码
k=2 轮廓系数(Silhouette Coefficient): 0.4152
k=4 轮廓系数(Silhouette Coefficient): 0.2548
k=6 轮廓系数(Silhouette Coefficient): 0.2039

层次聚类模型搭建与评估

python 复制代码
from sklearn.cluster import AgglomerativeClustering
python 复制代码
single_model = AgglomerativeClustering(n_clusters=3,linkage="single").fit(auto_scaled)

single_labels = single_model.labels_
print("Single Linkage 轮廓系数(Silhouette Coefficient): %0.4f"
      % metrics.silhouette_score(auto_scaled, single_labels))
复制代码
Single Linkage 轮廓系数(Silhouette Coefficient): 0.0111
python 复制代码
complete_model = AgglomerativeClustering(n_clusters=3, linkage='complete').fit(auto_scaled)

complete_labels = complete_model.labels_
print("Complete Linkage 轮廓系数(Silhouette Coefficient): %0.4f"
      % metrics.silhouette_score(auto_scaled, complete_labels))
复制代码
Complete Linkage 轮廓系数(Silhouette Coefficient): 0.2297
python 复制代码
average_model = AgglomerativeClustering(n_clusters=3, linkage='average').fit(auto_scaled)

average_labels = average_model.labels_
print("Average Linkage轮廓系数(Silhouette Coefficient): %0.4f"
      % metrics.silhouette_score(auto_scaled, average_labels))
复制代码
Average Linkage轮廓系数(Silhouette Coefficient): 0.3075
python 复制代码
#画每个簇样本数的柱状图
auto_label = average_model.labels_
auto_label_dataframe = pd.DataFrame({'clusters':auto_label})
auto_label_dataframe['clusters'] = auto_label_dataframe['clusters'].astype('category')
ggplot(auto_label_dataframe,aes('clusters',fill='clusters')) + geom_bar()
python 复制代码
# 绘制谱系图
from scipy.spatial.distance import pdist
from scipy.cluster.hierarchy import linkage, dendrogram
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']

#利用scipy中pdist,linkage,dendrogram函数绘制谱系图
#pdist函数返回距离矩阵,linkage函数返回一个ndarray对象,描述了簇合并的过程
#dendrogram函数用来绘制谱系图
row_clusters = linkage(pdist(auto_scaled,metric='euclidean'),method='ward')
fig = plt.figure(figsize=(12,10))
#参数p和参数truncate_mode用来将谱系图截断,部分结点的子树被剪枝,横轴显示的是该结点包含的样本数
row_dendr = dendrogram(row_clusters,p=50,truncate_mode='lastp',color_threshold=5)
plt.tight_layout()
plt.xticks(fontsize=15)
plt.title('谱系图', fontsize=15)
复制代码
Text(0.5, 1.0, '谱系图')

DBSCAN模型搭建与评估

python 复制代码
from sklearn.cluster import DBSCAN
python 复制代码
model = DBSCAN(eps=1.5,min_samples=4).fit(auto_scaled)
python 复制代码
#绘制不同簇的样本数柱状图
auto_label = model.labels_
auto_label_dataframe = pd.DataFrame({'clusters':auto_label})
auto_label_dataframe['clusters'] = auto_label_dataframe['clusters'].astype('category')
ggplot(auto_label_dataframe,aes('clusters',fill='clusters')) + geom_bar()
python 复制代码
# 样本的类别标签
labels = model.labels_

# 标签中的簇数,忽略噪声点
n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)

print('簇数: %d' % n_clusters_)
print("轮廓系数(Silhouette Coefficient): %0.4f"
      % metrics.silhouette_score(auto_scaled, labels))
复制代码
簇数: 3
轮廓系数(Silhouette Coefficient): 0.1476
python 复制代码
# 不同最小样本数下的簇个数
## 设置参数取值范围
min_samples_grid = [1, 3, 5, 7, 9]

## 训练模型并输出簇个数
cluster_number = []
slt_score = []

for item in min_samples_grid:
    model = DBSCAN(min_samples=item).fit(auto_scaled)
    cluster_number.append(len(np.unique(model.labels_))-1)
    
## 绘图
plt.plot(min_samples_grid, cluster_number, 'r-*', linewidth=2)
plt.xlabel('最小样本数')
plt.ylabel('簇个数')
plt.title('不同最小样本数下聚类的簇个数')
复制代码
Text(0.5, 1.0, '不同最小样本数下聚类的簇个数')
相关推荐
世人万千丶1 小时前
鸿蒙Crash高级捕获与异常监控:全局异常兜底/崩溃栈解析/符号表还原/智能聚类/闭环修复
学习·机器学习·华为·数据挖掘·harmonyos·鸿蒙·聚类
天天爱吃肉82186 小时前
# 商用车多体动力学实战笔记|第7篇:制动系统与制动热衰退、ABS滞环控制
大数据·人工智能·笔记·python·嵌入式硬件·汽车
龙亘川6 小时前
【客户定制更新】能源充电(汽车充电管理模块)版本更新详情
spring boot·汽车·系统安全·智慧城市·能源
spider_xcxc8 小时前
[特殊字符] 空间数据挖掘中频繁并置模式挖掘的并行与分布式加速:从传统方法到异构协同新架构
数据挖掘
AI科技星8 小时前
全域光速运动理论体系 (GAQ-UFT)——范式重构、核心方程与传统物理的本质分野
人工智能·线性代数·机器学习·重构·数据挖掘·回归·ai科技星
汽车仪器仪表相关领域9 小时前
NHA-604/605汽车排放气体测试仪:00级国标计量|多动力车型兼容|移动/固定双工况|智能联网尾气检测设备
大数据·人工智能·功能测试·安全·汽车·压力测试·可用性测试
Microvision维视智造10 小时前
行业洞察系列 06 | 汽车制造的视觉革命
人工智能·计算机视觉·汽车·视觉检测·制造·机器视觉
咱入行浅18 小时前
汽车之家联合HarmonyOS SDK,深度构建鸿蒙生态体系
华为·汽车·harmonyos
临床数据科学和人工智能兴趣组19 小时前
要使用 R Markdown,首先需要安装 R 和 RStudio,接着安装 rmarkdown 包
开发语言·数据挖掘·数据分析·r语言·r语言-4.2.1
炎武丶航20 小时前
汽车功能测试学习(1):FCW前方碰撞预警
功能测试·学习·汽车