daily pastebin goal
58%
SHARE
TWEET

Untitled

a guest Sep 26th, 2018 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from matplotlib.colors import ListedColormap
  2. import matplotlib.pyplot as plt
  3. import warnings
  4.  
  5.  
  6. def versiontuple(v):
  7.     return tuple(map(int, (v.split("."))))
  8.  
  9.  
  10. def plot_decision_regions(X, y, classifier, test_idx=None, resolution=0.02):
  11.  
  12.     # konfiguruje generator znaczników i mapę kolorów
  13.     markers = ('s', 'x', 'o', '^', 'v')
  14.     colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
  15.     cmap = ListedColormap(colors[:len(np.unique(y))])
  16.  
  17.     # rysuje wykres powierzchni decyzyjnej
  18.     x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
  19.     x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
  20.     xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
  21.                            np.arange(x2_min, x2_max, resolution))
  22.     Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
  23.     Z = Z.reshape(xx1.shape)
  24.     plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap)
  25.     plt.xlim(xx1.min(), xx1.max())
  26.     plt.ylim(xx2.min(), xx2.max())
  27.  
  28.     # rysuje wykres wszystkich próbek
  29.     for idx, cl in enumerate(np.unique(y)):
  30.         plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1],
  31.                     alpha=0.8, c=cmap(idx),
  32.                     marker=markers[idx], label=cl)
  33.  
  34.     # zaznacza próbki testowe
  35.     if test_idx:
  36.         if not versiontuple(np.__version__) >= versiontuple('1.9.0'):
  37.             X_test, y_test = X[list(test_idx), :], y[list(test_idx)]
  38.             warnings.warn('Zaktualizuj bibliotekę NumPy do wersji 1.9.0 lub nowszej')
  39.         else:
  40.             X_test, y_test = X[test_idx, :], y[test_idx]
  41.  
  42.         plt.scatter(X_test[:, 0],
  43.                     X_test[:, 1],
  44.                     c='',
  45.                     alpha=1.0,
  46.                     linewidths=1,
  47.                     marker='o',
  48.                     edgecolors='k',
  49.                     s=80, label='Zestaw testowy')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top