Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. #generator danych liniowo separowalnych
  5. def gen_lin_sep(ile_danych, zakresy, wektor_kierunkowy):
  6. zak = np.array(zakresy)
  7. X = np.random.sample((ile_danych, len(wektor_kierunkowy)-1))
  8. X *= (zak[:,1]-zak[:,0])
  9. X += zak[:,0]
  10. Y = np.zeros(ile_danych)
  11. for idx, x in enumerate(X):
  12. suma = np.dot(np.append(x, 1), wektor_kierunkowy)
  13. if(suma>0):
  14. Y[idx] = 1
  15. else:
  16. Y[idx] = -1
  17. return X, Y
  18.  
  19. ##ustalenie atrybutów i wygenerowanie danych
  20. #ile = 100
  21. #zakres = [[-10,10], [-2,5]]
  22. #wek = [1,1,1]
  23. #dane, klasy = gen_lin_sep(ile, zakres, wek)
  24.  
  25. ##wyswietlenie granicy decyzyjnej i danych
  26. #os_x = np.linspace(-10,10,101)
  27. #os_y = os_x*(wek[0]/-wek[1]) + (wek[2]/-wek[1])
  28. #plt.plot(os_x, os_y)
  29. #plt.scatter(dane[:, 0], dane[:, 1], c=klasy)
  30.  
  31. #generator danych "koło w prostokącie"
  32. def gen_sphere(ile_danych, zakresy, srodek, promien):
  33. zak = np.array(zakresy)
  34. X = np.random.sample((ile_danych, len(zakresy)))
  35. X *= (zak[:,1]-zak[:,0])
  36. X += zak[:,0]
  37. Y = np.zeros(ile_danych)
  38. for idx, x in enumerate(X):
  39. odleglosc = np.sqrt(np.power(x-srodek, 2).sum())
  40. if(odleglosc < promien):
  41. Y[idx] = 1
  42. else:
  43. Y[idx] = -1
  44. return X, Y
  45.  
  46. ##ustalenie atrybutów i wygenerowanie danych
  47. #ile = 1000
  48. #zakres = [[10,20], [10,20]]
  49. #sr = [15, 15]
  50. #rad = 3
  51. #dane, klasy = gen_sphere(ile, zakres, sr, rad)
  52.  
  53. ##wyswietlenie granicy decyzyjnej i danych
  54. #os_x = np.linspace(sr[0]-rad, sr[0]+rad, 101)
  55. #os_y1 = np.power((rad**2 -np.power(os_x-sr[0], 2)), 0.5) + sr[1]
  56. #os_y2 = -np.power((rad**2 -np.power(os_x-sr[0], 2)), 0.5) + sr[1]
  57.  
  58. #plt.plot(os_x, os_y1, c='r')
  59. #plt.plot(os_x, os_y2, c='r')
  60. #plt.scatter(dane[:, 0], dane[:, 1], c=klasy)
  61.  
  62.  
  63. #generator danych "dwa koła w prostokącie"
  64. def gen_two_sphere(ile_danych, zakresy, srodek1, promien1, srodek2, promien2):
  65. zak = np.array(zakresy)
  66. X = np.random.sample((ile_danych, len(zakresy)))
  67. X *= (zak[:,1]-zak[:,0])
  68. X += zak[:,0]
  69. Y = np.zeros(ile_danych)
  70. for idx, x in enumerate(X):
  71. odleglosc1 = np.sqrt(np.power(x-srodek1, 2).sum())
  72. if(odleglosc1 < promien1):
  73. Y[idx] = 1
  74. else:
  75. odleglosc2 = np.sqrt(np.power(x-srodek2, 2).sum())
  76. if(odleglosc2 < promien2):
  77. Y[idx] = 2
  78. else:
  79. Y[idx] = 3
  80. return X, Y
  81.  
  82. ##ustalenie atrybutów i wygenerowanie danych
  83. #ile = 1000
  84. #zakres = [[10,20], [10,20]]
  85. #sr1 = [17, 17]
  86. #sr2 = [13, 13]
  87. #rad1 = 2
  88. #rad2 = 1
  89. #dane, klasy = gen_two_sphere(ile, zakres, sr1, rad1, sr2, rad2)
  90. #klasy
  91.  
  92. ##wyswietlenie granicy decyzyjnej i danych
  93. #os_x1 = np.linspace(sr1[0]-rad1, sr1[0]+rad1, 101)
  94. #os_y11 = np.power((rad1**2 -np.power(os_x1-sr1[0], 2)), 0.5) + sr1[1]
  95. #os_y12 = -np.power((rad1**2 -np.power(os_x1-sr1[0], 2)), 0.5) + sr1[1]
  96.  
  97. #os_x2 = np.linspace(sr2[0]-rad2, sr2[0]+rad2, 101)
  98. #os_y21 = np.power((rad2**2 -np.power(os_x2-sr2[0], 2)), 0.5) + sr2[1]
  99. #os_y22 = -np.power((rad2**2 -np.power(os_x2-sr2[0], 2)), 0.5) + sr2[1]
  100.  
  101. #plt.plot(os_x1, os_y11, c='r')
  102. #plt.plot(os_x1, os_y12, c='r')
  103. #plt.plot(os_x2, os_y21, c='b')
  104. #plt.plot(os_x2, os_y22, c='b')
  105. #plt.scatter(dane[:, 0], dane[:, 1], c=klasy)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement