【ML】KMeans 实践(基于sklearn)

【ML】KMeans 实践(基于sklearn)

  • 读取数据
  • 可视化数据(观察规律)
  • 训练
  • 预测 + 可视化

读取数据

import numpy as np
import pandas as pd
data = pd.read_csv('/kaggle/input/xclara/xclara.csv')
data.head()

输出:

V1 V2
0 2.072345 -3.241693
1 17.936710 15.784810
2 1.083576 7.319176
3 11.120670 14.406780
4 23.711550 2.557729

可视化数据(观察规律)

from matplotlib import pyplot as plt
V1 = data.loc[:,'V1']
V2 = data.loc[:,'V2']
plt.scatter(V1,V2)

【ML】KMeans 实践(基于sklearn)_第1张图片

训练

from sklearn.cluster import KMeans
KM = KMeans(n_clusters=3)
KM.fit(data)

预测 + 可视化

predict = KM.predict(data)

plt.scatter(V1[predict==0],V2[predict==0])
plt.scatter(V1[predict==1],V2[predict==1])
plt.scatter(V1[predict==2],V2[predict==2])
plt.show()

【ML】KMeans 实践(基于sklearn)_第2张图片

你可能感兴趣的:(机器学习,算法,python,sklearn,kmeans,python)