# -*- coding:utf-8 -*-
__aithor__ = 'yangxin_ryan'
from numpy import *
from matplotlib import pyplot as plt
class K_Means(object):
def load_data_set(self, file_name):
data_set = []
fr = open(file_name)
for line in fr.readlines():
cur_line = line.strip().split("\t")
flt_line = list(map(float, cur_line))
data_set.append(flt_line)
return data_set
def dist_eclud(self, vec_a, vec_b):
return sqrt(sum(power(vec_a - vec_b, 2)))
def rand_cent(self, data_mat, k):
m, n = shape(data_mat)
centroids = mat(zeros((k, n)))
for j in range(n):
min_j = min(data_mat[:, j])
range_j = float(max(data_mat[:, j]) - min_j)
centroids[:, j] = mat(min_j + range_j * random.rand(k , 1))
return centroids
def k_means(self, data_mat, k, dis_meas=di