Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.99 KB | None | 0 0
  1. import math
  2. import sys
  3. import numpy as np
  4. import scipy.io.wavfile
  5.  
  6.  
  7. class Centroid:
  8.     _location = None
  9.     _assigned_points = []
  10.  
  11.     def __init__(self, init_location):
  12.         self._location = init_location
  13.         self._assigned_points = []
  14.  
  15.     def get_location(self):
  16.         return self._location
  17.  
  18.     def assign_point(self, point):
  19.         self._assigned_points.append(point)
  20.  
  21.     def clear_points(self):
  22.         self._assigned_points = []
  23.  
  24.     def update_location(self):
  25.         new_location = [0, 0]
  26.         size = self._assigned_points.__len__()
  27.         # Update centroid to be the average of the points in its cluster
  28.         for point in self._assigned_points:
  29.             new_location[0] = new_location[0] + point[0]
  30.             new_location[1] = new_location[1] + point[1]
  31.  
  32.         if size != 0:
  33.             new_location[0] /= size
  34.             new_location[1] /= size
  35.  
  36.         new_location[0] = round(new_location[0])
  37.         new_location[1] = round(new_location[1])
  38.  
  39.         if (self._location[0] != new_location[0] or self._location[1] != new_location[1]):
  40.             # update the centroid location
  41.             self._location = new_location
  42.             return False
  43.         return True
  44.  
  45.  
  46. def distance(x1, x2):
  47.     return math.sqrt(pow(x1[0] - x2[0], 2) + pow(x1[1] - x2[1], 2))
  48.  
  49.  
  50. def main():
  51.     sample, centroids = sys.argv[1], sys.argv[2]  # reading
  52.     fs, y = scipy.io.wavfile.read(sample)
  53.     x = np.array(y.copy())  # data
  54.     initial_centroids = np.loadtxt(centroids)  # centroids
  55.  
  56.     # the K value is the num of points in the centroid file
  57.     k = len(initial_centroids)
  58.  
  59.     # Create for each centroid his own list of points and put all the centroids in a list
  60.     centroids = []
  61.     for cent in initial_centroids:
  62.         centroids.append(Centroid(cent))
  63.  
  64.     f = open("output.txt", "a")
  65.  
  66.     is_convergence = False
  67.     i = 0
  68.     # do 30 iterations
  69.     while not is_convergence and i < 30:
  70.         is_convergence = True
  71.         # assign each point to the closest centroid to him
  72.         for point in x:
  73.             min_dist = distance(point, centroids[0].get_location())
  74.             new_cent = centroids[0]
  75.             for cent in centroids:
  76.                 dist = distance(point, cent.get_location())
  77.                 if dist < min_dist:
  78.                     min_dist = dist
  79.                     new_cent = cent
  80.             new_cent.assign_point(point)
  81.  
  82.         # update all the centroids location
  83.         for cent in centroids:
  84.             ans = cent.update_location()
  85.             if (ans == False):
  86.                 is_convergence = False
  87.  
  88.         # Print the centroids in each iteration
  89.         print(f"[iter {i}]:{','.join([str(cent.get_location()) for cent in centroids])}")
  90.         f.write(f"[iter {i}]:{','.join([str(cent.get_location()) for cent in centroids])}")
  91.         f.write("\n")
  92.         i += 1
  93.  
  94.         # clear all the assigned points
  95.         for cent in centroids:
  96.             cent.clear_points()
  97.  
  98.     f.close()
  99.  
  100.  
  101. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement