def plot_gram_matrix(x_train, gamma, n_reduce=None): kmatrix = gram_matrix(x_train, gamma) if n_reduce is not None: kmatrix = kmatrix[:n_reduce, :n_reduce] plt.pcolor(kmatrix, cmap=plt.cm.PuRd) plt.colorbar() plt.xlim([0, kmatrix.shape[0]]) plt.ylim([0, kmatrix.shape[0]]) plt.gca().invert_yaxis() plt.gca().xaxis.tick_top() plt.show()
def plot_regularization_path(alphas, coefs, features_labels, n_features_labels=None, legend_size='medium'): """ :param alphas: :param coefs: :param features_labels: :param n_features_labels: :param legend_size: :return: """ plt.figure(figsize=(12, 6)) ax = plt.gca() ax.plot(alphas, coefs) ax.set_xscale('log') plt.xlabel('Alpha') plt.ylabel('Coefficients') plt.legend(features_labels[:n_features_labels] if n_features_labels is not None else features_labels, loc='upper right', fontsize=legend_size) plt.show()
def plot_correlation_circle(self, n_plan=None, labels=None, label_rotation=0, lims=None, save_as_img=False, plot_size=(10, 8)): """ """ factorial_plan_nb = self.default_factorial_plan_nb if n_plan is None else n_plan # Build a list of tuples (example : [(0, 1), (2, 3), ... ]) axis_ranks = [(x, x + 1) for x in range(0, factorial_plan_nb, 2)] pcs = self.pca.components_ for d1, d2 in axis_ranks: if d2 < self.n_comp: fig, ax = plt.subplots(figsize=plot_size) # Fix factorial plan limits if lims is not None: xmin, xmax, ymin, ymax = lims elif pcs.shape[1] < 30: xmin, xmax, ymin, ymax = -1, 1, -1, 1 else: xmin, xmax, ymin, ymax = min(pcs[d1, :]), max( pcs[d1, :]), min(pcs[d2, :]), max(pcs[d2, :]) # affichage des flèches # s'il y a plus de 30 flèches, on n'affiche pas le triangle à leur extrémité if pcs.shape[1] < 30: plt.quiver(np.zeros(pcs.shape[1]), np.zeros(pcs.shape[1]), pcs[d1, :], pcs[d2, :], angles='xy', scale_units='xy', scale=1, color="grey") # (doc : https://matplotlib.org/api/_as_gen/matplotlib.pyplot.quiver.html) else: lines = [[[0, 0], [x, y]] for x, y in pcs[[d1, d2]].T] ax.add_collection( LineCollection(lines, axes=ax, alpha=.1, color='black')) # Display variables labels if labels is not None: for i, (x, y) in enumerate(pcs[[d1, d2]].T): if xmin <= x <= xmax and ymin <= y <= ymax: plt.text(x, y, labels[i], fontsize='14', ha='center', va='center', rotation=label_rotation, color="blue", alpha=0.5) # fontsize : 14 # Plot circle circle = plt.Circle((0, 0), 1, facecolor='none', edgecolor='b') plt.gca().add_artist(circle) # définition des limites du graphique plt.xlim(xmin, xmax) plt.ylim(ymin, ymax) # affichage des lignes horizontales et verticales plt.plot([-1, 1], [0, 0], color='grey', ls='--') plt.plot([0, 0], [-1, 1], color='grey', ls='--') # Axes labels with % explained variance plt.xlabel('F{} ({}%)'.format(d1 + 1, round(100 * self.evr[d1], 1)), labelpad=20) plt.ylabel('F{} ({}%)'.format(d2 + 1, round(100 * self.evr[d2], 1)), labelpad=20) plt.title("Cercle des corrélations (F{} et F{})".format( d1 + 1, d2 + 1), pad=20) if save_as_img: plt.tight_layout() plt.savefig( 'corr_circle_{}.jpg'.format(1 if d1 == 0 else d1)) plt.show(block=False)