diff --git a/homework_03_kmeans/homework/3-1.py b/homework_03_kmeans/homework/3-1.py new file mode 100644 index 0000000..80572fe --- /dev/null +++ b/homework_03_kmeans/homework/3-1.py @@ -0,0 +1,31 @@ +''' +Author: SJ2050 +Date: 2021-11-05 23:21:46 +LastEditTime: 2021-11-07 15:14:50 +Version: v0.0.1 +Description: Homework3.1. +Copyright © 2021 SJ2050 +''' +import numpy as np +from basic_kMeans import read_csv_data, show_figure, KMeans + +if __name__ == '__main__': + data_file = 'dataset_circles.csv' + original_data = read_csv_data(data_file) + + distance_func = lambda p1, p2: np.linalg.norm(p1 - p2) + def compute_center_point_func(points): + n = len(points) + return sum(points)/n + + clustered_points, center_points, iter_num, cost = KMeans(original_data[:, 0:2], 2, \ + distance_func, \ + compute_center_point_func, \ + 1000, 1e-3).run() + print(f'迭代次数: {iter_num}, 代价函数值为: {cost:.3f}') + + classified_points = np.array([(clustered_points[i][j][0], clustered_points[i][j][1], i) \ + for i in range(2) \ + for j in range(len(clustered_points[i]))]) + center_points = [(center_points[i][0], center_points[i][1], i) for i in range(2)] + show_figure(classified_points, center_points) diff --git a/homework_03_kmeans/homework/3-2.py b/homework_03_kmeans/homework/3-2.py new file mode 100644 index 0000000..5e74bc3 --- /dev/null +++ b/homework_03_kmeans/homework/3-2.py @@ -0,0 +1,32 @@ +''' +Author: SJ2050 +Date: 2021-11-07 13:49:04 +LastEditTime: 2021-11-07 15:05:48 +Version: v0.0.1 +Description: Homework3.2. +Copyright © 2021 SJ2050 +''' +import numpy as np +from basic_kMeans import read_csv_data, show_figure, KMeans + +if __name__ == '__main__': + data_file = 'dataset_circles.csv' + original_data = read_csv_data(data_file) + + distance_func = lambda p1, p2: abs(np.linalg.norm(p1)-np.linalg.norm(p2)) + def compute_center_point_func(points): + n = len(points) + return np.array([0 ,sum(np.linalg.norm(points, axis=1))/n]) + + clustered_points, center_points, iter_num, cost = KMeans(original_data[:, 0:2], 2, \ + distance_func, \ + compute_center_point_func, \ + 1000, 1e-3).run() + print(f'迭代次数: {iter_num}, 代价函数值为: {cost:.3f}') + + classified_points = np.array([(clustered_points[i][j][0], clustered_points[i][j][1], i) \ + for i in range(2) \ + for j in range(len(clustered_points[i]))]) + center_points = [(center_points[i][0], center_points[i][1], i) for i in range(2)] + show_figure(classified_points, center_points) + diff --git a/homework_03_kmeans/homework/3-3.py b/homework_03_kmeans/homework/3-3.py new file mode 100644 index 0000000..bacc092 --- /dev/null +++ b/homework_03_kmeans/homework/3-3.py @@ -0,0 +1,104 @@ +''' +Author: SJ2050 +Date: 2021-11-07 15:13:40 +LastEditTime: 2021-11-07 21:14:18 +Version: v0.0.1 +Description: Use a density-based clustering algorithm. +Copyright © 2021 SJ2050 +''' +import numpy as np +import random +from basic_kMeans import read_csv_data, show_figure + +def compute_distances(points): + """Compute the distances between every two points. + + Args: + points: All points. + + Returns: + distances: The distances between every two points. + + """ + points = np.array(points, dtype=np.float64) + n = len(points) + distances = np.zeros((n, n)) + + for i in range(n): + for j in range(n): + if i <= j: + distances[i, j] = np.linalg.norm(points[i]-points[j]) + else: + distances[i, j] = distances[j, i] + + return distances + +def find_neighbors(distances, p_ind, eps): + """Find neighbors of point p. + + When the distance between two points is less than eps, they are considered as neighbors. + + Args: + distances: The distances between every two points (numpy 2d array). + p_ind: Reference point's index. + eps: The distance between two neighbors should be less than eps. + + Returns: + neighbors: A set of the reference point's neighbors' indices. + """ + neighbors = {i for i in range(distances.shape[0]) if distances[p_ind][i] < eps and p_ind != i} + + return neighbors + +def DBSCAN(points, eps, min_Pts): + """A density-based clustering algorithm + + Args: + points: All points. + eps: The distance between two neighbors should be less than eps. + min_Pts: Each core object should at least have `min_Pts` neighbors. + + Returns: + clusters: Clusters of given points. + """ + # initialization + distances = compute_distances(points) + each_point_neighbors = [find_neighbors(distances, i, eps) for i in range(len(points))] + core_objects = {i for i in range(len(points)) \ + if len(each_point_neighbors[i]) >= min_Pts} + unvisited_indices = {i for i in range(len(points))} + + clusters = [] + while len(core_objects) > 0: + unvisited_indices_old = unvisited_indices.copy() + core_obj = random.choice(list(core_objects)) + Q = [core_obj] + unvisited_indices = unvisited_indices - {core_obj} + + while len(Q) > 0: + q = Q.pop(0) + Nq = each_point_neighbors[q] + if len(Nq) >= min_Pts: + delta = Nq & unvisited_indices + Q.extend(list(delta)) + unvisited_indices = unvisited_indices - delta + + Ck = unvisited_indices_old - unvisited_indices + clusters.append(Ck) + core_objects = core_objects - Ck + + clusters.append(unvisited_indices) + return clusters + +if __name__ == '__main__': + data_file = 'dataset_circles.csv' + original_data = read_csv_data(data_file) + points = original_data[:, :2] + eps = 5 + min_Pts = 5 + + clusters = DBSCAN(points, eps, min_Pts) + classified_points = [(points[j][0], points[j][1], i) \ + for i in range(len(clusters)) \ + for j in clusters[i]] + show_figure(classified_points, []) diff --git a/homework_03_kmeans/homework/basic_kMeans.py b/homework_03_kmeans/homework/basic_kMeans.py new file mode 100644 index 0000000..61fb59f --- /dev/null +++ b/homework_03_kmeans/homework/basic_kMeans.py @@ -0,0 +1,139 @@ +''' +Author: SJ2050 +Date: 2021-11-05 23:21:46 +LastEditTime: 2021-11-07 15:10:24 +Version: v0.0.1 +Description: Basic K-Means algorithm. +Copyright © 2021 SJ2050 +''' +import csv +import random +import numpy as np +from matplotlib import pyplot as plt + +def read_csv_data(data_file): + """Read data from file(.csv format). + + Args: + data_file: Raw data file(.csv). + + Returns: + A 2d numpy array of data from input file. + """ + with open(data_file, 'r') as fp_inp_data: + reader = csv.reader(fp_inp_data) + result = [(item[0], item[1], item[2]) for item in reader] + + return np.array(result, dtype=np.float64) + +def show_figure(points, center_points): + """Show result through figure. + + Args: + points: Points (size: N*3). For each point, first and second components represent location + and third compoint represents class type. + center_points: Center points to be highlighted. + + Returns: + None. + """ + points = np.array(points) + x = points[:, 0] + y = points[:, 1] + c = points[:, 2] + plt.scatter(x, y, s=20, c=c) + + if len(center_points) > 0: + center_points = np.array(center_points) + center_x = center_points[:, 0] + center_y = center_points[:, 1] + center_c = center_points[:, 2] + plt.scatter(center_x, center_y, s=100, c=center_c, marker='x') + + plt.show() + +class KMeans(): + """Basic KMeans class. + """ + def __init__(self, points, class_num, distance_func, compute_center_point_func, \ + max_iter_num, atol): + self.points = np.array(points, dtype=np.float64) + self.class_num = class_num + self.distance_func = distance_func + self.compute_center_point_func = compute_center_point_func + self.max_iter_num = max_iter_num + self.atol = atol + self.clustered_points = [] + self.center_points = [] + + @property + def cost(self): + assert len(self.clustered_points) == self.class_num, "点簇数目与分类数不一致" + assert len(self.center_points) == self.class_num, "中点的个数与分类数不一致" + + cost = 0 + for k in range(self.class_num): + for i in range(len(self.clustered_points[k])): + cost += self.distance_func(self.clustered_points[k][i], self.center_points[k])**2 + + return cost + + def choose_which_class_belonging_to(self, point): + distances = [self.distance_func(point, self.center_points[i]) for i in range(len(self.center_points))] + return np.argmin(distances) + + def initialize(self): + n = len(self.points) + center_points_indices = random.sample([i for i in range(n)], self.class_num) + self.center_points = self.points[center_points_indices] + self.clustered_points = [[] for i in range(self.class_num)] + + for p in self.points: + belonging_class = self.choose_which_class_belonging_to(p) + self.clustered_points[belonging_class].append(p) + + def cluster(self): + self.center_points = [self.compute_center_point_func(self.clustered_points[i]) for i in range(self.class_num)] + + self.clustered_points = [[] for i in range(self.class_num)] + + for p in self.points: + belonging_class = self.choose_which_class_belonging_to(p) + self.clustered_points[belonging_class].append(p) + + def run(self): + self.initialize() + prev_cost = None + curr_cost = self.cost + for i in range(self.max_iter_num): + if prev_cost and abs(curr_cost - prev_cost) < self.atol: + break + self.cluster() + prev_cost = curr_cost + curr_cost = self.cost + + return self.clustered_points, self.center_points, i, curr_cost + + + +if __name__ == '__main__': + # test + def compute_center_point(points): + n = len(points) + return sum(points)/n + + data_file = 'dataset_circles.csv' + original_data = read_csv_data(data_file) + + distance_func = lambda p1, p2: np.linalg.norm(p1 - p2) + clustered_points, center_points, iter_num, cost = KMeans(original_data[:, 0:2], 2, \ + distance_func, \ + compute_center_point, \ + 1000, 1e-3).run() + print(f'迭代次数: {iter_num}, 代价函数值为: {cost:.3f}') + + classified_points = np.array([(clustered_points[i][j][0], clustered_points[i][j][1], i) \ + for i in range(2) \ + for j in range(len(clustered_points[i]))]) + center_points = [(center_points[i][0], center_points[i][1], i) for i in range(2)] + show_figure(classified_points, center_points) diff --git a/homework_03_kmeans/homework/dataset_circles.csv b/homework_03_kmeans/homework/dataset_circles.csv new file mode 100644 index 0000000..8304452 --- /dev/null +++ b/homework_03_kmeans/homework/dataset_circles.csv @@ -0,0 +1,400 @@ +9.062095432950300733e+00,8.410568609122412553e+00,0.000000000000000000e+00 +-1.343405517875576327e-01,9.609814870236448314e+00,0.000000000000000000e+00 +-1.176738926012567887e+01,4.803147976205272840e-02,0.000000000000000000e+00 +-8.187928226885320404e-01,1.049254653793298786e+01,0.000000000000000000e+00 +4.191120791997052564e+00,-4.859337353049694386e+00,0.000000000000000000e+00 +7.278099468846941811e+00,4.440088331048181125e+00,0.000000000000000000e+00 +-1.900275675122245822e+00,-6.757950269983900249e+00,0.000000000000000000e+00 +-1.160051532664573593e+01,1.280720324354462258e+00,0.000000000000000000e+00 +-6.734042851045059841e+00,-9.662029276543851353e+00,0.000000000000000000e+00 +-1.590917618471234363e+00,-6.644126612710519630e+00,0.000000000000000000e+00 +-9.658061682157873307e+00,-5.631666951657030573e+00,0.000000000000000000e+00 +6.546514702885141146e-01,6.904946644254467358e+00,0.000000000000000000e+00 +5.825089114426075909e+00,-2.076091058228922481e+00,0.000000000000000000e+00 +-9.910757689306716856e+00,-5.394888599354432657e+00,0.000000000000000000e+00 +-1.066332396060839649e+01,-3.174463528066445495e+00,0.000000000000000000e+00 +-1.099429752168805585e+01,-5.286987975574418464e+00,0.000000000000000000e+00 +8.375759989492184587e+00,6.733285003867059082e+00,0.000000000000000000e+00 +8.244104799198593625e+00,-1.003506301778202925e+01,0.000000000000000000e+00 +9.942981682567344492e+00,-8.636124563447859614e+00,0.000000000000000000e+00 +8.033997548512344267e+00,2.515512562581684874e-01,0.000000000000000000e+00 +-6.428504738220388148e+00,-5.097581256039596553e+00,0.000000000000000000e+00 +-6.314421031249486838e+00,-5.175909050362075270e-01,0.000000000000000000e+00 +1.164619887502898354e+00,-1.320994498120528782e+01,0.000000000000000000e+00 +6.696829444560168376e+00,7.182729888738806245e+00,0.000000000000000000e+00 +-4.235246305491734198e+00,-7.159356454934342473e+00,0.000000000000000000e+00 +5.383052664452613989e+00,6.014628330823282631e+00,0.000000000000000000e+00 +-7.446474143451298211e+00,-3.072619184086839539e+00,0.000000000000000000e+00 +6.760530300065351383e+00,6.491756034733043634e+00,0.000000000000000000e+00 +2.734445225180177186e+00,-6.903850617524835087e+00,0.000000000000000000e+00 +-1.253932770562744814e+01,2.506102182116135779e+00,0.000000000000000000e+00 +-8.312994495650205096e+00,-9.668901686019307107e+00,0.000000000000000000e+00 +3.575184886406485685e+00,9.829701914520594386e+00,0.000000000000000000e+00 +9.210873348198441946e+00,-4.658467065733349899e-01,0.000000000000000000e+00 +2.294493514654305955e+00,-1.135717565800940143e+01,0.000000000000000000e+00 +7.417559270330603916e+00,-4.122257939560736162e+00,0.000000000000000000e+00 +7.852285329993975438e-01,-1.377764842065872308e+01,0.000000000000000000e+00 +-8.592381094680053621e+00,-1.036725741900020736e+01,0.000000000000000000e+00 +-9.463897506708443430e+00,-3.630513378318991791e+00,0.000000000000000000e+00 +2.926882716663875783e+00,5.282811617135619642e+00,0.000000000000000000e+00 +6.636824917046443950e+00,-6.325590953010028272e+00,0.000000000000000000e+00 +-3.206988557987293653e+00,7.029410513631201063e+00,0.000000000000000000e+00 +-4.483462085384717355e+00,-1.248765239318190368e+01,0.000000000000000000e+00 +-8.204039706532734400e+00,-8.828124739392466225e+00,0.000000000000000000e+00 +-4.491149550111644961e-01,-1.294422352630821038e+01,0.000000000000000000e+00 +7.584652864309679998e+00,-5.780844478800141850e+00,0.000000000000000000e+00 +-8.397574930127827741e+00,-6.072174091712802202e+00,0.000000000000000000e+00 +9.536024040617339992e+00,-4.885791167647323796e+00,0.000000000000000000e+00 +6.204913449369826139e+00,3.534932823231177856e+00,0.000000000000000000e+00 +5.343362143920028728e+00,8.020288325263983609e+00,0.000000000000000000e+00 +-1.100612678432352354e+01,8.711273195141605585e-01,0.000000000000000000e+00 +4.041258899744661015e+00,-4.531661311465215114e+00,0.000000000000000000e+00 +-1.081323332747492927e+01,-3.850249842643757026e-01,0.000000000000000000e+00 +-1.651469177720229942e+00,9.092961241515524762e+00,0.000000000000000000e+00 +-3.598983124757430652e+00,6.034186070324961015e+00,0.000000000000000000e+00 +4.857977932252491193e+00,-4.455138316533950871e+00,0.000000000000000000e+00 +-8.556999694314926685e+00,-9.276087351455689856e+00,0.000000000000000000e+00 +1.646002476627940636e+00,5.914559167101281822e+00,0.000000000000000000e+00 +-6.747697232463481853e+00,1.166916843249992475e+01,0.000000000000000000e+00 +1.119233611961231745e+01,-7.945378535110935836e+00,0.000000000000000000e+00 +-4.891468942769719774e+00,5.718555798419916236e+00,0.000000000000000000e+00 +-5.016619212735019540e-01,6.283371595425017553e+00,0.000000000000000000e+00 +-7.914146529220673187e+00,2.297769580684648982e+00,0.000000000000000000e+00 +-6.976824699389136519e+00,-7.473491510144525840e+00,0.000000000000000000e+00 +2.279976267026866676e-01,6.685022805602209850e+00,0.000000000000000000e+00 +2.775182125658465004e+00,-6.530772876454421727e+00,0.000000000000000000e+00 +-1.152484573953299751e+01,-3.781685492550360461e+00,0.000000000000000000e+00 +-7.665724454478979766e+00,1.061841609529312258e+01,0.000000000000000000e+00 +-6.050947749312078017e+00,-8.308928997465619304e+00,0.000000000000000000e+00 +1.127772297836719950e+01,5.211296464343789125e+00,0.000000000000000000e+00 +-1.113470689407102654e+01,6.260234650393305067e+00,0.000000000000000000e+00 +2.341543601664675567e+00,7.532088552934100179e+00,0.000000000000000000e+00 +-4.151751846745349361e+00,-7.490335637551074122e+00,0.000000000000000000e+00 +-7.805147815211818774e+00,3.243245274640753184e+00,0.000000000000000000e+00 +4.336434630574473204e+00,6.006705659813466269e+00,0.000000000000000000e+00 +2.212679442407554653e+00,7.218280668172093684e+00,0.000000000000000000e+00 +5.220755351664055688e+00,-4.094926467596918584e+00,0.000000000000000000e+00 +1.112747926534001941e+01,3.850834041798686602e+00,0.000000000000000000e+00 +-2.595587501786106532e+00,-9.806357366695770850e+00,0.000000000000000000e+00 +-9.028343948742850955e+00,8.915749548117700485e+00,0.000000000000000000e+00 +1.126634632652508117e+01,-6.482595170923743488e+00,0.000000000000000000e+00 +-3.286842484116981655e+00,-7.646127648092450713e+00,0.000000000000000000e+00 +7.419845811945131153e+00,9.565847320886607719e+00,0.000000000000000000e+00 +5.978018377910639281e+00,1.217238176923184412e+01,0.000000000000000000e+00 +-6.711146678585069303e+00,-4.722415378405640851e+00,0.000000000000000000e+00 +-8.485783184510578891e+00,5.672952800632031334e+00,0.000000000000000000e+00 +-1.144014291076578083e+01,-3.478202066131522052e+00,0.000000000000000000e+00 +4.384873230300504687e+00,-5.549646589217950066e+00,0.000000000000000000e+00 +-1.168300744153292747e+01,-5.194818386960251999e+00,0.000000000000000000e+00 +-9.644136031176124835e-01,1.049805166565995762e+01,0.000000000000000000e+00 +-6.536842892201116584e+00,5.345306757313686319e+00,0.000000000000000000e+00 +8.438989962482976281e+00,-3.475208865011710557e+00,0.000000000000000000e+00 +6.506186462480673782e+00,-3.744626303119004684e-01,0.000000000000000000e+00 +-3.862160515126369820e+00,5.446476112276574355e+00,0.000000000000000000e+00 +6.134051169689144878e+00,-1.978516060449600378e+00,0.000000000000000000e+00 +1.185204996490637264e+01,-6.124766316013841028e+00,0.000000000000000000e+00 +-4.859569339875388749e+00,6.353425638518374718e+00,0.000000000000000000e+00 +-1.519343617847903394e+00,-1.248943950276429504e+01,0.000000000000000000e+00 +-1.144861509135489541e+01,6.053286005181219842e+00,0.000000000000000000e+00 +4.331788290673597608e+00,-6.109292230414607339e+00,0.000000000000000000e+00 +3.334070762472415517e+00,5.456281297241006634e+00,0.000000000000000000e+00 +-1.065870788968298122e+01,-2.340567870836013586e+00,0.000000000000000000e+00 +-1.463630422294582845e+00,-1.001221660907637023e+01,0.000000000000000000e+00 +-3.258706257441662313e+00,-1.088106300955284844e+01,0.000000000000000000e+00 +6.275135646820408475e+00,-1.534938715019705135e+00,0.000000000000000000e+00 +2.021073821693852857e+00,-8.018368767233782890e+00,0.000000000000000000e+00 +-3.182102089242570653e+00,-1.088037918116285319e+01,0.000000000000000000e+00 +5.481629019877129139e+00,1.022320040714743250e+01,0.000000000000000000e+00 +-8.330105300232894194e+00,-8.288555228352629811e+00,0.000000000000000000e+00 +-2.755674768703163746e+00,1.187433721777218132e+01,0.000000000000000000e+00 +-4.683040648747073043e+00,8.164208797263796669e+00,0.000000000000000000e+00 +1.119515335900578279e-01,1.335746486991257598e+01,0.000000000000000000e+00 +6.700119633932653862e+00,1.082193885842432124e+01,0.000000000000000000e+00 +-8.550301619333071201e+00,3.419675931901079213e+00,0.000000000000000000e+00 +7.176748453521003768e+00,-1.025820308032380268e+01,0.000000000000000000e+00 +-5.041595214606491737e+00,9.063531739248613661e+00,0.000000000000000000e+00 +1.194486108691321880e+00,-1.044990485316076168e+01,0.000000000000000000e+00 +-9.686906870031076977e+00,-3.619449339125635490e+00,0.000000000000000000e+00 +-1.313845064569455801e+01,-8.365893417087570949e-01,0.000000000000000000e+00 +4.065075556680537439e+00,-5.805028193121499314e+00,0.000000000000000000e+00 +8.590045804906507598e+00,-1.083068171753233067e+01,0.000000000000000000e+00 +5.126125837069420932e+00,-4.406654442924812010e+00,0.000000000000000000e+00 +1.353837046348684758e+01,-5.790225488579785473e-01,0.000000000000000000e+00 +1.111931563740836282e+01,-2.045835439299618752e+00,0.000000000000000000e+00 +8.023788031578961366e+00,8.147772272168204211e+00,0.000000000000000000e+00 +-7.017294184359236908e+00,5.831118776551057259e+00,0.000000000000000000e+00 +-1.141949019659495335e+01,3.877836369592378762e+00,0.000000000000000000e+00 +-6.568151289332938014e-02,1.385097687202117989e+01,0.000000000000000000e+00 +1.198497403418259211e+01,5.562064536545864579e-02,0.000000000000000000e+00 +-8.044794464020926128e+00,7.363341681713536646e-01,0.000000000000000000e+00 +-1.338591590468644377e+01,-4.094229452548190373e+00,0.000000000000000000e+00 +5.673372417420740055e-01,8.086269673969569638e+00,0.000000000000000000e+00 +3.516308154983770695e+00,-6.695867415477852802e+00,0.000000000000000000e+00 +1.281549230629557101e+01,5.502104265343782608e+00,0.000000000000000000e+00 +6.978521281232607620e+00,4.489103854629955315e+00,0.000000000000000000e+00 +-9.492072557224918938e+00,-2.860723420255278260e+00,0.000000000000000000e+00 +6.435965292187971976e+00,-2.007430821741474691e+00,0.000000000000000000e+00 +9.787301848081414235e+00,-3.385987285381478085e+00,0.000000000000000000e+00 +-2.560272080421012397e+00,-1.060761918876632492e+01,0.000000000000000000e+00 +-5.741879482881461882e+00,7.675351118488469737e+00,0.000000000000000000e+00 +4.909833311186836191e+00,-7.884078238568854147e+00,0.000000000000000000e+00 +8.530059544583025399e+00,-8.271604816591201725e+00,0.000000000000000000e+00 +-8.137833035927506842e+00,4.752299354840485712e+00,0.000000000000000000e+00 +-2.945364290828344167e+00,1.181024110146612216e+01,0.000000000000000000e+00 +6.413013358841276101e+00,2.686811429118250771e+00,0.000000000000000000e+00 +-4.330576883415815814e+00,-4.312151816386741388e+00,0.000000000000000000e+00 +7.819651364870956911e+00,5.581944049584605771e+00,0.000000000000000000e+00 +6.374923678964727003e+00,-3.591063433682733397e+00,0.000000000000000000e+00 +3.920317473054968804e+00,-1.285867128337139675e+01,0.000000000000000000e+00 +5.969112688202600658e+00,-1.199043474049174485e+01,0.000000000000000000e+00 +2.206761741887119044e+00,1.231498183866549212e+01,0.000000000000000000e+00 +-4.493238306901402301e-02,-1.241246581439543739e+01,0.000000000000000000e+00 +-1.229500249305082527e+01,-3.152142610476000417e+00,0.000000000000000000e+00 +-4.249457240746512987e+00,9.692207951492818552e+00,0.000000000000000000e+00 +1.124606336305911114e+01,-7.885118833499940028e-01,0.000000000000000000e+00 +-1.059106427242662107e+01,7.468063051536828034e+00,0.000000000000000000e+00 +2.281377873306516868e+00,8.258766252373336059e+00,0.000000000000000000e+00 +2.776342194763606885e-01,1.145304502140566072e+01,0.000000000000000000e+00 +-1.779778342255576185e+00,1.103845161819147513e+01,0.000000000000000000e+00 +-1.138825413050090596e+01,1.875254253119878145e+00,0.000000000000000000e+00 +6.794503066116404089e-01,6.595367984622427571e+00,0.000000000000000000e+00 +4.977831129044696823e+00,-4.344026277914559309e+00,0.000000000000000000e+00 +1.741129599087095914e-01,-6.894034268664321452e+00,0.000000000000000000e+00 +2.475097088111755106e+00,-1.093174226816173800e+01,0.000000000000000000e+00 +3.991853833727508238e+00,1.121533736074506393e+01,0.000000000000000000e+00 +-1.686119504345058084e+00,1.009423236139700819e+01,0.000000000000000000e+00 +-1.317728548656465115e+01,4.029926630934554055e+00,0.000000000000000000e+00 +-7.730210435536935876e+00,-3.848806311305841277e+00,0.000000000000000000e+00 +-5.953807133460072620e+00,-6.561968042369668019e+00,0.000000000000000000e+00 +4.341106468164319665e+00,5.866644648406567519e+00,0.000000000000000000e+00 +-1.305998549861713798e+01,-3.139159220886123869e+00,0.000000000000000000e+00 +-6.907088887148628231e+00,2.993819425329168560e+00,0.000000000000000000e+00 +-7.307345974834201563e+00,-4.562759938513091029e+00,0.000000000000000000e+00 +6.485560801850815515e+00,9.021112597985857517e-01,0.000000000000000000e+00 +-9.370938668690817153e+00,-5.994195588713087552e+00,0.000000000000000000e+00 +9.617050841017174889e+00,-8.612082699874843428e+00,0.000000000000000000e+00 +9.409055950093341281e+00,-4.741431344911000156e-01,0.000000000000000000e+00 +-2.289336158713492608e+00,-7.659638808729879678e+00,0.000000000000000000e+00 +-1.380307893247301410e+00,-1.191093891786717762e+01,0.000000000000000000e+00 +9.393772070883024128e+00,1.529664323780117297e+00,0.000000000000000000e+00 +-1.516956642220887330e+00,-1.318124950821260200e+01,0.000000000000000000e+00 +-7.540611170155883336e+00,-6.656209192745754955e+00,0.000000000000000000e+00 +6.043360171054082741e+00,2.972465656442218052e+00,0.000000000000000000e+00 +1.043883027536696773e+01,-9.223050403079646742e+00,0.000000000000000000e+00 +-6.494305285412059980e+00,-9.357575042166038637e-01,0.000000000000000000e+00 +-8.233458392848859830e+00,5.152019730950110343e+00,0.000000000000000000e+00 +-8.182185219358300898e+00,2.576432004853596691e+00,0.000000000000000000e+00 +-7.296388033822859498e+00,-9.827599822187037759e+00,0.000000000000000000e+00 +1.086329745254326973e+01,-5.918393716619380074e-01,0.000000000000000000e+00 +3.517974198525071650e+00,-6.776790760325667407e+00,0.000000000000000000e+00 +-4.619640812695988252e+00,4.293083132292331072e+00,0.000000000000000000e+00 +-7.575960827573930523e+00,1.906506832631534865e+00,0.000000000000000000e+00 +4.216280904076635139e+00,-9.501055347552291863e+00,0.000000000000000000e+00 +-3.913629666268576557e+00,-1.058803850666876833e+01,0.000000000000000000e+00 +-5.407697948813054545e+00,9.037098612814480347e+00,0.000000000000000000e+00 +-9.573363214934863308e+00,5.940753943407081294e+00,0.000000000000000000e+00 +-4.206161487099325136e+00,-1.309635619914643634e+01,0.000000000000000000e+00 +-1.847425878338489147e+00,-1.136719147233808869e+01,0.000000000000000000e+00 +-6.538610721752731614e+00,6.888582381247962516e+00,0.000000000000000000e+00 +7.392422157939151361e+00,-2.466102574943974446e+00,0.000000000000000000e+00 +1.481439456830948798e+00,1.123186173935424748e+01,0.000000000000000000e+00 +1.573273104063076921e+01,1.868971488131447956e+01,1.000000000000000000e+00 +-1.099399377759256780e+01,2.390619522460748669e+01,1.000000000000000000e+00 +2.147678989689537588e+01,-8.599456107960150586e-01,1.000000000000000000e+00 +-2.627270035353325994e+01,-1.252577732524266185e+00,1.000000000000000000e+00 +-2.818139029858682676e+01,-3.649359736607879157e+00,1.000000000000000000e+00 +-2.628880642668411927e+00,2.321951252726932324e+01,1.000000000000000000e+00 +4.756784474337030666e+00,2.277609552632346990e+01,1.000000000000000000e+00 +1.085358516551754704e+01,2.244033595971868067e+01,1.000000000000000000e+00 +9.953077736469481351e+00,2.433413308722969148e+01,1.000000000000000000e+00 +7.897350785936433581e+00,-2.511574616187301956e+01,1.000000000000000000e+00 +-2.686822103988273369e+01,-6.847214913897639477e+00,1.000000000000000000e+00 +-2.281779599215670640e+00,-2.525501734479204430e+01,1.000000000000000000e+00 +1.939928400570243738e+01,-1.464288091459299856e+01,1.000000000000000000e+00 +-2.137322440460813056e+01,-4.253473020316800857e-01,1.000000000000000000e+00 +-2.801982415469146659e+01,6.797937345022894706e-02,1.000000000000000000e+00 +1.580530092266972098e+01,-1.944844405031079404e+01,1.000000000000000000e+00 +8.514232141761185702e+00,2.578196439746885105e+01,1.000000000000000000e+00 +9.161537436501838716e-01,2.126087176747068952e+01,1.000000000000000000e+00 +-2.100910930376482355e+01,5.187756086794577115e+00,1.000000000000000000e+00 +-1.887413186880452898e+01,-9.446453231504504444e+00,1.000000000000000000e+00 +-1.244973267290554908e+01,-2.376720011280642453e+01,1.000000000000000000e+00 +-8.636428179695197827e+00,-2.346212683200547389e+01,1.000000000000000000e+00 +-2.000836523737761397e+01,-1.425301495177956035e+01,1.000000000000000000e+00 +1.940837366976037259e+01,-1.393076839654248467e+01,1.000000000000000000e+00 +-1.833469807838950416e+01,-1.346167122655688431e+01,1.000000000000000000e+00 +2.494705394044481750e+01,-5.494995405026316782e+00,1.000000000000000000e+00 +-1.665557705744208761e+01,-1.798221914000725263e+01,1.000000000000000000e+00 +2.572752693938492641e+01,-1.311370549041896538e+01,1.000000000000000000e+00 +-8.244290646775706222e+00,2.084206231459166858e+01,1.000000000000000000e+00 +-7.885354738212532411e+00,2.116800800201663435e+01,1.000000000000000000e+00 +-2.003647125145762686e+01,-6.608417731861560895e+00,1.000000000000000000e+00 +6.209578562958157910e+00,-2.452260025417416500e+01,1.000000000000000000e+00 +-2.173539700526108120e+01,-9.535163494027462106e-01,1.000000000000000000e+00 +-1.224412708268634908e+01,-2.009421260915946306e+01,1.000000000000000000e+00 +-1.075840330797242572e+01,1.858300890695775820e+01,1.000000000000000000e+00 +-2.600534803198775435e+01,1.591213249083642367e+00,1.000000000000000000e+00 +9.361088087715186745e+00,2.230199633678528315e+01,1.000000000000000000e+00 +2.624073342088111715e+01,5.375489823808733192e+00,1.000000000000000000e+00 +5.292561881430067672e+00,2.830901784997630344e+01,1.000000000000000000e+00 +2.078059335633197335e+01,-5.406876312280275876e+00,1.000000000000000000e+00 +1.797212134077285128e+01,1.974674511194298177e+01,1.000000000000000000e+00 +1.418423831415259961e+00,2.824393733375810811e+01,1.000000000000000000e+00 +-2.523700214170278855e+01,-1.302431052248969046e+01,1.000000000000000000e+00 +-2.359325729631554225e+01,1.193588174281041958e+01,1.000000000000000000e+00 +4.894404473767219876e+00,2.115271000609101293e+01,1.000000000000000000e+00 +-1.569787656706957790e+01,-2.226072207802415903e+01,1.000000000000000000e+00 +2.768855825457124098e+01,5.605075740675674822e+00,1.000000000000000000e+00 +-2.387485343307933405e+01,8.035087859152445944e+00,1.000000000000000000e+00 +2.228670073867370860e+01,-1.148109322020260592e+01,1.000000000000000000e+00 +-1.585909433097451071e+01,2.303658854092108399e+01,1.000000000000000000e+00 +2.357944877093274982e+01,-1.175374184082949114e+01,1.000000000000000000e+00 +-1.480393836152355291e+01,2.187304118659174179e+01,1.000000000000000000e+00 +1.913088892130262053e+01,-9.150390103571130140e+00,1.000000000000000000e+00 +1.376147828505578374e+01,2.459517012664668911e+01,1.000000000000000000e+00 +1.443518228384972168e+01,1.539166840543182602e+01,1.000000000000000000e+00 +-1.093411572583228342e+01,2.157779792143726638e+01,1.000000000000000000e+00 +1.457084675867774415e+01,2.058110886110986470e+01,1.000000000000000000e+00 +1.456059238093794583e+01,-2.130944108185470753e+01,1.000000000000000000e+00 +-8.355501425562577023e-01,-2.281181486053621299e+01,1.000000000000000000e+00 +2.704093247153310031e+01,-8.429506993953767235e+00,1.000000000000000000e+00 +-2.124312453800017408e+01,-1.526040990890658122e+01,1.000000000000000000e+00 +-2.136641894209649095e+01,3.040648060253774432e-01,1.000000000000000000e+00 +-2.612199279305723820e+01,2.209045858016001618e-01,1.000000000000000000e+00 +-1.850413196330870136e+01,-1.133828795179985072e+01,1.000000000000000000e+00 +2.110676114344656895e+01,-1.966560125193576170e+01,1.000000000000000000e+00 +-1.065571376892860300e+01,-2.383858015746411851e+01,1.000000000000000000e+00 +-2.481993646286784028e+01,-5.096020447884035853e+00,1.000000000000000000e+00 +2.623216441805807619e+01,1.074989474234195796e+01,1.000000000000000000e+00 +2.147028827385963012e+01,1.653440992215027450e+01,1.000000000000000000e+00 +-9.217202701913098650e+00,2.489761654565133853e+01,1.000000000000000000e+00 +-1.937546768872905290e+01,2.127878701127900740e+01,1.000000000000000000e+00 +-5.924662594668204108e+00,-2.798573897331829130e+01,1.000000000000000000e+00 +1.249879505486993736e+01,2.054501486018938294e+01,1.000000000000000000e+00 +-1.429705926472585453e+01,1.731857462323765162e+01,1.000000000000000000e+00 +2.506288393682114446e+01,1.023037004319879095e+00,1.000000000000000000e+00 +-2.374518316171267429e+01,-4.110056212841346479e+00,1.000000000000000000e+00 +-4.685801268652608265e+00,2.681770544346142415e+01,1.000000000000000000e+00 +-1.636465900606647139e+01,-1.400104502066578860e+01,1.000000000000000000e+00 +-3.820694096413083773e+00,-2.344946840684222877e+01,1.000000000000000000e+00 +-2.730718148408208990e+01,-5.702233129672412026e+00,1.000000000000000000e+00 +1.526350021196561180e+01,-2.195423474625367533e+01,1.000000000000000000e+00 +-2.310367333743900531e+01,-3.529448810485903198e+00,1.000000000000000000e+00 +2.214294872855819918e+01,-2.554368757533279410e+00,1.000000000000000000e+00 +2.515511456743908880e+01,5.638475578451005443e-01,1.000000000000000000e+00 +-2.610666366339259170e+01,6.393728951851616493e+00,1.000000000000000000e+00 +-2.613636115978982843e+01,2.234408800128391270e+00,1.000000000000000000e+00 +2.072443839280482791e+01,-4.241044169515961748e+00,1.000000000000000000e+00 +-2.179363306807708511e+01,-2.219080654082041804e+00,1.000000000000000000e+00 +-5.755683200863610516e+00,-2.649364343689698842e+01,1.000000000000000000e+00 +-5.857497203861713686e+00,-2.647591147085865870e+01,1.000000000000000000e+00 +-5.459558693088531811e+00,2.788104381472268400e+01,1.000000000000000000e+00 +3.427184099803823325e-01,-2.407137911651751949e+01,1.000000000000000000e+00 +1.061809394417809038e+01,-1.913013651650792468e+01,1.000000000000000000e+00 +-6.170046215751034957e+00,2.688331541601552033e+01,1.000000000000000000e+00 +2.677989002899861504e+01,3.021183912819540573e+00,1.000000000000000000e+00 +1.083062130122765510e+01,-1.830172721846291850e+01,1.000000000000000000e+00 +3.580835672240723522e+00,-2.239452967733392796e+01,1.000000000000000000e+00 +-3.044557199432845529e+00,-2.688141071009908600e+01,1.000000000000000000e+00 +-2.583745747369688672e+01,8.347225757017310954e+00,1.000000000000000000e+00 +1.762724598409085175e+01,1.726998052121857441e+01,1.000000000000000000e+00 +1.643060907548389693e+01,1.973520095431023336e+01,1.000000000000000000e+00 +2.741842920091284341e+01,-2.610174934885582942e+00,1.000000000000000000e+00 +-2.672059870628813272e+01,3.910225727005835900e-02,1.000000000000000000e+00 +1.605260460268317502e+01,2.345994894291840183e+01,1.000000000000000000e+00 +-2.049497282996839687e+01,-1.594388950125812521e+01,1.000000000000000000e+00 +-2.638077417794028179e+01,-7.506860614897715500e+00,1.000000000000000000e+00 +1.135912814594559705e+01,2.263410882293552362e+01,1.000000000000000000e+00 +-2.131198135157140072e+01,5.342497366994218844e-01,1.000000000000000000e+00 +-2.493192955221257279e+01,-3.390420038797758551e+00,1.000000000000000000e+00 +-1.920154011348070000e+01,-1.668775834010846637e+01,1.000000000000000000e+00 +-2.373950481351785768e+01,-1.189906327932049734e+01,1.000000000000000000e+00 +1.720133100073515919e+01,-1.298798646107649546e+01,1.000000000000000000e+00 +1.032928046222053275e+01,-2.109396432727021065e+01,1.000000000000000000e+00 +1.632802282077261680e+01,2.024775105547126941e+01,1.000000000000000000e+00 +1.994211809924351186e+01,2.077471006435687784e+01,1.000000000000000000e+00 +2.568300097148087247e+01,-8.861716607679204216e+00,1.000000000000000000e+00 +-2.239700074240493777e+01,3.534284268236923765e+00,1.000000000000000000e+00 +-1.004781490610434602e+01,1.987594221138026285e+01,1.000000000000000000e+00 +-2.291267046192328394e+01,-1.312952385196898675e+01,1.000000000000000000e+00 +-2.083400139916478011e+01,1.367840467092887025e+01,1.000000000000000000e+00 +-3.221901391570413331e+00,2.319656316993572887e+01,1.000000000000000000e+00 +-1.615965062141641084e+01,-1.387331136271443199e+01,1.000000000000000000e+00 +2.069747316736948761e+01,-1.147884159567122353e+01,1.000000000000000000e+00 +2.486770183110991184e+01,-2.339728497108470862e-01,1.000000000000000000e+00 +2.149483025993792040e+01,-5.457067273154549980e+00,1.000000000000000000e+00 +2.178731583466868216e+01,-8.611277695152226741e+00,1.000000000000000000e+00 +-2.819828929677607476e+01,1.494550304680388964e+00,1.000000000000000000e+00 +2.497391257077165960e+01,-2.050764603404877651e+00,1.000000000000000000e+00 +2.271904439997901903e+01,-5.323111949980447122e+00,1.000000000000000000e+00 +-2.328784573925989676e+01,6.785810459573639042e-01,1.000000000000000000e+00 +-2.848732005134823453e+01,-4.982707054711080219e-01,1.000000000000000000e+00 +-2.165765020607312863e+01,1.352688804656367871e+01,1.000000000000000000e+00 +7.967025472762848004e+00,-2.433312685834100009e+01,1.000000000000000000e+00 +-2.119818411541692527e+01,-2.418414204064400330e+00,1.000000000000000000e+00 +-1.558443623565947433e+01,1.641174419907369142e+01,1.000000000000000000e+00 +-2.082915539559499862e+01,-7.437332431993692872e+00,1.000000000000000000e+00 +2.559979330876547721e+01,-3.001953567538953926e+00,1.000000000000000000e+00 +-1.360557447237118245e+00,-2.281304107613448906e+01,1.000000000000000000e+00 +2.120883803732100858e+01,-1.446018697744938741e+01,1.000000000000000000e+00 +1.391155978732186327e+01,-1.747318229034387826e+01,1.000000000000000000e+00 +1.144475890863789580e+01,-1.972159647135347171e+01,1.000000000000000000e+00 +-1.535318812347566109e+01,-2.144716578645262572e+01,1.000000000000000000e+00 +-2.607594731370153074e+00,2.701312253739854796e+01,1.000000000000000000e+00 +-1.334053456404284077e+01,-2.084889283182081599e+01,1.000000000000000000e+00 +1.998640594085265221e+01,1.720332962609349536e+01,1.000000000000000000e+00 +2.600535599273910492e+01,-1.276148460329973844e+01,1.000000000000000000e+00 +2.053596390340138811e+01,6.685120048962751582e+00,1.000000000000000000e+00 +2.218491300901504459e+01,2.621424574218144343e+00,1.000000000000000000e+00 +2.422636853488194930e+01,-9.948171788084671618e+00,1.000000000000000000e+00 +-2.033677988510935464e+01,1.620601851879276722e+01,1.000000000000000000e+00 +1.779272758076690408e+01,1.297545655685749999e+01,1.000000000000000000e+00 +1.170332534869767827e+01,2.323496705447028177e+01,1.000000000000000000e+00 +1.943385226906604757e+01,-9.425801367760476168e+00,1.000000000000000000e+00 +1.772384009512879643e+01,-1.820490739410527681e+01,1.000000000000000000e+00 +1.840152047079322983e+01,-1.589728872981584828e+01,1.000000000000000000e+00 +3.254618704199605528e+00,-2.694420946928563154e+01,1.000000000000000000e+00 +-2.027940100142528124e+00,-2.357110419141051239e+01,1.000000000000000000e+00 +-2.079785053187988808e+01,6.969151913322670033e+00,1.000000000000000000e+00 +1.333160136635900805e+01,1.955228905670615447e+01,1.000000000000000000e+00 +-2.337230297711032279e+01,4.946199771144971002e+00,1.000000000000000000e+00 +2.824049165135675921e+01,2.421501932209903785e-01,1.000000000000000000e+00 +-1.924952663450711654e+01,1.001865500041339097e+01,1.000000000000000000e+00 +1.161828018995340273e+01,-2.316147917217766761e+01,1.000000000000000000e+00 +-2.115519342857973228e+01,9.507389082689076787e+00,1.000000000000000000e+00 +-1.631523968261841162e+01,1.663595577256764457e+01,1.000000000000000000e+00 +2.478598127000429940e+01,-1.099951470185801483e+01,1.000000000000000000e+00 +2.714127299805003801e+01,-4.214881301056182750e+00,1.000000000000000000e+00 +-2.031208480873932842e+01,5.489224324605968874e+00,1.000000000000000000e+00 +1.131393395507636335e+01,-1.911809820478688593e+01,1.000000000000000000e+00 +9.872220307355086266e-01,2.477132951265166483e+01,1.000000000000000000e+00 +2.368443361556522220e+01,6.908070614196779147e+00,1.000000000000000000e+00 +-6.550111289632817524e+00,-2.362637227568149356e+01,1.000000000000000000e+00 +7.142292479753053591e+00,-2.807003130402094726e+01,1.000000000000000000e+00 +9.220217262888104059e+00,2.318491894192909797e+01,1.000000000000000000e+00 +-1.278070362574096563e+01,2.491815532847987313e+01,1.000000000000000000e+00 +1.544822472767310906e+01,-1.905927650362333736e+01,1.000000000000000000e+00 +-1.782148007375803545e+01,-1.270499655956880680e+01,1.000000000000000000e+00 +-2.598083105734318821e+01,-1.166340717489061873e+01,1.000000000000000000e+00 +-2.115329431705612961e+01,1.922103940750838902e+01,1.000000000000000000e+00 +-8.572106253750373384e+00,2.167156366831950720e+01,1.000000000000000000e+00 +-2.222844308368978616e+01,1.216371128338294305e+01,1.000000000000000000e+00 +-1.460159793703849296e+01,-1.873375216063436000e+01,1.000000000000000000e+00 +-2.171765776703275463e+01,-1.476020540458901920e+01,1.000000000000000000e+00 +-4.238550116190669570e+00,-2.389972003984688698e+01,1.000000000000000000e+00 +6.165939327369154732e+00,-2.683570375136353903e+01,1.000000000000000000e+00 +1.719572832923617156e+01,1.677410597119486368e+01,1.000000000000000000e+00 +1.873103075511164661e+01,1.918492263728331793e+01,1.000000000000000000e+00 +-9.227368178597460613e+00,-2.473440238093422394e+01,1.000000000000000000e+00 +-1.831003118936073193e+01,2.071120293221047959e+01,1.000000000000000000e+00 +1.351990022808921310e+01,-2.261653460112551173e+01,1.000000000000000000e+00 +2.850165519451800833e+01,4.157533834040251897e+00,1.000000000000000000e+00 +-1.993243840613153850e+01,8.919474195504907499e+00,1.000000000000000000e+00 +2.363463868162161319e+01,1.369461759608625684e+01,1.000000000000000000e+00 +-2.178992915483502557e+01,1.301881154832546628e+00,1.000000000000000000e+00 +2.022320544459192959e+01,-1.805379750173514708e+01,1.000000000000000000e+00 +-7.998643143606845918e+00,-2.319610733875643760e+01,1.000000000000000000e+00 +2.618081162402923923e+01,1.037024573479389034e+01,1.000000000000000000e+00 +2.127632507298599407e+01,8.437198622827882799e-01,1.000000000000000000e+00 +-1.407077715450147792e+01,-2.020577296356315955e+01,1.000000000000000000e+00 +2.293969907863483115e+01,-1.194796862278524108e+00,1.000000000000000000e+00 diff --git a/homework_03_kmeans/homework/images/basic_kMeans.png b/homework_03_kmeans/homework/images/basic_kMeans.png new file mode 100644 index 0000000..adfbdef Binary files /dev/null and b/homework_03_kmeans/homework/images/basic_kMeans.png differ diff --git a/homework_03_kmeans/homework/images/density_cluster.png b/homework_03_kmeans/homework/images/density_cluster.png new file mode 100644 index 0000000..3577f95 Binary files /dev/null and b/homework_03_kmeans/homework/images/density_cluster.png differ diff --git a/homework_03_kmeans/homework/images/map.png b/homework_03_kmeans/homework/images/map.png new file mode 100644 index 0000000..bd62285 Binary files /dev/null and b/homework_03_kmeans/homework/images/map.png differ diff --git a/homework_03_kmeans/homework/images/original_points.png b/homework_03_kmeans/homework/images/original_points.png new file mode 100644 index 0000000..4dafb1b Binary files /dev/null and b/homework_03_kmeans/homework/images/original_points.png differ diff --git a/homework_03_kmeans/homework/images/pseudocode.png b/homework_03_kmeans/homework/images/pseudocode.png new file mode 100644 index 0000000..9648e75 Binary files /dev/null and b/homework_03_kmeans/homework/images/pseudocode.png differ diff --git a/homework_03_kmeans/homework/作业报告.md b/homework_03_kmeans/homework/作业报告.md new file mode 100644 index 0000000..a442815 --- /dev/null +++ b/homework_03_kmeans/homework/作业报告.md @@ -0,0 +1,40 @@ +# 作业报告 + +## 1. 基本的kMeans方法 + + 首先我编写了一个最基础的kMeans类,聚类方法采用简单的离最近重心进行归类,并且不进行任何映射。原始数据点如下所示: + + + +