From fbd79a298bd6dd9b90b1b65730294a27924b45fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=99=93=E5=86=9B?= Date: Wed, 8 Mar 2017 15:28:08 +0800 Subject: [PATCH] kmeans --- .gitignore | 1 + kmeans/data/places.txt | 69 +++++++++++++ kmeans/data/portlandClubs.txt | 69 +++++++++++++ kmeans/data/testSet.txt | 80 +++++++++++++++ kmeans/data/testSet2.txt | 60 +++++++++++ kmeans/kmeans.py | 173 ++++++++++++++++++++++++++++++++ kmeans/test/bi_kmeans.py | 24 +++++ kmeans/test/normal_kmeans.py | 24 +++++ linear_regression/npm-debug.log | 23 ----- 9 files changed, 500 insertions(+), 23 deletions(-) create mode 100755 kmeans/data/places.txt create mode 100755 kmeans/data/portlandClubs.txt create mode 100755 kmeans/data/testSet.txt create mode 100755 kmeans/data/testSet2.txt create mode 100755 kmeans/kmeans.py create mode 100644 kmeans/test/bi_kmeans.py create mode 100644 kmeans/test/normal_kmeans.py delete mode 100644 linear_regression/npm-debug.log diff --git a/.gitignore b/.gitignore index dde3895..56de96a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store *.pyc +*.log diff --git a/kmeans/data/places.txt b/kmeans/data/places.txt new file mode 100755 index 0000000..5af4c34 --- /dev/null +++ b/kmeans/data/places.txt @@ -0,0 +1,69 @@ +Dolphin II 10860 SW Beaverton-Hillsdale Hwy Beaverton, OR 45.486502 -122.788346 +Hotties 10140 SW Canyon Rd. Beaverton, OR 45.493150 -122.781021 +Pussycats 8666a SW Canyon Road Beaverton, OR 45.498187 -122.766147 +Stars Cabaret 4570 Lombard Ave Beaverton, OR 45.485943 -122.800311 +Sunset Strip 10205 SW Park Way Beaverton, OR 45.508203 -122.781853 +Vegas VIP Room 10018 SW Canyon Rd Beaverton, OR 45.493398 -122.779628 +Full Moon Bar and Grill 28014 Southeast Wally Road Boring, OR 45.430319 -122.376304 +505 Club 505 Burnside Rd Gresham, OR 45.507621 -122.425553 +Dolphin 17180 McLoughlin Blvd Milwaukie, OR 45.399070 -122.618893 +Dolphin III 13305 SE McLoughlin BLVD Milwaukie, OR 45.427072 -122.634159 +Acropolis 8325 McLoughlin Blvd Portland, OR 45.462173 -122.638846 +Blush 5145 SE McLoughlin Blvd Portland, OR 45.485396 -122.646587 +Boom Boom Room 8345 Barbur Blvd Portland, OR 45.464826 -122.699212 +Bottoms Up 16900 Saint Helens Rd Portland, OR 45.646831 -122.842918 +Cabaret II 17544 Stark St Portland, OR 45.519142 -122.482480 +Cabaret Lounge 503 W Burnside Portland, OR 45.523094 -122.675528 +Carnaval 330 SW 3rd Avenue Portland, OR 45.520682 -122.674206 +Casa Diablo 2839 NW St. Helens Road Portland, OR 45.543016 -122.720828 +Chantilly Lace 6723 Killingsworth St Portland, OR 45.562715 -122.593078 +Club 205 9939 Stark St Portland, OR 45.519052 -122.561510 +Club Rouge 403 SW Stark Portland, OR 45.520561 -122.675605 +Dancin' Bare 8440 Interstate Ave Portland, OR 45.584124 -122.682725 +Devil's Point 5305 SE Foster Rd Portland, OR 45.495365 -122.608366 +Double Dribble 13550 Southeast Powell Boulevard Portland, OR 45.497750 -122.524073 +Dream on Saloon 15920 Stark St Portland, OR 45.519142 -122.499672 +DV8 5003 Powell Blvd Portland, OR 45.497498 -122.611177 +Exotica 240 Columbia Blvd Portland, OR 45.583048 -122.668350 +Frolics 8845 Sandy Blvd Portland, OR 45.555384 -122.571475 +G-Spot Airport 8654 Sandy Blvd Portland, OR 45.554263 -122.574167 +G-Spot Northeast 3400 NE 82nd Ave Portland, OR 45.547229 -122.578746 +G-Spot Southeast 5241 SE 72nd Ave Portland, OR 45.484823 -122.589208 +Glimmers 3532 Powell Blvd Portland, OR 45.496918 -122.627920 +Golden Dragon Exotic Club 324 SW 3rd Ave Portland, OR 45.520714 -122.674189 +Heat 12131 SE Holgate Blvd. Portland, OR 45.489637 -122.538196 +Honeysuckle's Lingerie 3520 82nd Ave Portland, OR 45.548651 -122.578730 +Hush Playhouse 13560 Powell Blvd Portland, OR 45.497765 -122.523985 +JD's Bar & Grill 4523 NE 60th Ave Portland, OR 45.555811 -122.600881 +Jody's Bar And Grill 12035 Glisan St Portland, OR 45.526306 -122.538833 +Landing Strip 6210 Columbia Blvd Portland, OR 45.595042 -122.728825 +Lucky Devil Lounge 633 SE Powell Blvd Portland, OR 45.501585 -122.659310 +Lure 11051 Barbur Blvd Portland, OR 45.445233 -122.732606 +Magic Garden 217 4th Ave Portland, OR 45.524692 -122.674466 +Mary's Club 129 Broadway Portland, OR 45.535101 -122.667390 +Montego's 15826 SE Division Portland, OR 45.504448 -122.500034 +Mr. Peeps 709 122nd Ave Portland, OR 45.527863 -122.537726 +Mynt Gentlemen's Club 3390 NE Sandy Blvd Portland, OR 45.532426 -122.628865 +Mystic 9950 SE Stark St. Portland, OR 45.519037 -122.561283 +Nicolai Street Clubhouse 2460 24th Ave Portland, OR 45.540098 -122.641114 +Oh Zone 6218 Columbia Blvd Portland, OR 45.595069 -122.728961 +Pallas Club 13639 Powell Blvd Portland, OR 45.497990 -122.522849 +Pirates Cove 7427 Sandy Blvd Portland, OR 45.549288 -122.586505 +Private Pleasures 10931 53rd Ave Portland, OR 45.446442 -122.731034 +Pussycats 3414 Northeast 82nd Avenue Portland, OR 45.547337 -122.578744 +Riverside Corral 545 Tacoma St Portland, OR 45.464338 -122.660285 +Rooster's 605 Columbia Blvd Portland, OR 45.583693 -122.672462 +Rose City Strip 3620 35th Pl Portland, OR 45.496601 -122.627688 +Safari Show Club 3000 SE Powell Blvd Portland, OR 45.497091 -122.634581 +Sassy's Bar & Grill 927 Morrison St Portland, OR 45.517225 -122.656367 +Secret Rendezvous 12503 Division St Portland, OR 45.504087 -122.534481 +Shimmers 7944 Foster Rd Portland, OR 45.483836 -122.581608 +Soobie's 333 SE 122nd Ave Portland, OR 45.520162 -122.537787 +Spyce Gentleman's Club 33 NW 2nd Ave Portland, OR 45.523370 -122.672388 +Sugar Shack 6732 Killingsworth St Portland, OR 45.562699 -122.593048 +The Hawthorne Strip 1008 Hawthorne Blvd Portland, OR 45.512220 -122.655527 +Tommy's Too 10335 Foster Rd Portland, OR 45.476721 -122.557005 +Union Jacks 938 Burnside St Portland, OR 45.522902 -122.656249 +Video Visions 6723 Killingsworth St Portland, OR 45.562715 -122.593078 +Stars Cabaret Bridgeport 17939 SW McEwan Rd Tigard, OR 45.425788 -122.765754 +Jiggles 7455 SW Nyberg St Tualatin, OR 45.382682 -122.753932 diff --git a/kmeans/data/portlandClubs.txt b/kmeans/data/portlandClubs.txt new file mode 100755 index 0000000..fd35dce --- /dev/null +++ b/kmeans/data/portlandClubs.txt @@ -0,0 +1,69 @@ +Dolphin II 10860 SW Beaverton-Hillsdale Hwy Beaverton, OR +Hotties 10140 SW Canyon Rd. Beaverton, OR +Pussycats 8666a SW Canyon Road Beaverton, OR +Stars Cabaret 4570 Lombard Ave Beaverton, OR +Sunset Strip 10205 SW Park Way Beaverton, OR +Vegas VIP Room 10018 SW Canyon Rd Beaverton, OR +Full Moon Bar and Grill 28014 Southeast Wally Road Boring, OR +505 Club 505 Burnside Rd Gresham, OR +Dolphin 17180 McLoughlin Blvd Milwaukie, OR +Dolphin III 13305 SE McLoughlin BLVD Milwaukie, OR +Acropolis 8325 McLoughlin Blvd Portland, OR +Blush 5145 SE McLoughlin Blvd Portland, OR +Boom Boom Room 8345 Barbur Blvd Portland, OR +Bottoms Up 16900 Saint Helens Rd Portland, OR +Cabaret II 17544 Stark St Portland, OR +Cabaret Lounge 503 W Burnside Portland, OR +Carnaval 330 SW 3rd Avenue Portland, OR +Casa Diablo 2839 NW St. Helens Road Portland, OR +Chantilly Lace 6723 Killingsworth St Portland, OR +Club 205 9939 Stark St Portland, OR +Club Rouge 403 SW Stark Portland, OR +Dancin' Bare 8440 Interstate Ave Portland, OR +Devil's Point 5305 SE Foster Rd Portland, OR +Double Dribble 13550 Southeast Powell Boulevard Portland, OR +Dream on Saloon 15920 Stark St Portland, OR +DV8 5003 Powell Blvd Portland, OR +Exotica 240 Columbia Blvd Portland, OR +Frolics 8845 Sandy Blvd Portland, OR +G-Spot Airport 8654 Sandy Blvd Portland, OR +G-Spot Northeast 3400 NE 82nd Ave Portland, OR +G-Spot Southeast 5241 SE 72nd Ave Portland, OR +Glimmers 3532 Powell Blvd Portland, OR +Golden Dragon Exotic Club 324 SW 3rd Ave Portland, OR +Heat 12131 SE Holgate Blvd. Portland, OR +Honeysuckle's Lingerie 3520 82nd Ave Portland, OR +Hush Playhouse 13560 Powell Blvd Portland, OR +JD's Bar & Grill 4523 NE 60th Ave Portland, OR +Jody's Bar And Grill 12035 Glisan St Portland, OR +Landing Strip 6210 Columbia Blvd Portland, OR +Lucky Devil Lounge 633 SE Powell Blvd Portland, OR +Lure 11051 Barbur Blvd Portland, OR +Magic Garden 217 4th Ave Portland, OR +Mary's Club 129 Broadway Portland, OR +Montego's 15826 SE Division Portland, OR +Mr. Peeps 709 122nd Ave Portland, OR +Mynt Gentlemen's Club 3390 NE Sandy Blvd Portland, OR +Mystic 9950 SE Stark St. Portland, OR +Nicolai Street Clubhouse 2460 24th Ave Portland, OR +Oh Zone 6218 Columbia Blvd Portland, OR +Pallas Club 13639 Powell Blvd Portland, OR +Pirates Cove 7427 Sandy Blvd Portland, OR +Private Pleasures 10931 53rd Ave Portland, OR +Pussycats 3414 Northeast 82nd Avenue Portland, OR +Riverside Corral 545 Tacoma St Portland, OR +Rooster's 605 Columbia Blvd Portland, OR +Rose City Strip 3620 35th Pl Portland, OR +Safari Show Club 3000 SE Powell Blvd Portland, OR +Sassy's Bar & Grill 927 Morrison St Portland, OR +Secret Rendezvous 12503 Division St Portland, OR +Shimmers 7944 Foster Rd Portland, OR +Soobie's 333 SE 122nd Ave Portland, OR +Spyce Gentleman's Club 33 NW 2nd Ave Portland, OR +Sugar Shack 6732 Killingsworth St Portland, OR +The Hawthorne Strip 1008 Hawthorne Blvd Portland, OR +Tommy's Too 10335 Foster Rd Portland, OR +Union Jacks 938 Burnside St Portland, OR +Video Visions 6723 Killingsworth St Portland, OR +Stars Cabaret Bridgeport 17939 SW McEwan Rd Tigard, OR +Jiggles 7455 SW Nyberg St Tualatin, OR \ No newline at end of file diff --git a/kmeans/data/testSet.txt b/kmeans/data/testSet.txt new file mode 100755 index 0000000..1b63ebb --- /dev/null +++ b/kmeans/data/testSet.txt @@ -0,0 +1,80 @@ +1.658985 4.285136 +-3.453687 3.424321 +4.838138 -1.151539 +-5.379713 -3.362104 +0.972564 2.924086 +-3.567919 1.531611 +0.450614 -3.302219 +-3.487105 -1.724432 +2.668759 1.594842 +-3.156485 3.191137 +3.165506 -3.999838 +-2.786837 -3.099354 +4.208187 2.984927 +-2.123337 2.943366 +0.704199 -0.479481 +-0.392370 -3.963704 +2.831667 1.574018 +-0.790153 3.343144 +2.943496 -3.357075 +-3.195883 -2.283926 +2.336445 2.875106 +-1.786345 2.554248 +2.190101 -1.906020 +-3.403367 -2.778288 +1.778124 3.880832 +-1.688346 2.230267 +2.592976 -2.054368 +-4.007257 -3.207066 +2.257734 3.387564 +-2.679011 0.785119 +0.939512 -4.023563 +-3.674424 -2.261084 +2.046259 2.735279 +-3.189470 1.780269 +4.372646 -0.822248 +-2.579316 -3.497576 +1.889034 5.190400 +-0.798747 2.185588 +2.836520 -2.658556 +-3.837877 -3.253815 +2.096701 3.886007 +-2.709034 2.923887 +3.367037 -3.184789 +-2.121479 -4.232586 +2.329546 3.179764 +-3.284816 3.273099 +3.091414 -3.815232 +-3.762093 -2.432191 +3.542056 2.778832 +-1.736822 4.241041 +2.127073 -2.983680 +-4.323818 -3.938116 +3.792121 5.135768 +-4.786473 3.358547 +2.624081 -3.260715 +-4.009299 -2.978115 +2.493525 1.963710 +-2.513661 2.642162 +1.864375 -3.176309 +-3.171184 -3.572452 +2.894220 2.489128 +-2.562539 2.884438 +3.491078 -3.947487 +-2.565729 -2.012114 +3.332948 3.983102 +-1.616805 3.573188 +2.280615 -2.559444 +-2.651229 -3.103198 +2.321395 3.154987 +-1.685703 2.939697 +3.031012 -3.620252 +-4.599622 -2.185829 +4.196223 1.126677 +-2.133863 3.093686 +4.668892 -2.562705 +-2.793241 -2.149706 +2.884105 3.043438 +-2.967647 2.848696 +4.479332 -1.764772 +-4.905566 -2.911070 diff --git a/kmeans/data/testSet2.txt b/kmeans/data/testSet2.txt new file mode 100755 index 0000000..d82fa0f --- /dev/null +++ b/kmeans/data/testSet2.txt @@ -0,0 +1,60 @@ +3.275154 2.957587 +-3.344465 2.603513 +0.355083 -3.376585 +1.852435 3.547351 +-2.078973 2.552013 +-0.993756 -0.884433 +2.682252 4.007573 +-3.087776 2.878713 +-1.565978 -1.256985 +2.441611 0.444826 +-0.659487 3.111284 +-0.459601 -2.618005 +2.177680 2.387793 +-2.920969 2.917485 +-0.028814 -4.168078 +3.625746 2.119041 +-3.912363 1.325108 +-0.551694 -2.814223 +2.855808 3.483301 +-3.594448 2.856651 +0.421993 -2.372646 +1.650821 3.407572 +-2.082902 3.384412 +-0.718809 -2.492514 +4.513623 3.841029 +-4.822011 4.607049 +-0.656297 -1.449872 +1.919901 4.439368 +-3.287749 3.918836 +-1.576936 -2.977622 +3.598143 1.975970 +-3.977329 4.900932 +-1.791080 -2.184517 +3.914654 3.559303 +-1.910108 4.166946 +-1.226597 -3.317889 +1.148946 3.345138 +-2.113864 3.548172 +0.845762 -3.589788 +2.629062 3.535831 +-1.640717 2.990517 +-1.881012 -2.485405 +4.606999 3.510312 +-4.366462 4.023316 +0.765015 -3.001270 +3.121904 2.173988 +-4.025139 4.652310 +-0.559558 -3.840539 +4.376754 4.863579 +-1.874308 4.032237 +-0.089337 -3.026809 +3.997787 2.518662 +-3.082978 2.884822 +0.845235 -3.454465 +1.327224 3.358778 +-2.889949 3.596178 +-0.966018 -2.839827 +2.960769 3.079555 +-3.275518 1.577068 +0.639276 -3.412840 diff --git a/kmeans/kmeans.py b/kmeans/kmeans.py new file mode 100755 index 0000000..916f124 --- /dev/null +++ b/kmeans/kmeans.py @@ -0,0 +1,173 @@ +# coding: utf-8 + +import numpy as np + + +def loadDataSet(filename): + """ + 读取数据集 + + Args: + filename: 文件名 + Returns: + dataMat: 数据样本矩阵 + """ + dataMat = [] + fr = open(filename) + for line in fr.readlines(): + curLine = line.strip().split('\t') + # 通过map函数批量转换 + fitLine = map(float, curLine) + dataMat.append(fitLine) + return dataMat + + +def distEclud(vecA, vecB): + """ + 计算两向量的欧氏距离 + + Args: + vecA: 向量A + vecB: 向量B + Returns: + 欧式距离 + """ + return np.sqrt(np.sum(np.power(vecA - vecB, 2))) + + +def J(): + """ + 计算失真函数 + + Args: + + Returns: + 失真函数 + """ + + +def randCent(dataSet, k): + """ + 随机生成k个聚类中心 + + Args: + dataSet: 数据集 + k: 簇数目 + Returns: + centroids: 聚类中心矩阵 + """ + n = np.shape(dataSet)[1] + centroids = np.mat(np.zeros((k, n))) + for j in range(n): + # 随机聚类中心落在数据集的边界之内 + minJ = np.min(dataSet[:, j]) + maxJ = np.max(dataSet[:, j]) + rangeJ = float(maxJ - minJ) + centroids[:, j] = minJ + rangeJ * np.random.rand(k, 1) + return centroids + +def kMeans(dataSet, k): + """ + K-Means + + Args: + dataSet: 数据集 + k: 聚类数 + Returns: + centroids: 聚类中心 + clusterAssment: 点分配结果 + """ + # 随机初始化聚类中心 + centroids = randCent(dataSet, k) + m, n = np.shape(dataSet) + # 点分配结果: 第一列指明样本所在的簇,第二列指明该样本到聚类中心的距离 + clusterAssment = np.mat(np.zeros((m, 2))) + # 标识聚类中心是否仍在改变 + clusterChanged = True + # 直至聚类中心不再变化 + while clusterChanged: + clusterChanged = False + # 分配样本到簇 + for i in range(m): + # 计算第i个样本到各个聚类中心的距离 + minIndex = 0 + minDist = np.inf + for j in range(k): + dist = distEclud(dataSet[i, :], centroids[j, :]) + if(dist < minDist): + minIndex = j + minDist = dist + # 判断cluster是否改变 + if(clusterAssment[i, 0] != minIndex): + clusterChanged = True + clusterAssment[i, :] = minIndex, minDist**2 + # 刷新聚类中心: 移动聚类中心到所在簇的均值位置 + for cent in range(k): + # 通过数组过滤获得簇中的点 + ptsInCluster = dataSet[np.nonzero( + clusterAssment[:, 0].A == cent)[0]] + # 计算均值并移动 + centroids[cent, :] = np.mean(ptsInCluster, axis=0) + return centroids, clusterAssment + + +def biKmeans(dataSet, k): + """ + 二分kmeans算法 + Args: + dataSet: 数据集 + k: 聚类数 + Returns: + centroids: 聚类中心 + clusterAssment: 点分配结果 + """ + # 起始时,只有一个簇,该簇的聚类中心为所有样本的平均位置 + centroid0 = np.mean(dataSet, axis=0).tolist()[0] + # 设置一个列表保存当前的聚类中心 + currentCentroids = [centroid0] + # 点分配结果: 第一列指明样本所在的簇,第二列指明该样本到聚类中心的距离 + clusterAssment = np.mat(np.zeros(m, 2)) + # 初始化点分配结果,默认将所有样本先分配到初始簇 + m, n = np.shape(dataSet) + for j in range(m): + clusterAssment[j, 0] = 0 + clusterAssment[j, 1] = distEclud(dataSet[j, :] - np.mat(centroid0))**2 + # 直到簇的数目达标 + while(len(currentCentroids) < k): + # 当前最小的代价 + lowestError = np.inf + # 对于每一个簇 + for j in range(len(currentCentroids)): + # 获得该簇的样本 + ptsInCluster = dataSet[np.nonzero(clusterAssment[:, 0].A == j)[0]] + # 在该簇上进行2-means聚类 + # 注意,得到的centroids,其聚类编号含0,1 + centroids, clusterAss = kmeans(ptsInCluster, 2) + # 获得划分后的误差之和 + splitedError = np.sum(clusterAss[:, 1]) + # 获得其他簇的样本 + ptsNoInCluster = dataSet[np.nonzero( + clusterAssment[:, 0].A != j)[0]] + # 获得剩余数据集的误差 + nonSplitedError = np.sum(ptsNoInCluster[:, 1]) + # 比较,判断此次划分是否划算 + if(splitedError + nonSplitedError) < lowestError: + # 如果划算,刷新总误差 + lowestError = splitedError + nonSplitedError + # 记录当前的应当划分的簇 + needToSplit = j + # 新获得的簇以及点分配结果 + newCentroids = centroids + newClusterAss = clusterAss.copy() + # 更新簇的分配结果 + # 第0簇应当修正为被划分的簇 + newClusterAss[np.nonzero(newClusterAss[:, 0].A == 0)[0], 0] = needToSplit + # 第1簇应当修正为最新一簇 + newClusterAss[np.nonzero(newClusterAss[:, 0].A == 1)[0], 0] = len(centroids) + # 被划分的簇需要更新 + currentCentroids[needToSplit] = newCentroids[0, :] + # 加入新的划分后的簇 + currentCentroids.append(newCentroids[1, :]) + # 刷新点分配结果 + clusterAssment[np.nonzero(clusterAssment[:, 0].A == newCentroids[0])] + return np.mat(currentCentroids), clusterAssment diff --git a/kmeans/test/bi_kmeans.py b/kmeans/test/bi_kmeans.py new file mode 100644 index 0000000..82a7ffd --- /dev/null +++ b/kmeans/test/bi_kmeans.py @@ -0,0 +1,24 @@ +# coding: utf-8 +import kmeans +import numpy as np +import matplotlib.pyplot as plt + +if __name__ == "__main__": + dataMat = np.mat(kmeans.loadDataSet('data/testSet2.txt')) + centroids, clusterAssment = kmeans.kMeans(dataMat, 3) + clusterCount = np.shape(centroids)[0] + m = np.shape(dataMat)[0] + # 绘制散点图 + patterns = ['o', 'D', '^'] + colors = ['b', 'g', 'y'] + fig = plt.figure() + title = 'bi-kmeans with k=3' + ax = fig.add_subplot(111, title=title) + for k in range(clusterCount): + # 绘制聚类中心 + ax.scatter(centroids[k,0], centroids[k,1], color='r', marker='+', linewidth=20) + for i in range(m): + # 绘制属于该聚类中心的样本 + ptsInCluster = dataMat[np.nonzero(clusterAssment[:, 0].A==k)[0]] + ax.scatter(ptsInCluster[:, 0].flatten().A[0], ptsInCluster[:, 1].flatten().A[0], marker=patterns[k], color=colors[k]) + plt.show() diff --git a/kmeans/test/normal_kmeans.py b/kmeans/test/normal_kmeans.py new file mode 100644 index 0000000..50d0efc --- /dev/null +++ b/kmeans/test/normal_kmeans.py @@ -0,0 +1,24 @@ +# coding: utf-8 +import kmeans +import numpy as np +import matplotlib.pyplot as plt + +if __name__ == "__main__": + dataMat = np.mat(kmeans.loadDataSet('data/testSet.txt')) + centroids, clusterAssment = kmeans.kMeans(dataMat, 2) + clusterCount = np.shape(centroids)[0] + m = np.shape(dataMat)[0] + # 绘制散点图 + patterns = ['o', 'D', '^', 's'] + colors = ['b', 'g', 'y', 'black'] + fig = plt.figure() + title = 'kmeans with k=2' + ax = fig.add_subplot(111, title=title) + for k in range(clusterCount): + # 绘制聚类中心 + ax.scatter(centroids[k, 0], centroids[k, 1], color='r', marker='+', linewidth=20) + for i in range(m): + # 绘制属于该聚类中心的样本 + ptsInCluster = dataMat[np.nonzero(clusterAssment[:, 0].A==k)[0]] + ax.scatter(ptsInCluster[:, 0].flatten().A[0], ptsInCluster[:, 1].flatten().A[0], marker=patterns[k], color=colors[k]) + plt.show() diff --git a/linear_regression/npm-debug.log b/linear_regression/npm-debug.log deleted file mode 100644 index 1634bc7..0000000 --- a/linear_regression/npm-debug.log +++ /dev/null @@ -1,23 +0,0 @@ -0 info it worked if it ends with ok -1 verbose cli [ '/Users/feiyu06/.nvm/versions/node/v6.2.1/bin/node', -1 verbose cli '/Users/feiyu06/.nvm/versions/node/v6.2.1/bin/npm', -1 verbose cli 'run', -1 verbose cli 'start-dev-server' ] -2 info using npm@3.9.3 -3 info using node@v6.2.1 -4 verbose stack Error: ENOENT: no such file or directory, open '/Users/feiyu06/Projects2016/ml/linear_regression/package.json' -4 verbose stack at Error (native) -5 verbose cwd /Users/feiyu06/Projects2016/ml/linear_regression -6 error Darwin 15.6.0 -7 error argv "/Users/feiyu06/.nvm/versions/node/v6.2.1/bin/node" "/Users/feiyu06/.nvm/versions/node/v6.2.1/bin/npm" "run" "start-dev-server" -8 error node v6.2.1 -9 error npm v3.9.3 -10 error path /Users/feiyu06/Projects2016/ml/linear_regression/package.json -11 error code ENOENT -12 error errno -2 -13 error syscall open -14 error enoent ENOENT: no such file or directory, open '/Users/feiyu06/Projects2016/ml/linear_regression/package.json' -15 error enoent ENOENT: no such file or directory, open '/Users/feiyu06/Projects2016/ml/linear_regression/package.json' -15 error enoent This is most likely not a problem with npm itself -15 error enoent and is related to npm not being able to find a file. -16 verbose exit [ -2, true ]