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))
row_dendr = dendrogram(
row_clusters,
p=data_copy.shape[0],
truncate_mode='lastp',
color_threshold=7,
)
5. 个人实战
GardenLu的实战传送门
|