def getGraph(): for i, clf in enumerate((svm, rbf_svc, rbf_svc_tunning)): # Se grafican las fronteras plt.subplot(2, 2, i + 1) plt.subplots_adjust(wspace=0.4, hspace=0.4) Z = clf.predict(np.c_[x_matrizSetEntrenamientoVect, y_clases]) #Color en las gráficas Z = Z.reshape(x_matrizSetEntrenamientoVect.shape) plt.contourf(x_matrizSetEntrenamientoVect, y_clases, Z, cmap=plt.cm.Paired, alpha=0.8) #Puntos de entrenamiento plt.scatter(x_matrizSetEntrenamientoVect[:, 0], x_matrizSetEntrenamientoVect[:, 1], c=y_clases, cmap=plt.cm.Paired) plt.xlabel('Longitud Sepal') plt.ylabel('Peso Sepal') plt.xlim(x_matrizSetEntrenamientoVect.min(), x_matrizSetEntrenamientoVect.max()) plt.ylim(y_clases.min(), y_clases.max()) plt.xticks(()) plt.yticks(()) plt.title(titles[i]) plt.show()
def plot_metrics(name, data): # for metric in data.columns: for i in range(2, len(data.columns) - 1): plt.ylim([0, 1]) plt.plot(data[data.columns[i]], label=data.columns[i]) plt.savefig('./run/' + name + data.columns[i] + '.png') plt.clf()
def plot_median_profile(): # Plot median line profile plt.plot(bseg.dw, bseg.s, '.', label='Median Stellar Spectrum') plt.plot(bseg.dw, bseg.model, '-', label='Median Telluric Model') plt.title('Median Line Profile') plt.xlabel('Distance From Line Center (A)') yl = list(plt.ylim()) yl[1] = 1.05 plt.ylim(*yl)
def plot_median_profile(): # Plot median line profile plt.plot(bseg.dw,bseg.s,'.',label='Median Stellar Spectrum') plt.plot(bseg.dw,bseg.model,'-',label='Median Telluric Model') plt.title('Median Line Profile') plt.xlabel('Distance From Line Center (A)') yl = list(plt.ylim()) yl[1] = 1.05 plt.ylim(*yl)
def plot_spectrum(): # Plot telluric lines and fits plt.plot(spec['w'],spec['s'],label='Stellar Spectrum') plt.plot(spec['w'],mod,label='Telluric Model') plt.plot(spec['w'],spec['s']-mod+0.5,label='Residuals') plt.xlim(6275,6305) plt.ylim(0.0,1.05) plt.xlabel('Wavelength (A)') plt.ylabel('Intensity') plt.title('Telluric Lines')
def plot_spectrum(): # Plot telluric lines and fits plt.plot(spec['w'], spec['s'], label='Stellar Spectrum') plt.plot(spec['w'], mod, label='Telluric Model') plt.plot(spec['w'], spec['s'] - mod + 0.5, label='Residuals') plt.xlim(6275, 6305) plt.ylim(0.0, 1.05) plt.xlabel('Wavelength (A)') plt.ylabel('Intensity') plt.title('Telluric Lines')
def plot(data, fname): plt.clf() width = 10 data = np.array(data) bins = np.arange(0, max(data), step=width) sns.distplot(data, norm_hist=True, kde=True, bins=bins, label='No ones') plt.ylim((0, 0.001)) plt.xlabel('First passage time (ps)') plt.text( 1000, 0.0005, 'MFPT = {0:4.2f} +/- {1:4.2f} ps'.format(data.mean(), 2 * data.std())) plt.savefig(fname, transparanet=True)
def plot_piledspectra(): fig = plt.figure(figsize = (6,8)) plt.xlim(5000,9000) specindex = range(0,100,10) offset = np.arange(0,len(specindex)) * 0.5 ylim = [0.5, offset[-1] + 1.3] plt.ylim(ylim[0], ylim[1]) plt.rc('text', usetex=True) plt.rc('font', family='serif') plt.xlabel(r'Restrame Wavelength [ \AA\ ]') plt.ylabel(r'Flux') line_wave = [5175., 5892., 6562.8, 8498., 8542., 8662.] # ['Mgb', 'NaD', 'Halpha', 'CaT', 'CaT', 'CaT'] for line in line_wave: x = [line, line] y = [ylim[0], ylim[1]] plt.plot(x, y, c= 'gray', linewidth=1.0) plt.annotate(r'CaT', xy=(8540.0, ylim[1] + 0.05), xycoords='data', annotation_clip=False) plt.annotate(r'H$\alpha$', xy=(6562.8, ylim[1] + 0.05), xycoords='data', annotation_clip=False) plt.annotate(r'NaD', xy=(5892., ylim[1] + 0.05), xycoords='data', annotation_clip=False) plt.annotate(r'Mg$\beta$', xy=(5175., ylim[1] + 0.05), xycoords='data', annotation_clip=False) for i,j in zip(specindex,offset): iraf.noao.onedspec.continuum(input = GCssorted.ORIGINALFILE.iloc[i] + '[1]', output = '/Volumes/VINCE/OAC/continuum.fits', type = 'ratio', naverage = '3', function = 'spline3', order = '5', low_reject = '2.0', high_reject = '2.0', niterate = '10') data = fits.getdata('/Volumes/VINCE/OAC/continuum.fits', 0) hdu = fits.open(GCssorted.ORIGINALFILE.iloc[i]) header1 = hdu[1].header lamRange = header1['CRVAL1'] + np.array([0., header1['CD1_1'] * (header1['NAXIS1'] - 1)]) wavelength = np.linspace(lamRange[0],lamRange[1], header1['NAXIS1']) hdu.close() zp = 1. + (GCssorted.VREL.iloc[i] / 299792.458) plt.plot(wavelength/zp, gaussian_filter(data,2) + j, c = 'black', lw=1) os.remove('/Volumes/VINCE/OAC/continuum.fits')
def plot_result(y, result, threshold, title): """ :param title: :return: 蓝色为原始数据, 亮绿色为均值, 绿色为正常范围上界,蓝色为正常范围下界 """ avg = result[AVG_FILTER] std = result[STD_FILTER] upper_bound = avg + threshold * std lower_bound = avg - threshold * std print("upper_bound: %s, lower_bound: %s" % (upper_bound, lower_bound)) signals = result[Z_SCORE_SIGNALS] data_points_number = np.arange(1, len(y) + 1) plt.subplot(211) plt.plot(data_points_number, y) plt.plot(data_points_number, avg, color="cyan", lw=2) plt.plot(data_points_number, upper_bound, color="green", lw=2) plt.plot(data_points_number, lower_bound, color="blue", lw=2) plt.subplot(212) plt.step(data_points_number, signals, color="red", lw=2) plt.ylim(-1.5, 1.5) plt.savefig(title) plt.show()
X = np.arange(-10, 10, 0.01) Y = np.arange(-10, 10, 0.01) X, Y = np.meshgrid(X, Y) Z = function_1(np.array([X, Y])) # 외곽선 단순화 mask = Z > 10 Z[mask] = 0 plt.subplot(2, 3, idx) idx += 1 plt.plot(x_process[:, 0], x_process[:, 1], '.-', color="blue") plt.contour(X, Y, Z) plt.ylim(-10, 10) plt.xlim(-10, 10) plt.plot(0, 0, '+', color="red") # 극소점 plt.title(key) plt.show() #graph comparision _2 (2 dimensional) idx = 1 for key in optimizers: optimizer = optimizers[key] def process(f, init_x, I=100): x = init_x
# In[100]: get_ipython().run_cell_magic( 'latex', '', '$\\textbf{Visualize the Correlations}: $\n$\\text{Cor}(X_i,Y_j) = \\frac{\\text{Cov}(X_i,Y_j)}{\\sigma_{X_i}\\sigma_{Y_j}}$' ) # In[101]: R = np.corrcoef(data.T) plt.figure(figsize=(10, 8)) plt.pcolor(R) plt.colorbar() plt.xlim([0, len(headers)]) plt.ylim([0, len(headers)]) plt.xticks(np.arange(32) + 0.5, np.array(headers), rotation='vertical') plt.yticks(np.arange(32) + 0.5, np.array(headers)) plt.show() # In[108]: #Lets fit both the models using PCA/FA down to two dimensions. #construct a function implementing the factor analysis which returns a vector of n_components largest # variances and the corresponding components (as column vectors in a matrix). You can # check your work by using decomposition.FactorAnalysis from sklearn #### ~THIS FUNCTION IS WAS A STAB, NEW CODE HERE: ########### def FactorAnalysis(data, n_components):
lon, lat = np.mgrid[-40:40, -40:40] T = temp(lon, lat) sns.set_style("dark") ax = fig.add_subplot(121) plt.scatter(x1, y1, color='firebrick', alpha=0.15) plt.scatter(x2, y2, color='mediumseagreen', alpha=0.15) plt.scatter(x3, y3, color='goldenrod', alpha=0.15) plt.scatter(xL, yL, color='navy') levels = [2.5, 7.5, 12.5, 17.5] CS = ax.contour(lon, lat, T, levels=[2.5, 7.5, 12.5, 17.5], linewidths=1, colors='k') fmt = '%r $^{\circ}$C' ax.clabel(CS, inline=1, fontsize=10, fmt=fmt, **hfont) plt.xlabel('$^{\circ}$E', fontsize=18, **hfont) plt.ylabel('$^{\circ}$N', fontsize=18, **hfont) ax.tick_params(labelbottom=False, labelleft=False) plt.title('(a)', fontsize=18, **hfont) plt.xlim(-40, 40) plt.ylim(-40, 40) plt.savefig('/Users/nooteboom/Documents/PhD/firstpaper/articleplots/plots/' + 'illustration_PDFS.pdf', bbox_inches="tight") plt.show()
train_scores_mean = np.mean(train_scores, axis=1) train_scores_std = np.std(train_scores, axis=1) test_scores_mean = np.mean(test_scores, axis=1) test_scores_std = np.std(test_scores, axis=1) plt.grid() plt.fill_between(train_sizes, train_scores_mean - train_scores_std, train_scores_mean + train_scores_std, alpha=0.1, color="r") plt.fill_between(train_sizes, test_scores_mean - test_scores_std, test_scores_mean + test_scores_std, alpha=0.1, color="g") plt.plot(train_sizes, train_scores_mean, '-', color="r", label="Training score") plt.plot(train_sizes, test_scores_mean, '-', color="g", label="Cross-validation score") plt.ylim(0,1.2) plt.xlim(0,200) plt.legend(loc="best") plt.xlabel("Train test size") plt.savefig("learning_curves.png") plt.close("all") # plot the model vs the predictions for what_plot in [0,1,2,3]: fig=plt.figure(figsize=(16,8)) # ax1 = fig.add_subplot(1,2,1); ax2 = fig.add_subplot(1,2,2) ax1.tick_params(labelsize=20); ax2.tick_params(labelsize=20) #
yerr = SchuberthMatch['e.1'] print 'rms (VIMOS - Schuberth) GCs = ', np.std(x - y) plt.close('all') plt.figure(figsize=(6, 6)) plt.errorbar(x, y, yerr=yerr, xerr=xerr, fmt='o', c='black', label='Schuberth et al.') plt.plot([-200, 2200], [-200, 2200], '--k') plt.xlim(-200, 2200) plt.ylim(-200, 2200) x = VIMOS['r_auto'] y = SchuberthMatch['Rmag'] plt.scatter(x, y, c='black') # ---------------------------------- # ---------------------------------- cat1 = coords.SkyCoord(stars['RA_g'], stars['DEC_g'], unit=(u.degree, u.degree)) cat2 = coords.SkyCoord(S_Stars['RA'], S_Stars['DEC'], unit=(u.degree, u.degree))
import numpy as np from matplotlib.pylab import plt fig, ax = plt.subplots() plt.legend() ax.spines['left'].set_position('zero') ax.spines['right'].set_color('none') ax.spines['bottom'].set_position('zero') ax.spines['top'].set_color('none') plt.grid() plt.ylim(-4.5,4.5) plt.xlim(-5.5,5.5) ax.xaxis.set_ticks([-5,-4,-3,-2,-1,0,1,2,3,4,5]) plt.show()
plt.show() #graph 2 (contour graph) x = np.arange(-5, 5, 0.01) y = np.arange(-5, 5, 0.01) X, Y = np.meshgrid(x, y) Z = function_1(np.array([X, Y])) idx = 1 plt.subplot(2, 2, idx) idx += 1 plt.plot( x_process[:,0], x_process[:,1], '.-', color="blue") #수렴과정 plt.contour(X, Y, Z) plt.ylim(-5, 5) plt.xlim(-5, 5) plt.plot(0, 0, '+', color = "red") #극소점 plt.show() #graph 3 (3 dimensional graph) x, x_process = gradient_descent_process(function_1, np.array([-3.0, 4.0]), L = 0.1, I = 50 ) x = np.arange(-5, 5, 0.01) y = np.arange(-5, 5, 0.01) x, y = np.meshgrid(x, y) z = function_1(np.array([X, Y]))
import numpy as np from matplotlib.pylab import plt fig, ax = plt.subplots() plt.legend() ax.spines['left'].set_position('zero') ax.spines['right'].set_color('none') ax.spines['bottom'].set_position('zero') ax.spines['top'].set_color('none') plt.grid() plt.ylim(-5.5, 5.5) plt.xlim(-5.5, 5.5) ax.xaxis.set_ticks([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]) ax.yaxis.set_ticks([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]) xs = np.linspace(-1, 1, 5) up_ys = np.repeat(2, 5) bottom_ys = np.repeat(-2, 5) plt.scatter(xs, up_ys, c="r") plt.scatter(xs, bottom_ys, c="r") plt.scatter(1, -5, c="r") xs = np.concatenate((xs, xs, [1]), axis=0) ys = np.concatenate((up_ys, bottom_ys, [-5]), axis=0) u, s, vh = np.linalg.svd(np.array((xs, ys)).T) v0 = vh[0] # plt.plot([0,0], [5,-5], label="(i)", linewidth="3") # plt.plot([-1,-1], [5,-5], label="(ii)", linewidth="3", c="g")
((result['g_auto'] - result['r_auto']) > (-0.2 + 0.6 * (result['g_auto'] - result['i_auto']))) & ((result['g_auto'] - result['i_auto']) > 0.5) & ((result['g_auto'] - result['i_auto']) < 1.3) & ((result['i_auto']) < 24)) subset = result[mask] subset = subset.sample(n=1000) plt.figure() plt.scatter(result['g_auto'] - result['i_auto'], result['g_auto'] - result['r_auto'], s=10, c='gray', edgecolor='none', alpha = 0.5) plt.scatter(subset['g_auto'] - subset['i_auto'], subset['g_auto'] - subset['r_auto'], s=20, c='blue', edgecolor='none') plt.scatter(GCs['g_auto'] - GCs['i_auto'], GCs['g_auto'] - GCs['r_auto'], s=10, c='red', edgecolor='none') plt.xlabel('(g - i)') plt.ylabel('(g - r)') plt.xlim(-1,4) plt.ylim(-1,4) plt.figure() plt.scatter(subset['g_auto'] - subset['r_auto'], subset['r_auto'], s=30, c='blue', edgecolor='none') plt.scatter(GCs['g_auto'] - GCs['r_auto'], GCs['i_auto'], s=8, c='red', edgecolor='none') plt.ylim(13,24) plt.gca().invert_yaxis() plt.xlabel('(g - i)') plt.ylabel('i') plt.figure() plt.scatter(result['g_auto'] - result['u_auto'], result['g_auto'] - result['r_auto'], s=10, c='gray', edgecolor='none', alpha = 0.5) plt.scatter(subset['g_auto'] - subset['u_auto'], subset['g_auto'] - subset['r_auto'], s=20, c='blue', edgecolor='none') plt.scatter(GCs['g_auto'] - GCs['u_auto'], GCs['g_auto'] - GCs['r_auto'], s=10, c='red', edgecolor='none') plt.scatter(galaxies['g_auto'] - galaxies['u_auto'], galaxies['g_auto'] - galaxies['r_auto'], s=10, c='green', edgecolor='none') plt.xlabel('(g - u)')
def train(): results_wins_agent1 = [] results_draws = [] solved = False last_win_percentage = 0 for i_episode in range(args.episodes): model.train() state, ep_reward = env.reset(), 0 for _ in range(1, 10000): action = select_action(state) state, reward, done, _ = env.step(action) if args.render: env.render() model.rewards.append(reward) ep_reward += reward if done: break finish_episode(i_episode) if i_episode % args.evaluation_interval == 0: model.eval() print("Comparing @ Episode", i_episode, end=': ') _, win_percentage_agent1, draws = run_compare(model) results_wins_agent1.append([i_episode, win_percentage_agent1]) results_draws.append([i_episode, draws]) avg_win_percentage_agent1 = 0.5 * (win_percentage_agent1 + last_win_percentage) if avg_win_percentage_agent1 > args.solved: solved = True last_win_percentage = win_percentage_agent1 writer.add_scalar("Win percentage", win_percentage_agent1, i_episode) writer.add_scalar("Draws percentage", 100 * draws / args.evaluation_games, i_episode) if solved: print( "Solved after {} episodes! The last win percentage was {:2,f}". format(i_episode, last_win_percentage)) break if not solved: print("Not Solved after {} episodes!".format(args.episodes)) arr_results_wins_agent1 = np.array(results_wins_agent1) fig = plt.figure(figsize=(8, 6)) plt.ylim(0, 110) plt.plot(arr_results_wins_agent1[:, 0], arr_results_wins_agent1[:, 1], label="Wins Percentage Agent 1") plt.legend(loc='lower right') plt.xlabel("Episode") plt.ylabel("Win percentage") plt.show() fig.savefig('train_win_percentage_' + os.path.basename(args.model_path) + '.png')
def main(): results_wins_agent1 = [] results_draws = [] running_reward = 10 for i_episode in range(args.episodes): policy.train() state, ep_reward = env.reset(), 0 for _ in range(1, 10000): action = select_action(state) state, reward, done, _ = env.step(action) if args.render: env.render() policy.rewards.append(reward) ep_reward += reward if done: break running_reward = 0.05 * ep_reward + (1 - 0.05) * running_reward finish_episode() solved = False last_win_percentage = 0 if i_episode % args.log_interval == 0: print("Comparing @ Episode", i_episode, end=': ') policy.eval() n_games, wins_agent_one, draws = run_compare(policy) results_wins_agent1.append([i_episode, wins_agent_one]) results_draws.append([i_episode, draws]) if wins_agent_one > args.solved: solved = True last_win_percentage = wins_agent_one if solved: print( "Solved after {} episodes! The last win percentage was {:2,f}". format(i_episode, last_win_percentage)) break arr_results_wins_agent1 = np.array(results_wins_agent1) fig = plt.plot(arr_results_wins_agent1[:, 0], arr_results_wins_agent1[:, 1], label="Wins Percentage Agent 1") plt.legend(loc='lower right') plt.xlabel("Episode") plt.ylabel("Win percentage") plt.ylim(0, 110) plt.show() fig.clear() print("") print("Running final comparisons:") policy.eval() n_games = 1000 agent_class1 = PyTorchAgent agent_classes = [ RandomAgent, MaxScoreAgent, MaxScoreRepeatAgent, MinimaxAgent ] agents = [] win_percentage_for_agent = [] for agent_class2 in agent_classes: agent1 = agent_class1(policy) agent2 = agent_class2() [wins_agent_one, _, draws, _] = compare_agents(args.bins, args.seeds, n_games, agent1, agent2) win_percentage = 100 * wins_agent_one / (n_games - draws) if draws != n_games: print(agent_class1.__name__, "won", win_percentage, "% ( n =", wins_agent_one, ") of all N =", n_games, "games against", agent_class2.__name__, "Number of draws:", draws) else: print("Only draws in", agent_class1.__name__, "vs", agent_class2.__name__) agents.append(agent_class2.__name__) win_percentage_for_agent.append(win_percentage) plt.clf() fig, ax = plt.subplots() x = np.arange(len(agents)) width = 0.35 ax.bar(x - width / 2, win_percentage_for_agent, width) ax.set_ylabel('Win percentage') ax.set_xlabel('Opponent') ax.set_title('Wins against various agents') ax.set_xticks(x) ax.set_xticklabels(agents) ax.set_ylim(0, 110) plt.show() fig.clear()
sns.set_palette("Blues_r", n_ts * 2) for msm in all_msms: timescales.append(msm.timescales_[:n_ts]) lags.append(msm.get_params()['lag_time']) lags = np.array(lags) timescales = np.array(timescales).T / ps_to_ns with sns.plotting_context("notebook", font_scale=1.5): for i in range(len(timescales)): plt.plot(lags, timescales[i]) plt.xlabel('Lag time $ps$') plt.ylabel('Implied timescale $ns$') plt.savefig('figures/rmsd_msm_ts_vs_lag.png', transparent=True) ymax = (timescales[1].max() // 10 + 1) * 10 plt.ylim((0, ymax)) plt.savefig('figures/rmsd_msm_ts_vs_lag-detail.png', transparent=True) msm = all_msms[np.extract(lags == 2000, np.arange(len(lags)))[0]] m = 2 plt.clf() with sns.plotting_context("talk", font_scale=1.5): vec = msm.left_eigenvectors_ n_states = vec.shape[ 0] # may be less than 200 as T may be non-ergodic. fig, axes = plt.subplots(nrows=m, sharex=True) for i in range(m): axes[i].bar(range(n_states), vec[:, i], label='Eigenvector {}'.format(i + 1),
GCs = pd.read_csv('/Volumes/VINCE/OAC/GCs_903.csv', dtype = {'ID': object}, comment = '#') # ---------------------------------- rep1 = GCs[GCs.Alt1.isin(GCs.ID)] df1 = pd.DataFrame() df2 = pd.DataFrame() for j in range(0,len(rep1)): df1.iloc[j] = rep1.iloc[j] x = VIMOS['VREL_helio'] xerr = VIMOS['VERR'] y = SchuberthMatch['HRV'] yerr = SchuberthMatch['e.1'] print 'rms (VIMOS - Schuberth) GCs = ', np.std(x-y) plt.close('all') plt.figure(figsize=(6,6)) plt.errorbar(x, y, yerr= yerr, xerr = xerr, fmt = 'o', c ='black', label = 'Schuberth et al.') plt.plot([-200, 2200], [-200, 2200], '--k') plt.xlim(-200,2200) plt.ylim(-200,2200) x = VIMOS['r_auto'] y = SchuberthMatch['Rmag'] plt.scatter(x, y, c ='black')