示例#1
0
import numpy as np
import time
import matplotlib.pyplot as plt
import kmeans

## step 1: load data
print("step 1: load data...")
dataSet = []
fileIn = open('testSet.txt')
for line in fileIn.readlines():
    lineArr = line.strip().split('\t')
    dataSet.append([float(lineArr[0]), float(lineArr[1])])

## step 2: clustering...
print("step 2: clustering...")
dataSet = np.mat(dataSet)
k = 4
centroids, clusterAssment = kmeans.kmeans(dataSet, k)

## step 3: show the result
print("step 3: show the result...")
kmeans.showCluster(dataSet, k, centroids, clusterAssment)
from numpy import *  
import time  
import matplotlib.pyplot as plt
import kmeans  
  
## step 1: load data  
print ("step 1: load data...")  
dataSet = []  
fileIn = open('D:/AI/Assignment4/realdata.txt')  
for line in fileIn.readlines():  
    lineArr = line.strip().split('\t')  
    dataSet.append([float(lineArr[0]), float(lineArr[1])])  
  
## step 2: clustering...  
print ("step 2: clustering..." ) 
dataSet = mat(dataSet)  
k = 4  
centroids, clusterAssment = kmeans.kmeans(dataSet, k)   
## step 3: show the result  
print ("step 3: show the result...")  
kmeans.showCluster(dataSet, k, centroids, clusterAssment)
# Date   : 2013-12-25
# HomePage : http://blog.csdn.net/zouxy09
# Email  : [email protected]
#################################################

from numpy import *
import time
import matplotlib.pyplot as plt
from kmeans import kmeans
from kmeans import showCluster
# step 1: load data
print "step 1: load data..."
dataSet = []
fileIn = open('testSet.txt')
for line in fileIn.readlines():
    lineArr = line.strip().split('\t')
    dataSet.append([float(lineArr[0]), float(lineArr[1])])

# step 2: clustering...
print "step 2: clustering..."
dataSet = mat(dataSet)
for i in xrange(dataSet.shape[0]):
    plt.plot(dataSet[i, 0], dataSet[i, 1], 'or')
plt.show()
k = 4
centroids, clusterAssment = kmeans(dataSet, k)

# step 3: show the result
print "step 3: show the result..."
showCluster(dataSet, k, centroids, clusterAssment)