Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import math
  3.  
  4. def plots(a1_cluster,a2_cluster,b1_cluster,b2_cluster):
  5. plt.figure()
  6. plt.plot(a1_cluster,a2_cluster,'.')
  7. plt.plot(b1_cluster,b2_cluster,'*')
  8. plt.grid(True)
  9. plt.show()
  10.  
  11. def cluster1(c1_x1,c1_x2,c2_x1,c2_x2):
  12. cluster1_x1 = []
  13. cluster1_x2 = []
  14. cluster2_x1 = []
  15. cluster2_x2 = []
  16.  
  17. for i,j in zip(x1,x2):
  18. a = math.sqrt(((i-c1_x1)**2 + (j-c1_x2)**2))
  19. b = math.sqrt(((i-c2_x1)**2 + (j-c2_x1)**2))
  20. if a < b:
  21. cluster1_x1.append(i)
  22. cluster1_x2.append(j)
  23. else:
  24. cluster2_x1.append(i)
  25. cluster2_x2.append(j)
  26.  
  27. plots(cluster1_x1,cluster1_x2,cluster2_x1,cluster2_x2)
  28.  
  29. c3_x1 = sum(cluster1_x1)/len(cluster1_x1)
  30. c3_x2 = sum(cluster1_x2)/len(cluster1_x2)
  31. c4_x1 = sum(cluster2_x1)/len(cluster2_x1)
  32. c4_x2 = sum(cluster2_x2)/len(cluster2_x2)
  33.  
  34. cluster2 (c3_x1,c3_x2,c4_x1,c4_x2)
  35.  
  36. def cluster2(c3_x1,c3_x2,c4_x1,c4_x2):
  37. cluster3_x1 = []
  38. cluster3_x2 = []
  39. cluster4_x1 = []
  40. cluster4_x2 = []
  41.  
  42. for i,j in zip(x1,x2):
  43. c = math.sqrt(((i-c3_x1)**2 + (j-c3_x2)**2))
  44. d = math.sqrt(((i-c4_x1)**2 + (j-c4_x2)**2))
  45. if c < d:
  46. cluster3_x1.append(i)
  47. cluster3_x2.append(j)
  48. else:
  49. cluster4_x1.append(i)
  50. cluster4_x2.append(j)
  51.  
  52. plots(cluster3_x1,cluster3_x2,cluster4_x1,cluster4_x2)
  53.  
  54. x1 = [15, 19, 15, 5, 13, 17, 15, 12, 8, 6, 9, 13]
  55. x2 = [13, 16, 17, 6, 17, 14, 15, 13, 7, 6, 4, 12]
  56.  
  57. plots(x1,x2,[],[])
  58. cluster1(x1[4],x2[4],x1[10],x2[10])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement