Skip to content

Commit b14a254

Browse files
committed
import numpy as np
1 parent 78435b0 commit b14a254

File tree

6 files changed

+420
-38
lines changed

6 files changed

+420
-38
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ out
1616
*.obj
1717
.ipynb_checkpoints
1818
*/.ipynb_checkpoints
19+
*/**/.ipynb_checkpoints
1920

2021
# Compiled Dynamic libraries
2122
*.so

K-Means/K-Means-sklearn.ipynb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## K-Means 聚类算法 "
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": null,
13+
"metadata": {
14+
"collapsed": true
15+
},
16+
"outputs": [],
17+
"source": [
18+
"%matplotlib inline\n",
19+
"import numpy as np\n",
20+
"from scipy import io as spio\n",
21+
"from matplotlib import pyplot as plt\n",
22+
"from sklearn.cluster import KMeans\n",
23+
"import matplotlib as mpl"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": null,
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"data = spio.loadmat(\"data.mat\")\n",
33+
"X = data[\"X\"]\n",
34+
"model = KMeans(n_clusters=3).fit(X)\n",
35+
"centroids = model.cluster_centers_ \n",
36+
"print centroids"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"metadata": {},
43+
"outputs": [],
44+
"source": [
45+
"def draw(X,center):\n",
46+
" plt.figure(figsize=(8,8)) \n",
47+
" plt.scatter(X[:,0], X[:,1]) # 原数据的散点图\n",
48+
" plt.plot(center[:,0],center[:,1],'r^',markersize=10) # 聚类中心\n",
49+
"\n",
50+
"draw(X,centroids) \n"
51+
]
52+
}
53+
],
54+
"metadata": {
55+
"kernelspec": {
56+
"display_name": "Python [Root]",
57+
"language": "python",
58+
"name": "Python [Root]"
59+
},
60+
"language_info": {
61+
"codemirror_mode": {
62+
"name": "ipython",
63+
"version": 2
64+
},
65+
"file_extension": ".py",
66+
"mimetype": "text/x-python",
67+
"name": "python",
68+
"nbconvert_exporter": "python",
69+
"pygments_lexer": "ipython2",
70+
"version": "2.7.12"
71+
}
72+
},
73+
"nbformat": 4,
74+
"nbformat_minor": 1
75+
}

K-Means/K-Means.ipynb

Lines changed: 307 additions & 0 deletions
Large diffs are not rendered by default.

K-Means/K-Means_scikit-learn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
def kMenas():
99
data = spio.loadmat("data.mat")
1010
X = data['X']
11-
model = KMeans(n_clusters=3).fit(X) # n_clusters指定3类,拟合数据
11+
model = KMeans(n_clusters=3).fit(X) # n_clusters指定3类,拟合数据
1212
centroids = model.cluster_centers_ # 聚类中心
1313

1414
plt.scatter(X[:,0], X[:,1]) # 原数据的散点图

K-Means/K-Menas.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
def KMeans():
1313
'''二维数据聚类过程演示'''
14-
print u'聚类过程展示...\n'
14+
print (u'聚类过程展示...\n')
1515
data = spio.loadmat("data.mat")
1616
X = data['X']
1717
K = 3 # 总类数
@@ -21,7 +21,7 @@ def KMeans():
2121
'''
2222
图片压缩
2323
'''
24-
print u'K-Means压缩图片\n'
24+
print (u'K-Means压缩图片\n')
2525
img_data = misc.imread("bird.png") # 读取图片像素数据
2626
img_data = img_data/255.0 # 像素值映射到0-1
2727
img_size = img_data.shape
@@ -31,21 +31,21 @@ def KMeans():
3131
max_iters = 5
3232
initial_centroids = kMeansInitCentroids(X,K)
3333
centroids,idx = runKMeans(X, initial_centroids, max_iters, False)
34-
print u'\nK-Means运行结束\n'
35-
print u'\n压缩图片...\n'
34+
print (u'\nK-Means运行结束\n')
35+
print (u'\n压缩图片...\n')
3636
idx = findClosestCentroids(X, centroids)
3737
X_recovered = centroids[idx,:]
3838
X_recovered = X_recovered.reshape(img_size[0],img_size[1],3)
3939

40-
print u'绘制图片...\n'
40+
print (u'绘制图片...\n')
4141
plt.subplot(1,2,1)
4242
plt.imshow(img_data)
4343
plt.title(u"原先图片",fontproperties=font)
4444
plt.subplot(1,2,2)
4545
plt.imshow(X_recovered)
4646
plt.title(u"压缩图像",fontproperties=font)
4747
plt.show()
48-
print u'运行结束!'
48+
print (u'运行结束!')
4949

5050

5151
# 找到每条数据距离哪个类中心最近
@@ -86,7 +86,7 @@ def runKMeans(X,initial_centroids,max_iters,plot_process):
8686
idx = np.zeros((m,1)) # 每条数据属于哪个类
8787

8888
for i in range(max_iters): # 迭代次数
89-
print u'迭代计算次数:%d'%(i+1)
89+
print (u'迭代计算次数:%d'%(i+1))
9090
idx = findClosestCentroids(X, centroids)
9191
if plot_process: # 如果绘制图像
9292
plt = plotProcessKMeans(X,centroids,previous_centroids) # 画聚类中心的移动过程
@@ -120,4 +120,3 @@ def kMeansInitCentroids(X,K):
120120

121121
if __name__ == "__main__":
122122
KMeans()
123-

0 commit comments

Comments
 (0)