Advertisement
jamestha3d

omo3.7.3

Jun 6th, 2021
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1.  
  2.  
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. import random
  6.  
  7. N=25
  8.  
  9. r=np.random.normal(0,2,2*N);
  10. the=2*np.pi*np.random.rand(2*N);
  11.  
  12.  
  13. x=np.ones([2*N,2]);  
  14.  
  15.  
  16. x[0:N,0]=5 + r[0:N]*np.cos(the[0:N])
  17. x[0:N,1]=10 + r[0:N]*np.sin(the[0:N])
  18. x[N:2*N,0]=10 + r[N:2*N]*np.cos(the[N:2*N])
  19. x[N:2*N,1]=5 + r[N:2*N]*np.sin(the[N:2*N])
  20.  
  21.  
  22. n = random.sample(list(range(0,50)), 20)
  23.  
  24. red = x[n]
  25.  
  26. blue = np.delete(x, n, 0)
  27.  
  28. plt.figure(figsize=(10,10))
  29. plt.scatter(blue[:,0], blue[:,1],marker='s')
  30. plt.scatter(red[:,0], red[:,1], marker='o',c = 'r')
  31.  
  32.  
  33. closer_p= 0
  34. closer_q = 0
  35. p = np.array([(5,10)], dtype = np.int64)
  36. q = np.array([(10,5)], dtype = np.int64)
  37.  
  38. for point in red:
  39.     d1 = np.linalg.norm(p-point)
  40.     d2 = np.linalg.norm(q-point)
  41.     if d1 < d2:
  42.         closer_p+=1
  43.     else:
  44.         closer_q+=1
  45.  
  46.  
  47. print(closer_p)
  48. print(closer_q)
  49. plt.show()
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement