GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — MachineLearning/KMeans at master · Sainath-Reddy7/MachineLearning

Expand file tree

/

KMeans

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
 
 
/

KMeans

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Readme.md
Readme.md
 
 
data.pkl
data.pkl
 
 
kmeans.py
kmeans.py
 
 
test.py
test.py
 
 

Readme.md

###KMeans算法,二分KMeans算法

  • 封装成class
  • 参考sklearn.cluster.KMeans的接口设计

###依赖库

  • Numpy
  • Matplotlib (可视化结果)

###使用方法

import cPickle
from kmeans import KMeans,biKMeans
X,y = cPickle.load(open('data.pkl','r'))
clf = KMeans(n_clusters=10,initCent='random',max_iter=200)
#clf = KMeans(n_clusters=10,initCent=X[0:10],max_iter=200)
#clf = biKMeans(n_clusters=10)
clf.fit(X)
clf.predict(X)

###可视化


import numpy as np
import matplotlib.pyplot as plt
from kmeans import biKMeans
n_clusters = 10
clf = biKMeans(n_clusters)
clf.fit(X)
cents = clf.centroids
labels = clf.labels
sse = clf.sse
#画出聚类结果,每一类用一种颜色
colors = ['b','g','r','k','c','m','y','#e24fff','#524C90','#845868']
for i in range(n_clusters):
	index = np.nonzero(labels==i)[0]
	x0 = X[index,0]
	x1 = X[index,1]
	y_i = y[index]
	for j in range(len(x0)):
		plt.text(x0[j],x1[j],str(int(y_i[j])),color=colors[i],\
				fontdict={'weight': 'bold', 'size': 9})
	plt.scatter(cents[i,0],cents[i,1],marker='x',color=colors[i],linewidths=12)
plt.title("SSE={:.2f}".format(sse))
plt.axis([-30,30,-30,30])
plt.show()

得到下图:

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.