天池-车辆产品聚类分析-积累笔记

文章目录

1 LabelEncoder方法

sklearn.preprocess.LabelEncoder

pd.get_dummies(data)

import pandas as pd
data = pd.read_csv("data/car_price.csv")

one_matrix = pd.get_dummies(data)
one_matrix

天池-车辆产品聚类分析-积累笔记

2 特征之间关联性分析

sns.pairplot(data=data)


import seaborn as sns 
import matplotlib.pyplot as plt

sns.pairplot(data=df)
plt.savefig("pairplot.svg")
plt.show()

train_corr=x_train.corr()

# 删除无关数据
x_train = data_price.drop(columns=['car_ID', 'symboling', 'CarName'])
train_corr=x_train.corr()

天池-车辆产品聚类分析-积累笔记

3 聚类评估方法

(kmeans.inertia_)簇内误差平方和

# 寻找聚类最优簇的个数
score=[]
for n_cluster in range(1,11):
    kmeans = KMeans(n_cluster)
    kmeans.fit(x_train)
    score.append(kmeans.inertia_)
plt.plot(range(1,11),score)
plt.show    

天池-车辆产品聚类分析-积累笔记

轮廓系数 sklearn.metrics.silhouette_score

专门做的笔记传送门


from sklearn.metrics import silhouette_score

sc_score = silhouette_score(x, kmeans_model.labels_, metric="euclidean")

4 层次聚类 sklearn.cluster.AgglomerativeClustering


#使用层次聚类
from scipy.cluster.hierarchy import dendrogram, ward
from sklearn.cluster import KMeans, AgglomerativeClustering
import matplotlib.pyplot as plt
model = AgglomerativeClustering(linkage='ward', n_clusters=3)
y = model.fit_predict(train_x)
print(y)

linkage_matrix = ward(train_x)
dendrogram(linkage_matrix)
plt.show()

传送门


from scipy.spatial.distance import pdist
from scipy.cluster.hierarchy import linkage, dendrogram
import matplotlib.pyplot as plt
%matplotlib inline
row_clusters = linkage(pdist(data_copy, metric='euclidean'), method='ward')
fig = plt.figure(figsize=(12,10))
# 参数p和参数truncate_mode用来将谱系图截断,部分结点的子树被剪枝,横轴显示的是该结点包含的样本数
row_dendr = dendrogram(
    row_clusters,
    p=data_copy.shape[0],
    truncate_mode='lastp',
    color_threshold=7,
)

天池-车辆产品聚类分析-积累笔记

5. 个人实战

GardenLu的实战传送门

上一篇:机器学习入门篇——sklearn入门&决策树在sklearn中的实现


下一篇:Python手写字母识别