Author: Not specified Language: python
Description: (v6) Timestamp: 2018-05-21 03:12:41 +0000
View raw paste Parent paste by: Not specified - Child paste by: Not specified Reply
  1. def plot_decision_boundary(b,alphas,ax, resolution=100, colors=('b', 'k', 'r')):
  2.         """Plots the model's decision boundary on the input axes object.
  3.        Range of decision boundary grid is determined by the training data.
  4.        Returns decision boundary grid and axes object (`grid`, `ax`)."""
  5.        
  6.         # Generate coordinate grid of shape [resolution x resolution]
  7.         # and evaluate the model over the entire space
  8.         xrange = np.linspace(data_set[:,0].min(), data_set[:,0].max(), resolution)
  9.         yrange = np.linspace(data_set[:,1].min(), data_set[:,1].max(), resolution)
  10.         grid = [[plot_decision_function(alphas, data_set,b,
  11.                                    np.array([xr, yr])) for yr in yrange] for xr in xrange]
  12.         print type(grid)
  13.         grid = np.array(grid).reshape(len(xrange), len(yrange))
  14.        
  15.         # Plot decision contours using grid and
  16.         # make a scatter plot of training data
  17.         ax.contour(xrange, yrange, grid, (-1, 0, 1), linewidths=(1, 1, 1),
  18.                    linestyles=('--', '-', '--'), colors=colors)
  19.         ax.scatter(data_set[:,0], data_set[:,1],
  20.                    c=data_set[:,2], cmap=plt.cm.viridis, lw=0, alpha=0.5)
  21.        
  22.         # Plot support vectors (non-zero alphas)
  23.         # as circled points (linewidth > 0)
  24.         mask = alphas != 0.0
  25.         ax.scatter(data_set[:,0][mask], data_set[:,1][mask],
  26.                    c=data_set[:,2][mask], cmap=plt.cm.viridis)
  27.        
  28.         return grid, ax
View raw paste Parent paste by: Not specified - Child paste by: Not specified Reply