mpl.rcParams['ytick.labelsize'] = ticksize import matplotlib.cm as cm # function to plot numSVs = 40 basename = 'rc37u-pt14' vecext = 'vec' identext = 'ids' IDthresh = 0.6 varalpha=False indat = np.genfromtxt('%s.%s' %(basename,identext),names=True,dtype=None) parnames = indat['parameter'] NPAR = len(parnames) colors = cm.rainbow_r(np.linspace(0, 1, numSVs)) cumulID = np.zeros((NPAR,numSVs)) cumulID[:,0] = indat['eig1'] IDs = cumulID.copy() for ccol in np.arange(1,numSVs): cumulID[:,ccol] = cumulID[:,ccol-1] + indat['eig%s' %(ccol+1)] IDs[:,ccol] = indat['eig%s' %(ccol+1)] rowINDS = np.nonzero(indat['identifiability'] > IDthresh)[0] # abbreviate to only value over a threshold cumulIDs2plot = cumulID[rowINDS,:] IDs2plot = IDs[rowINDS,:] PAR2plot = parnames[rowINDS]
def plot_reinforcement_snapshots(N, M, samples, nu): file = 'states/reinforcement_N' + str(N) + '_M' + str( M) + '_samples' + str(samples) + '_nu' + str(nu) + '.txt' statefile = open(file, 'r') snapshots = statefile.readlines() statefile.close() WaveFunction = FeedForwardNeuralNetwork(N, M) Hamiltonian = CalogeroSutherland(WaveFunction, nu, 0.0) Sampler = ImportanceSampling(Hamiltonian, 0.001) plt.figure(figsize=(12, 8)) sns.set_style("whitegrid") colors = cm.rainbow_r(np.linspace(0, 1, len(snapshots))) num_samples = 300000 num_skip = 5000 index = [0, 10, 19, 29, 39] for snapshot, color in zip([snapshots[i] for i in index], [colors[i] for i in index]): WaveFunction.alpha = np.array(snapshot.split()[1:]).astype(np.float) WaveFunction.separate() WaveFunction.x = np.random.normal(0, 1 / np.sqrt(2), N) for sample in range(num_skip): accepted = Sampler.sample() x = np.empty(N * num_samples) for sample in range(num_samples): accepted = Sampler.sample() x[N * sample:N * (sample + 1)] = WaveFunction.x sns.kdeplot(x, shade=True, linewidth=2, color=color, label=str(snapshot.split()[0]) + ' updates') num_samples = 10000000 WaveFunction.alpha = np.array(snapshots[-1].split()[1:]).astype(np.float) WaveFunction.separate() WaveFunction.x = np.random.normal(0, 1 / np.sqrt(2), N) for sample in range(num_skip): accepted = Sampler.sample() x = np.empty(N * num_samples) for sample in range(num_samples): accepted = Sampler.sample() x[N * sample:N * (sample + 1)] = WaveFunction.x sns.kdeplot(x, shade=True, linewidth=2, color=colors[-1], label=str(snapshots[-1].split()[0]) + ' updates') x = np.empty(N * num_samples) WaveFunction.x = np.random.normal(0, 1 / np.sqrt(2), N) for sample in range(num_skip): accepted = Sampler.exact_sample() for sample in range(num_samples): accepted = Sampler.exact_sample() x[N * sample:N * (sample + 1)] = WaveFunction.x sns.kdeplot(x, shade=False, linewidth=3, color='k', linestyle='dashed', label=r'$|\Psi_0^{exact}(x)|^2$') plt.ylim(-0.1, 0.7) plt.xlim(-6, 6) plt.ylabel(r'Probability Distribution $|\Psi(x)|^2$', fontsize=20) plt.xlabel(r'Positions $x$', fontsize=20) plt.title('Reinforcement Learning of the Wave Function for $N$ = ' + str(N) + r'$, \ \nu$ = ' + str(nu), fontsize=24) plt.legend(loc='upper right', fontsize=20) plt.savefig('figures/N' + str(N) + '_M' + str(M) + '_reinforcement_snapshots.pdf', format='pdf')
def plot_supervised_snapshots(N, M): if N == 1: file = 'states/supervised_N1_M' + str(M) + '.txt' statefile = open(file, 'r') snapshots = statefile.readlines() statefile.close() WaveFunction = FeedForwardNeuralNetwork(1, M) Hamiltonian = CalogeroSutherland(WaveFunction, 0.0, 0.0) fig, ax = plt.subplots(figsize=(12, 8)) colors = cm.rainbow_r(np.linspace(0, 1, len(snapshots))) num_points = 200 x = np.linspace(-5.0, 5.0, num_points) for snapshot, color in zip(snapshots, colors): WaveFunction.alpha = np.array(snapshot.split()[1:]).astype( np.float) WaveFunction.separate() psi = np.zeros(num_points) for i in range(num_points): psi[i] = WaveFunction.calc_psi([x[i]]) plt.plot(x, psi, color=color, linewidth=3, label=str(snapshot.split()[0]) + ' updates') psi_nonint = np.zeros(num_points) for i in range(num_points): psi_nonint[i] = Hamiltonian.nonint_gs_wavefunction([x[i]]) plt.plot(x, psi_nonint, color='k', linewidth=2, linestyle='dashed', label=r'$\Psi_0^{non\text{-}int}(x)$') plt.ylim(-0.1, 1.5) plt.xlim(-5, 5) plt.ylabel(r'Ground state wave function $\Psi(x)$', fontsize=20) plt.xlabel(r'Position $x$', fontsize=20) plt.title( r'Supervised learning of the wave function for the non-interacting case', fontsize=24) ax.tick_params(axis='both', which='major', labelsize=16) plt.legend(loc='upper right', fontsize=20) plt.savefig('figures/N1_M' + str(M) + '_supervised_snapshots.pdf', format='pdf') else: file = 'states/supervised_N' + str(N) + '_M' + str(M) + '.txt' statefile = open(file, 'r') snapshots = statefile.readlines() statefile.close() WaveFunction = FeedForwardNeuralNetwork(N, M) Hamiltonian = CalogeroSutherland(WaveFunction, 0.0, 0.0) Sampler = ImportanceSampling(Hamiltonian, 0.005) fig, ax = plt.subplots(figsize=(12, 8)) sns.set_style("whitegrid") colors = cm.rainbow_r(np.linspace(0, 1, len(snapshots))) num_samples = 100000 num_skip = 1 #for snapshot, color in zip(snapshots, colors): index = [0, 2, 4, 5, 7, 9] for snapshot, color in zip([snapshots[i] for i in index], [colors[i] for i in index]): WaveFunction.alpha = np.array(snapshot.split()[1:]).astype( np.float) WaveFunction.separate() WaveFunction.x = np.random.normal(0, 1 / np.sqrt(2), N) for sample in range(num_skip): accepted = Sampler.sample() x = np.empty(N * num_samples) for sample in range(num_samples): accepted = Sampler.sample() x[N * sample:N * (sample + 1)] = WaveFunction.x sns.kdeplot(x, shade=True, linewidth=2, color=color, label=str(snapshot.split()[0]) + ' updates') num_samples = 1000000 WaveFunction.alpha = np.array(snapshots[-1].split()[1:]).astype( np.float) WaveFunction.separate() WaveFunction.x = np.random.normal(0, 1 / np.sqrt(2), N) for sample in range(num_skip): accepted = Sampler.sample() x = np.empty(N * num_samples) for sample in range(num_samples): accepted = Sampler.sample() x[N * sample:N * (sample + 1)] = WaveFunction.x sns.kdeplot(x, shade=True, linewidth=2, color=colors[-1], label=str(snapshots[-1].split()[0]) + ' updates') num_samples = 10000000 x = np.random.normal(0, 1 / np.sqrt(2), N * num_samples) sns.kdeplot(x, shade=False, linewidth=3, color='k', linestyle='dashed', label=r'$|\Psi_0^{non\text{-}int}(x)|^2$') plt.ylim(-0.1, 0.7) plt.xlim(-4, 4) plt.ylabel(r'Probability Distribution $|\Psi(x)|^2$', fontsize=20) plt.xlabel(r'Positions $x$', fontsize=20) plt.title( 'Supervised Learning of the Wave Function for the Non-Interacting Case', fontsize=24) ax.tick_params(axis='both', which='major', labelsize=16) plt.legend(loc='upper right', fontsize=20) plt.savefig('figures/N' + str(N) + '_M' + str(M) + '_supervised_snapshots.pdf', format='pdf')
def main(): """ main routine """ # create some abscissa values x = np.linspace(0, 10, 200) # compute the pdfs of the chi-squared dist. for a few degrees of freedom ks = [1, 2, 3, 4, 5, 7, 10, 15] # degrees of freedom to investigate pdfs = [chi_squared_pdf(x, k) for k in ks] # do the same thing using the the scipy function pdfs_scipy = [chi2.pdf(x, k) for k in ks] # compute the cdfs of the chi-squared dist. for a few degrees of freedom cdfs = [chi_squared_cdf(x, k) for k in ks] # do the same thing using the the scipy function cdfs_scipy = [chi2.cdf(x, k) for k in ks] # find the right-hand-side constant for an F_desired-percentage confidence # interval for an uncorrelated 2D dataset k = 2 # dimensions of the ellipsoid confidence_prob = 0.95 # desired confidence interval ellipse_rhs = fsolve(cdf_root_function, 0.0, args=(k, confidence_prob))[0] # print the ellipsoid constant found print('\n\t' + 60 * '-') print('\n\tRHS constant computation: %d-dimensional ellipsoid' % k) print('\n\t desired confidence:\t\t', confidence_prob) print('\n\t RHS ellipsoid constant:\t', ellipse_rhs) print('\n\t' + 60 * '-') # plot the distribution plot_name = 'chi-squared pdfs (mine)' auto_open = True the_fontsize = 16 plt.figure(plot_name) # make a list of colors colors = cm.rainbow_r(np.linspace(0, 1, len(ks))) # plotting for i in range(len(ks)): plt.plot(x, pdfs[i], color=colors[i], label='$k=' + str(ks[i]) + '$') plt.xlabel('$x$', fontsize=the_fontsize) plt.ylabel('$p(x)$', fontsize=the_fontsize) plt.title('$\chi^2 \! -\! distributions \quad (subroutine)$') plt.legend(loc='best') plt.ylim(0, 0.5) # save plot and close print('\n\t' + 'saving final image...', end='') file_name = plot_name + '.png' plt.savefig(file_name, dpi=300) print('figure saved: ' + plot_name) plt.close(plot_name) # open the saved image, if desired if auto_open: webbrowser.open(file_name) # plot the distribution plot_name = 'chi-squared pdfs (scipy)' auto_open = True the_fontsize = 16 plt.figure(plot_name) # make a list of colors colors = cm.rainbow_r(np.linspace(0, 1, len(ks))) # plotting for i in range(len(ks)): plt.plot(x, pdfs_scipy[i], color=colors[i], label='$k=' + str(ks[i]) + '$') plt.xlabel('$x$', fontsize=the_fontsize) plt.ylabel('$p(x)$', fontsize=the_fontsize) plt.title('$\chi^2 \! -\! distributions \quad (\\mathtt{scipy.stats})$') plt.legend(loc='best') plt.ylim(0, 0.5) # save plot and close print('\n\t' + 'saving final image...', end='') file_name = plot_name + '.png' plt.savefig(file_name, dpi=300) print('figure saved: ' + plot_name) plt.close(plot_name) # open the saved image, if desired if auto_open: webbrowser.open(file_name) # plot the distribution plot_name = 'chi-squared cdfs (mine)' auto_open = True the_fontsize = 16 plt.figure(plot_name) # make a list of colors colors = cm.rainbow_r(np.linspace(0, 1, len(ks))) # plotting for i in range(len(ks)): plt.plot(x, cdfs[i], color=colors[i], label='$k=' + str(ks[i]) + '$') # plot the point that was found using fsolve plt.plot(ellipse_rhs, confidence_prob, 'k.') plt.text(ellipse_rhs, confidence_prob, '$' + str(100 * confidence_prob) + '\%$') plt.xlabel('$x$', fontsize=the_fontsize) plt.ylabel('$F(x)$', fontsize=the_fontsize) plt.title('$\chi^2 \; CDFs \quad (subroutine)$') plt.legend(loc='best') # save plot and close print('\n\t' + 'saving final image...', end='') file_name = plot_name + '.png' plt.savefig(file_name, dpi=300) print('figure saved: ' + plot_name) plt.close(plot_name) # open the saved image, if desired if auto_open: webbrowser.open(file_name) # plot the distribution plot_name = 'chi-squared cdfs (scipy)' auto_open = True the_fontsize = 16 plt.figure(plot_name) # make a list of colors colors = cm.rainbow_r(np.linspace(0, 1, len(ks))) # plotting for i in range(len(ks)): plt.plot(x, cdfs_scipy[i], color=colors[i], label='$k=' + str(ks[i]) + '$') # plot the point that was found using fsolve plt.plot(ellipse_rhs, confidence_prob, 'k.') plt.text(ellipse_rhs, confidence_prob, '$' + str(100 * confidence_prob) + '\%$') plt.xlabel('$x$', fontsize=the_fontsize) plt.ylabel('$F(x)$', fontsize=the_fontsize) plt.title('$\chi^2 \; CDFs \quad (\\mathtt{scipy.stats})$') plt.legend(loc='best') # save plot and close print('\n\t' + 'saving final image...', end='') file_name = plot_name + '.png' plt.savefig(file_name, dpi=300) print('figure saved: ' + plot_name) plt.close(plot_name) # open the saved image, if desired if auto_open: webbrowser.open(file_name)
axthresh.imshow(binimg, interpolation='nearest', cmap='Greys') # Display color-coded clusters axclust = figclust.add_subplot(2, 3, ii + 1) axclust.set_xticks([]) axclust.set_yticks([]) axcltwo = figcltwo.add_subplot(2, 3, ii + 1) axcltwo.set_xticks([]) axcltwo.set_yticks([]) axcltwo.imshow(binimg, interpolation='nearest', cmap='Greys') clustimg = np.ones(rgbimg.shape) unique_labels = set(labels) # 为每个聚类生成单个颜色 plcol = cm.rainbow_r(np.linspace(0, 1, len(unique_labels))) print('plcol', plcol) for lbl, pix in zip(labels, Xslice): for col, unqlbl in zip(plcol, unique_labels): if lbl == unqlbl: # -1 表示无聚类成员 if lbl == -1: col = [0.0, 0.0, 0.0, 1.0] for ij in range(3): clustimg[pix[0], pix[1], ij] = col[ij] # 扩张 图像,用于更好展示 axcltwo.plot(pix[1], pix[0], 'o', markerfacecolor=col, markersize=1,
237: 'Avril', 33: 'Bob', 50: 'Bill', 31: 'John', 100: 'Dick', 241: 'Ed' } train_data_10users['user_id'] = train_data_10users['user_id'].map(id_name_dict) # In[17]: import matplotlib.cm as cm user_list = train_data_10users.user_id.unique() color_dict = dict((user, color) for user, color in zip( user_list, cm.rainbow_r(np.linspace(0, 1, len(user_list))))) # In[18]: train_data_10users.head() # **1. Постройте гистограмму распределения длины сессии в секундах (*session_timespan*). Ограничьте по *x* значением 200 (иначе слишком тяжелый хвост). Сделайте гистограмму цвета *darkviolet*, подпишите оси по-русски.** # In[19]: hist_data = train_data_10users['session_timespan'] plt.hist(hist_data, bins=50, range=(0, 200), color='darkviolet', zorder=2) plt.grid(True, alpha=.2, zorder=0) plt.xlabel('Длительность сессии, с') plt.ylabel('Количество сессий') plt.title('Распределение длительности сессий')
# Data for three-dimensional scattered points #zdata = np.array([data[i][2] for i in range(1000)]) #xdata = np.array([data[i][0] for i in range(1000)]) #ydata = np.array([data[i][1] for i in range(1000)]) #xdata = np.sin(zdata) + 0.1 * np.random.randn(100) #ydata = np.cos(zdata) + 0.1 * np.random.randn(100) format = "%Y\t%m\t%d %H" xdata = [data[dates][0] for dates in range(len(data))] ydata = [data[dates][1] for dates in range(len(data))] xdata = np.array([]) ydata = np.array([]) colors = cm.rainbow_r(np.arange(1, 10000, 1)) currentmonth = 1 for dates in MJO_dict: currentdate = datetime.strptime(dates, format) if (currentdate.month == currentmonth): xdata = np.append(arr=xdata, values=MJO_dict[dates][0]) ydata = np.append(arr=ydata, values=MJO_dict[dates][1]) else: ax.scatter(xdata, ydata, s=10, color=colors[currentmonth * 20]) plt.xlim(-4, 4) plt.ylim(-4, 4) plt.xticks(ticks=np.arange(-4, 4, .5)) plt.yticks(ticks=np.arange(-4, 4, .5)) plt.plot(xdata, ydata, color=colors[currentmonth * 20]) plt.text(xdata[0], ydata[0], "{}".format(currentdate.month)) currentmonth = currentdate.month
def plot_actions_selected(actions_selected, actions_list, auto_open=True, plots_directory='.'): """ given a list of the action selected at each timestep during a run, this function plots a time history of the selection process """ # print a header message to the screen print('\n\t- plotting the time history of selected actions') # count the number of timesteps n_timesteps = len(actions_selected) # make a list of the timesteps (the abscissas) timesteps = np.arange(1, n_timesteps + 1) # create a list of action numbers from the list action names action_numbers_selected = [ int(action_name.split('#')[-1]) for action_name in actions_selected ] # make a list of colors, one for each action n_actions = len(actions_list) colors = cm.rainbow_r(np.linspace(0, 1, n_actions)) # plotting preliminaries plot_name = 'selected actions' the_fontsize = 14 fig = plt.figure(plot_name) # stretch the plotting window width, height = fig.get_size_inches() fig.set_size_inches(1.75 * width, 1.0 * height, forward=True) # plotting for i in range(n_timesteps): action_number = action_numbers_selected[i] action_number_index = action_number - 1 plt.plot(timesteps[i], action_number, marker='.', ms=1, color=colors[action_number_index]) # set the y-axis ticks action_indices = list(range(1, n_actions + 1)) y_tick_labels = ['$a_{' + str(index) + '}$' for index in action_indices] plt.yticks(action_indices, y_tick_labels) # label the axes plt.xlabel('$t$', fontsize=the_fontsize) plt.ylabel('$A_t$', fontsize=the_fontsize) plt.title('$selected \; actions$', fontsize=the_fontsize) # create the plots directory, if it doesn't already exist path_to_plots = create_directory(plots_directory) # write the file name for the plot and the corresponding full path file_name = plot_name + '.png' path_to_file = path_to_plots.joinpath(file_name) path_to_cwd = os.getcwd() relative_file_path = str(path_to_file).replace(path_to_cwd, '') relative_file_path = relative_file_path.lstrip('\\').lstrip('/') # save and close the figure print('\n\t\t' + 'saving figure ... ', end='') plt.savefig(path_to_file, dpi=300) print('done.\n\t\tfigure saved: ' + relative_file_path) plt.close(plot_name) # open the saved image, if desired if auto_open: webbrowser.open(path_to_file)
def plot_reward_distributions(action_values, reward_stds, auto_open=True, plots_directory='.'): """ given the true action values associated with each action and the desired standard deviations to be used with each of the reward distributions, this function will create a plot showing all the reward pdfs associated with each action """ # print a header message to the screen print('\n\t- plotting the distributions from which rewards will be ' + 'drawn for each available action') # plotting preliminaries plot_name = 'reward distribution' the_fontsize = 14 plt.figure(plot_name) # create a long, fine mesh over which to to plot the distributions (this # mesh will be truncated later) q_star_min = min(action_values) - max(reward_stds) q_star_max = max(action_values) + max(reward_stds) q_star_span = q_star_max - q_star_min q_star_overall_min = q_star_min - 3 * q_star_span q_star_overall_max = q_star_max + 3 * q_star_span n_points_per_curve = 2000 x = np.linspace(q_star_overall_min, q_star_overall_max, n_points_per_curve) # make a list of colors, one for each action colors = cm.rainbow_r(np.linspace(0, 1, n_actions)) # get machine zero machine_eps = np.finfo(float).eps # plotting for i in range(n_actions): # for each available action, pull out the corresponding mean and # standard deviation for the reward distribution reward_mean = action_values[i] reward_std = reward_stds[i] # compute the pdf describing this action's reward distribution reward_dist = compute_gaussian_pdf(x, reward_mean, reward_std) # pull out the indices where the pdf is non negligible indices_to_keep = np.where(reward_dist > 1e8 * machine_eps) # pull out the abscissas and ordinates at these indices x_reward = x[indices_to_keep] prob_reward = reward_dist[indices_to_keep] # plot the distribution plt.plot(x_reward, prob_reward, color=colors[i], label='$A_t=a_{' + str(i + 1) + '}$') # write the expected reward for this action above the curve y_lims = plt.ylim() text_padding = (y_lims[1] - y_lims[0]) / 75 q_star_str = str(round(reward_mean, 2)) plt.text(reward_mean - 20 * text_padding, max(reward_dist) + text_padding, '$q_*(a_{' + str(i + 1) + '})=' + q_star_str + '$', fontsize=the_fontsize - 6) # label the x axis and write the title plt.xlabel('$R_t$', fontsize=the_fontsize) plt.title('$reward\; distributions\colon\; \\textrm{PDF}s\; f\! or\; ' + 'R_t \\vert A_t$', fontsize=the_fontsize) plt.legend(loc='best') # create the plots directory, if it doesn't already exist path_to_plots = create_directory(plots_directory) # write the file name for the plot and the corresponding full path file_name = plot_name + '.png' path_to_file = path_to_plots.joinpath(file_name) path_to_cwd = os.getcwd() relative_file_path = str(path_to_file).replace(path_to_cwd, '') relative_file_path = relative_file_path.lstrip('\\').lstrip('/') # save and close the figure print('\n\t\t' + 'saving figure ... ', end='') plt.savefig(path_to_file, dpi=300) print('done.\n\t\tfigure saved: ' + relative_file_path) plt.close(plot_name) # open the saved image, if desired if auto_open: webbrowser.open(path_to_file)
import matplotlib.cm as cm # function to plot numSVs = 60 basename = 'nacp12_ss' vecext = 'vec' identext = 'ids' parsperpage = 13 IDthresh = 0.5 varalpha=False indat = np.genfromtxt('%s.%s' %(basename,identext),names=True,dtype=None) parnames = indat['parameter'] NPAR = len(parnames) indat['identifiability'] colors = cm.rainbow_r(np.linspace(0, 1, numSVs)) cumulID = np.zeros((NPAR,numSVs)) cumulID[:,0] = indat['eig1'] IDs = cumulID.copy() for ccol in np.arange(1,numSVs): cumulID[:,ccol] = cumulID[:,ccol-1] + indat['eig%s' %(ccol+1)] IDs[:,ccol] = indat['eig%s' %(ccol+1)] allrowINDS = np.nonzero(indat['identifiability'] >= IDthresh)[0] # set up a PDF for output fig_pdf = PdfPages('Identifiability.pdf')