Skip to content

Commit 3705ccc

Browse files
committed
KMenas 聚类
1 parent c8c59ca commit 3705ccc

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

K-Means/K-Means_scikit-learn.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#-*- coding: utf-8 -*-
2+
import numpy as np
3+
from scipy import io as spio
4+
from matplotlib import pyplot as plt
5+
from sklearn.cluster import KMeans
6+
7+
8+
def kMenas():
9+
data = spio.loadmat("data.mat")
10+
X = data['X']
11+
model = KMeans(n_clusters=3).fit(X) # n_clusters指定3类,拟合数据
12+
centroids = model.cluster_centers_ # 聚类中心
13+
14+
plt.scatter(X[:,0], X[:,1]) # 原数据的散点图
15+
plt.plot(centroids[:,0],centroids[:,1],'r^',markersize=10) # 聚类中心
16+
plt.show()
17+
18+
if __name__ == "__main__":
19+
kMenas()

0 commit comments

Comments
 (0)