# import numpy as np import math import random def normal mu sigma retur

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92``` ```import numpy as np import math import random def normal(x, mu, sigma): return (1/(sigma*(2*math.pi)**0.5))*math.exp(-(x - 2.*random.random() - mu))**2/(2*sigma**2) def normal_distribution(mu, sigma): return sigma*random.random() + mu dots = [] x, sigma = 100, 0.5 for mu in [2., 4., 6.]: dots += [[normal_distribution(mu, sigma),normal_distribution(mu, sigma), normal_distribution(mu, sigma)] for _ in range(3)] print dots #print '[' #for [a, b, c] in dots: # print a ,',', b, ',', c, ';' #print ']' def metrics(point_1, point_2): [x1, y1, z1] = point_1 print 'point_1', point_1 [x2, y2, z2] = point_2 print 'point_2', point_2 a = (x1-x2)**2 + (y1-y2)**2 + (z1-z2)**2 print 'metrix = ', a return a a = metrics([1,1,1], [0, 0, 0]) print a n = 3 centers = 6.0*np.random.random((n, 3)) print centers def get_nearest(x, centers): return np.argmin([metrics(x, c) for c in centers]) lables = [get_nearest(dot, centers) for dot in dots] print 'lables' print lables def recalck_center(dots, indexes): x, y, z = 0., 0., 0. for i in indexes: [a, b, c] = dots[i] x += a y += b z += c return [x / len(indexes), y / len(indexes), z / len(indexes)] def get_dots_for_labels(klaster_index, lables_for_all): result = [] i=0 print 'for klaster ', klaster_index for lable in lables_for_all: if klaster_index == lable: result += [i] i += 1 print 'vot rezultat:' print result return result centers = 6.0*np.random.random((3, 3)) centers = [[2., 2., 2.], [4., 4., 4.], [6., 6., 6.]] print 'centers:' print centers def my_kmeans(dots, klasters_number, iterations=3): centers = 6.0*np.random.random((klasters_number, 3)) for i in xrange(iterations): lables = [get_nearest(dot, centers) for dot in dots] for klaster_index in range(klasters_number): current_claster_indexes = get_dots_for_labels(klaster_index, lables) #print current_claster_indexes #centers[klaster_index] = recalck_center(dots, current_claster_indexes) return lables my_kmeans(dots, 3) ```