def test_plot_density(self): N = 200 np.random.seed(1) X = np.concatenate((np.random.normal(0, 1, int(0.3 * N)), np.random.normal(5, 1, int(0.7 * N))))[:, np.newaxis] X_plot = CArray(np.linspace(-5, 10, 1000)[:, np.newaxis]) true_dens = CArray(0.3 * norm(0, 1).pdf(X_plot[:, 0].tondarray()) + 0.7 * norm(5, 1).pdf(X_plot[:, 0].tondarray())) fig = CFigure(width=7) fig.sp._sp.fill(X_plot[:, 0].tondarray(), true_dens.tondarray(), fc='black', alpha=0.2, label='input distribution') for kernel in ['gaussian', 'tophat', 'epanechnikov']: kde = CDensityEstimation(kernel=kernel, bandwidth=0.5) x, y = kde.estimate_density(CArray(X), n_points=N) fig.sp.plot(x, y, '-', label="kernel = '{0}'".format(kernel)) fig.sp.text(6, 0.38, "N={0} points".format(N)) fig.sp.legend(loc='upper left') fig.sp.plot(X[:, 0], -0.005 - 0.01 * np.random.random(X.shape[0]), '+k') fig.sp.xlim(-4, 9) fig.sp.ylim(-0.02, 0.4) fig.show()
def _save_fig(self): """Visualizing the function being optimized with line search.""" x_range = CArray.arange(-5, 20, 0.5, ) score_range = x_range.T.apply_along_axis(self.fun.fun, axis=1) ref_line = CArray.zeros(x_range.size) fig = CFigure(height=6, width=12) fig.sp.plot(x_range, score_range, color='b') fig.sp.plot(x_range, ref_line, color='k') filename = fm.join(fm.abspath(__file__), 'test_line_search_bisect.pdf') fig.savefig(filename)
def test_standard(self): """Plot of standard ROC.""" # Testing without input CFigure roc_plot = CFigure() roc_plot.sp.title('ROC Curve Standard') # Plotting 2 times (to show multiple curves) # add one curve for repetition and call it rep 0 and rep 1 of roc 1 roc_plot.sp.plot_roc(self.roc_wmean.mean_fpr, self.roc_wmean.mean_tpr) roc_plot.show()
def test_single(self): """Plot of ROC repetitions.""" # Testing without input CFigure roc_plot = CFigure() roc_plot.sp.title('ROC Curve Repetitions') # Plotting 2 times (to show multiple curves) # add one curve for repetition and call it rep 0 and rep 1 of roc 1 roc_plot.sp.plot_roc_reps(self.roc_nomean, label='roc1') # add one curve for repetition and call it rep 0 and rep 1 of roc 2 roc_plot.sp.plot_roc_reps(self.roc_nomean, label='roc2') roc_plot.show()
def plot_loss_after_attack(evasAttack): """ This function plots the evolution of the loss function of the surrogate classifier after an attack is performed. The loss function is normalized between 0 and 1. It helps to know whether parameters given to the attack algorithm are well tuned are not; the loss should be as minimal as possible. The script is inspired from https://secml.gitlab.io/tutorials/11-ImageNet_advanced.html#Visualize-and-check-the-attack-optimization """ n_iter = evasAttack.x_seq.shape[0] itrs = CArray.arange(n_iter) # create a plot that shows the loss during the attack iterations # note that the loss is not available for all attacks fig = CFigure(width=10, height=4, fontsize=14) # apply a linear scaling to have the loss in [0,1] loss = evasAttack.f_seq if loss is not None: loss = CNormalizerMinMax().fit_transform(CArray(loss).T).ravel() fig.subplot(1, 2, 1) fig.sp.xlabel('iteration') fig.sp.ylabel('loss') fig.sp.plot(itrs, loss, c='black') fig.tight_layout() fig.show()
def test_custom_params(self): """Plot of ROC altering default parameters.""" # Testing without input CFigure roc_plot = CFigure() roc_plot.sp.title('ROC Curve - Custom') roc_plot.sp.xlim(0.1, 100) roc_plot.sp.ylim(30, 100) roc_plot.sp.yticks([70, 80, 90, 100]) roc_plot.sp.yticklabels(['70', '80', '90', '100']) # Plotting 2 times (to show 2 curves) roc_plot.sp.plot_roc_mean(self.roc_wmean, label='roc1') roc_plot.sp.plot_roc_mean(self.roc_wmean, label='roc2') roc_plot.show()
def test_mean(self): """Plot of average ROC.""" # Testing without input CFigure roc_plot = CFigure() roc_plot.sp.title('ROC Curve') # Plotting 2 times (to show 2 curves) roc_plot.sp.plot_roc_mean(self.roc_wmean, label='roc1 mean', plot_std=True) roc_plot.sp.plot_roc_reps(self.roc_wmean, label='roc1') roc_plot.show() # Testing mean plot with no average with self.assertRaises(ValueError): roc_plot.sp.plot_roc_mean(self.roc_nomean)
def test_margin(self): self.logger.info("Testing margin separation of SGD...") # we create 50 separable points dataset = CDLRandomBlobs(n_samples=50, centers=2, random_state=0, cluster_std=0.60).load() # fit the model clf = CClassifierSGD(loss=CLossHinge(), regularizer=CRegularizerL2(), alpha=0.01, max_iter=200, random_state=0) clf.fit(dataset.X, dataset.Y) # plot the line, the points, and the nearest vectors to the plane xx = CArray.linspace(-1, 5, 10) yy = CArray.linspace(-1, 5, 10) X1, X2 = np.meshgrid(xx.tondarray(), yy.tondarray()) Z = CArray.empty(X1.shape) for (i, j), val in np.ndenumerate(X1): x1 = val x2 = X2[i, j] Z[i, j] = clf.decision_function(CArray([x1, x2]), y=1) levels = [-1.0, 0.0, 1.0] linestyles = ['dashed', 'solid', 'dashed'] colors = 'k' fig = CFigure(linewidth=1) fig.sp.contour(X1, X2, Z, levels, colors=colors, linestyles=linestyles) fig.sp.scatter(dataset.X[:, 0].ravel(), dataset.X[:, 1].ravel(), c=dataset.Y, s=40) fig.savefig( fm.join(fm.abspath(__file__), 'figs', 'test_c_classifier_sgd2.pdf'))