def plotmhd(file_DRR_AP, title=None, margin=0.0, dpi=300): #plot the mhd DRR and return the ax and plt itkimage = sitk.ReadImage(file_DRR_AP) nda = sitk.GetArrayFromImage(itkimage) spacing = itkimage.GetSpacing() xsize = nda.shape[1] ysize = nda.shape[0] figsize = (1 + margin) * xsize / dpi, (1 + margin) * ysize / dpi fig = plt.figure(figsize=figsize, dpi=dpi) #print "image pixel numbers", xsize, ysize dpi:dots per inch #print "figure size", figsize #ax = fig.add_subplot(111) ax = plt.Axes(fig, [0, 0, 1, 1], frameon=False) fig.add_axes(ax) extent = (0, xsize * spacing[0], ysize * spacing[1], 0 ) # the same orientation as DRR #print "extent", extent t = ax.imshow(nda, extent=extent, interpolation=None) if nda.ndim == 2: t.set_cmap("gray") if (title): plt.tile(title) #plt.show() return ax, fig
def plot_cross_validation(self, figsize=(6, 4), figname="metrics.png", show=True): """ Plots Cross validation Input: - figsize: tuple, the size of the metric curve - figname: string, what you want the combined plot to be saved as - show: boolean, whether you want to plt.show() your figure or just save it to your computer """ file_name = "K_fold_Cross_Validation.png" # check_for_array(self.cross_val, "Cross_Val") plt.figure(figsize=figsize) plt.tile("K-fold Cross Validation", fontsize=14) plt.yticks(fontsize=14) plt.xticks(fontsize=14) plt.ylabel("Folds", fontsize=14) plt.xlabel("Accuracy", fontsize=14) # plt.plot(self.loss) plt.plot(self.cross_val) plt.savefig(file_name) if show: plt.show() plt.close()
plt.savefig(out+'/yq_tot_hist.png', dpi=190) if False: out='/home/dw1519/dw1519/galex/plots/co239-10/radec' co_list = [] info_list = [] for i in range(0, 28): co = np.load(out+'/{0}.npy'.format((i+1)*100))/36000./800/0.001666*2400 info = np.load(out+'/info_{0}.npy'.format((i+1)*100))[1:] co_list.append(co) info_list.append(info) co = np.concatenate(co_list, axis=0) info = np.concatenate(info_list, axis=0) plt.hist(info[:,1],32,alpha=0.5) plt.tile('{0}, photons'.format(32, np.sum(info[:,1]<2)/np.sum(info[:,1]>=0))) plt.savefig(out+'/ya_tot_hist.png', dpi=190) if False: scan = '2813' name = 'AIS_GAL_SCAN_0'+scan+'_0003' out = '../data/'+name+'-cal-sec' data = np.load('../data/'+name+'-cal-sec/photon_match_d_081017.npy') print scan data_mask = data[:,-3]>-1 data = data[data_mask] dy = data[:,-1]/36000./800/0.001666*2400 dx = data[:,-2]/36000./800/0.001666*2400 ya = data[:,-5] q = data[:,-6] ya = data[:,-7]
def save_plot(self,circuit,i,log=True,name='',**kwargs): v = circuit.evaluate(self.spice_commands[i]) #For every measurement in results for k in v.keys(): score = self._rank(v,i,k) plt.figure() freq = v[k][0] gain = v[k][1] goal_val = [self.ff[i](c,k) for c in freq] if self.plot_weight: weight_val = [self.fitness_weight[i](c,k) for c in freq] if self.constraints[i]!=None and self.plot_constraints: constraint_val = [not self.constraints[i](freq[c],gain[c],k) for c in xrange(len(freq))] if log==True:#Logarithmic plot plt.semilogx(freq,gain,'g',basex=10) plt.semilogx(freq,goal_val,'b',basex=10) if self.plot_weight: plt.semilogx(freq,weight_val,'r--',basex=10) if self.plot_constraints: plt.semilogx(freq,constraint_val,'m',basex=10) else: plt.plot(freq,gain,'g') plt.plot(freq,goal_val,'b') if self.plot_weight: plt.plot(freq,weight_val,'r--') if self.constraints[i]!=None and self.plot_constraints: plt.plot(freq,constraint_val,'m') # update axis ranges ax = [] ax[0:4] = plt.axis() # check if we were given a frequency range for the plot if k in self.plot_yrange.keys(): plt.axis([min(freq),max(freq),self.plot_yrange[k][0],self.plot_yrange[k][1]]) else: plt.axis([min(freq),max(freq),min(-0.5,-0.5+min(goal_val)),max(1.5,0.5+max(goal_val))]) if self.sim_type[i]=='dc': plt.xlabel("Input (V)") if self.sim_type[i]=='ac': plt.xlabel("Input (Hz)") if self.sim_type[i]=='tran': plt.xlabel("Time (s)") if self.plot_titles!=None: try: plt.title(self.plot_titles[i][k]) except: plt.tile(k) plt.annotate('Generation '+str(self.generation),xy=(0.05,0.95),xycoords='figure fraction') if score!=None: plt.annotate('Score '+'{0:.2f}'.format(score),xy=(0.75,0.95),xycoords='figure fraction') plt.grid(True) # turn on the minor gridlines to give that awesome log-scaled look plt.grid(True,which='minor') if k[0]=='v': plt.ylabel("Output (V)") elif k[0]=='i': plt.ylabel("Output (A)") plt.savefig('plot'+strftime("%Y-%m-%d %H:%M:%S")+'-'+k+'-'+name+'.png')
if False: out = '/home/dw1519/dw1519/galex/plots/co239-10/radec' co_list = [] info_list = [] for i in range(0, 28): co = np.load(out + '/{0}.npy'.format( (i + 1) * 100)) / 36000. / 800 / 0.001666 * 2400 info = np.load(out + '/info_{0}.npy'.format((i + 1) * 100))[1:] co_list.append(co) info_list.append(info) co = np.concatenate(co_list, axis=0) info = np.concatenate(info_list, axis=0) plt.hist(info[:, 1], 32, alpha=0.5) plt.tile('{0}, photons'.format( 32, np.sum(info[:, 1] < 2) / np.sum(info[:, 1] >= 0))) plt.savefig(out + '/ya_tot_hist.png', dpi=190) if True: scan = '0815' name = 'AIS_GAL_SCAN_0' + scan + '_0001' out = '../data/' + name + '-cal-sec' data = np.load('../data/' + name + '-cal-sec/photon_match_d_072217.npy') print scan dy = data[:, -1] / 36000. / 800 / 0.001666 * 2400 dx = data[:, -2] / 36000. / 800 / 0.001666 * 2400 q = data[:, -6] ya = data[:, -7]
r2_score(y_train, model.predict(x_train)) model.score(x_train, y_train) model.score(x_test, y_test) ''' = 0.5171.. => 낮은 예측률 따라서 리그레이션 말고 다른 모델로 예측하기 추천 ''' plt.scatter(y_train, model.predict(x_train)) plt.xlabel('true values') plt.ylabel('predictions') plt.title('train') plt.scatter(y_test, pred) plt.xlabel('true values') plt.ylabel('predictions') plt.tile('test') #cross validation from sklearn.cross_validation import cross_val_score, cross_val_predict scores = cross_val_score(model, x_train, y_train, cv=6) pred = cross_val_predict(model, x_train, y_train, cv=6) plt.scatter(y, pred) #k fold cross validation import numpy as np from sklearn.model_selection import KFold x = np.array([[1, 2], [3, 4], [1, 2], [3, 4]]) y = np.array([1, 2, 3, 4]) kf = KFold(n_splits=2) kf.get_n_splits(x)
import matplotlib.pyplot as plt import pandas as pd file = pd.read_csv("C:/Users/gestor/Downloads/a2_MANCHAS(1).csv") x = list(file["Ano"]) y = list(file["manchas"]) plt.plot(x, y) plt.tile("Demonstrando as manchas solares de ano em ano(30 primeiros)", fontsize=15, color="r", style="italic", family="monospace") plt.title("Demonstrando as manchas solares de ano em ano(30 primeiros)", fontsize=15, color="r", style="italic", family="monospace") plt.xlabel("Ano", size=12) plt.ylabel("Manchas", size=12) plt.grid() plt.tight_layout() plt.savefig("30first.jpg") plt.savefig("30first.png")
### Covariances cov = [] for i in range(20, 66): tryt = g.loc[g['age'] == i].cov() tryt = np.array(tryt) cov.append(tryt[0:3, 0:3]) del tryt cov_ci = np.zeros((46, 1)) for j in range(0, 46): cov_ci[j, 0] = cov[j][0, 1] plt.plot(ages, cov_ci) plt.tile('Lifecycile CI Covariance') del cov_ci cov_cw = np.zeros((46, 1)) for j in range(0, 46): cov_cw[j, 0] = cov[j][0, 2] plt.plot(ages, cov_cw) plt.tile('Lifecycile CW Covariance') del cov_cw cov_wi = np.zeros((46, 1)) for j in range(0, 46): cov_wi[j, 0] = cov[j][1, 2] plt.plot(ages, cov_wi)
plt.title('PCA Variance across each dimension') pca_top2 = pca.transform(x)[:, :2] plt.figure() plt.scatter(pca_top2[:, 0], pca_top2[:, 1], s=0.01) plt.title('PCA Top-2 Dimensions') from sklearn.manifold import TSNE tsne = TSNE(n_components=2, perplexity=30, learning_rate=100) tsne_dims2 = tsne.fit_transform(x) tsne_c = TSNE(n_components=2, perplexity=30, learning_rate=100) tsne_dims2_c = tsne_c.fit_transform(x_c) plt.figure() plt.scatter(tsne_dims2[:, 0], tsne_dims2[:, 1], s=0.1, c='r') #plt.title('Clean_Data: TSNE (down-to) 2 Dimensions'); plt.scatter(tsne_dims2_c[:, 0], tsne_dims2_c[:, 1], s=0.1, c='b') #plt.title('Corrupt_data: TSNE (down-to) 2 Dimensions'); plt.tile('tSNE') plt.show() #%% 6. FFT on the time-series data fft_x = np.abs(np.fft.fft(x, axis=1)) fft_x = fft_x[:, :int(np.ceil(x.shape[1] / 2)) + 1] for label in range(y.shape[-1]): labelled = fft_x[np.where(y[:, label] == 1)[0], :] fig_rows, fig_cols = 5, 2 fig, ax = plt.subplots(fig_rows, fig_cols, figsize=(16, 5)) row_inds = np.random.choice(labelled.shape[0], fig_rows * fig_cols, replace=False) for fig_row in range(fig_rows): for fig_col in range(fig_cols): ax[fig_row, fig_col].plot(
#rf = np.fft.fft(r[iworm, :ntimes]); #rf = 2.0/ntimes * np.abs(rf[:ntimes//2]); #ff = np.linspace(0.0, 1.0/(2.0*dt), ntimes//2) ### FFT ff, pfsd_r, = signal.periodogram(d[iworm, :ntimes], 1 / dt) nmean = 50 pfsd_r_m = np.convolve(pfsd_r, np.ones(nmean) / nmean, mode='same') plt.figure(1 * fi) plt.clf() plt.semilogy(ff, pfsd_r) plt.semilogy(ff, pfsd_r_m) plt.tile('power %s %s %d' % (strain, fn, iworm)) ### Spectogram f, t, Sxx = signal.spectrogram(d[iworm, :ntimes], 1 / dt, nperseg=2**14, noverlap=2**14 - 2**8) plt.figure(2 * fi) plt.clf() ax = plt.subplot(2, 1, 1) nfmax = 50 plt.pcolormesh(t, f[:nfmax], Sxx[:nfmax, :]) plt.ylabel('Frequency [Hz]') plt.xlabel('Time [sec]') plt.tile('spectogram %s %s %d' % (strain, fn, iworm))
""" h1 = np.arange(-1, 1, res1) gauss = intensity(wi, lamda, P, -1, 1, res1) """ BEAM NEL FUOCO """ h2 = np.arange(-0.01, 0.01, res2) gauss_after_lens = intensity(w1, lamda, P, -0.01, 0.01, res2) """ BEAM DOPO LA SECONDA LENTE """ h3 = np.arange(-10, 10, res3) gauss_after_second_lens = intensity(w2, lamda, P, -10, 10, res3) plt.figure(1) plt.tile('Beam before the SF') plt.plot(h1, gauss, color="Red") plt.xlabel("Contour Radius (mm)") plt.ylabel("Percent Irradiance") plt.show() plt.clf() plt.figure(2) plt.tile('Beam in the focal point (pinhole position)') plt.plot(h2, gauss_after_lens, color="Red") plt.xlabel("Contour Radius (mm)") plt.ylabel("Percent Irradiance") plt.xticks(np.arange(-0.01, 0.015, step=0.005)) plt.show() plt.clf()
# Building the Model model = tf.keras.Sequential([ tf.keras.layers.Embedding(tokenizer.vocab_size, 64), tf.keras.layers.Conv1D(128, 5, activation='relu'), tf.keras.layers.GlobalAveragePooling1D(), tf.keras.layers.Dense(64, activation='relu'), tf.keras.layers.Dense(1, activation='sigmoid') ]) print(model.summary()) # Compile the model model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # Train the model NUM_EPOCHS = 1 # Traning only on single Epoch, as it takes a long time to train. Try with more Epochs! history = model.fit(train_dataset, epochs=NUM_EPOCHS, validation_data=test_dataset) print(history.history.keys()) plt.plot(history.history['acc']) plt.xlabel('Epochs') plt.ylabel('Accuracy') plt.tile('Model Training') plt.show()
cummulate_survival_ratio = [] for i in range(1, 80): cummulate_survival_ratio.append( df_train[df_train["Age"] < i]["Survived"].mean()) pd.DataFrame([0, pd.NA, 1]).mean() cummulate_survival_ratio = [] for i in range(1, 80): cummulate_survival_ratio.append( df_train["Survived"][df_train["Age"] < i].mean()) plt.figure(figsize=(7, 7)) plt.plot(cummulate_survival_ratio) plt.tile("Survival rate change depending on range of Age", y=1.02) plt.ylabel("Survival rate") plt.xlabel("Range of Age(0~x)") plt.show() f, ax = plt.subplots(1, 2, figsize=(18, 8)) sns.violinplot("Pclass", "Age", hue="Survived", data=df_train, scale="count", split=True, ax=ax[0]) ax[0].set_title("Pclass and Age vs Survived") ax[0].set_yticks(range(0, 110, 10)) sns.violinplot("Sex",
sizes = [15, 30, 45, 10] # 占比 explode = (0, 0.1, 0, 0) plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',shadow=False, startangle=90) # 饼图 plt.axis('equal') # 例2 直方图: np.random.seed(0) mu, sigma = 100, 20 a = np.random.normal(mu, sigma, size=100) plt.hist(a, 20, # 直方图的个数 normed=1, histtype='stepfilled', facecolor='b', alpha=0.75) plt.tile('Histogram') plt.show() # 例3:散点图 面向对象的方式绘制 fig, ax = plt.subplots() # 将subplots 函数变成object,分别对应函数生成的图表和图表对应的区域; # 为空时,默认为111,对应的绘图区域(即当前的绘图区域)是ax ax.plot(10*np.random.randn(100), 10*np.random.randn(100), 'o') # 在ax(绘图区)中绘制 ax.set_title('Simple Scatter') # 使用ax这种面向对象的方法,所有.plot函数和标题设置函数 变成了object的methods,而不再是plt下面的函数。官方推荐该方法。 plt.show() # √ 横轴是日期 plt.plot_date(date,data) # √ 将 x轴 y轴改为对数标度 pylab.semilogx()