Advertisement
ddeexxiikk

SI-LAB3-StudentTask:1

Mar 20th, 2025
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | Source Code | 0 0
  1. # Trenowanie klasyfikatora One vs Rest dla wszystkich trzech klas
  2. OvRClassifier = OneVsRestClassifier(LogisticRegression())
  3. OvRClassifier.fit(iris_train_scaled_data, iris_train_target)
  4.  
  5. # Tworzenie wykresu z danymi i granicami decyzyjnymi dla trzech klasyfikatorów
  6. plt.figure(figsize=(10, 8))
  7.  
  8. # Tworzenie siatki do wizualizacji granic decyzyjnych
  9. x_min, x_max = iris_train_scaled_data[:, 0].min() - 1, iris_train_scaled_data[:, 0].max() + 1
  10. y_min, y_max = iris_train_scaled_data[:, 1].min() - 1, iris_train_scaled_data[:, 1].max() + 1
  11. xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02),
  12.                      np.arange(y_min, y_max, 0.02))
  13.  
  14. # Pobieranie unikalnych klas
  15. unique_classes = np.unique(iris_train_target)
  16. class_colors = ['blue', 'orange', 'green']
  17.  
  18. # Wyświetlanie punktów danych
  19. for i, class_idx in enumerate(unique_classes):
  20.     plt.scatter(iris_train_scaled_data[iris_train_target == class_idx][:, 0],
  21.                 iris_train_scaled_data[iris_train_target == class_idx][:, 1],
  22.                 color=class_colors[i],
  23.                 alpha=0.7,
  24.                 label=f'{class_idx} - {iris.target_names[class_idx]}')
  25.  
  26. # Wykreślanie granic decyzyjnych dla każdego klasyfikatora binarnego w OvR
  27. for i, (class_idx, estimator) in enumerate(zip(unique_classes, OvRClassifier.estimators_)):
  28.     # Pobieranie współczynników i wyrazu wolnego dla tego klasyfikatora
  29.     coef = estimator.coef_[0]
  30.     intercept = estimator.intercept_[0]
  31.    
  32.     # Obliczanie granicy decyzyjnej
  33.     x1_vals = np.linspace(x_min, x_max, 100)
  34.     x2_vals = -(coef[0] / coef[1]) * x1_vals - (intercept / coef[1])
  35.    
  36.     plt.plot(x1_vals, x2_vals,
  37.              linestyle='--',
  38.              color=class_colors[i],
  39.              linewidth=2,
  40.              label=f'Granica dla klasy {class_idx}')
  41.  
  42. plt.title('Klasyfikatory One-vs-Rest dla zbioru Iris')
  43. plt.xlabel('Przeskalowana długość działki kielicha')
  44. plt.ylabel('Przeskalowana szerokość działki kielicha')
  45. plt.legend()
  46. plt.grid(True)
  47. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement