def shist(self,data,sflag,spath,sname,pflag): g.tprinter('Running sist',pflag) xdata=data[:,0] ydata=data[:,1] plt.plot(xdata,ydata,'ro') plt.savefig(os.path.join(spath,str(sname))+'.pdf') plt.close()
def plot2D(x,y,x_ex,y_ex,ylabl): #static variable counter plot2D.fig_num += 1 plt.subplot(2,2,plot2D.fig_num) plt.xlabel('$x$ (cm)') plt.ylabel(ylabl) plt.plot(x,y,"b+-",label="Lagrangian") plt.plot(x_ex,y_ex,"r--",label="Exact") plt.savefig("var_"+str(plot2D.fig_num)+".pdf")
def plot(self, output): plt.figure(figsize=output.fsize, dpi=output.dpi) for ii in range(0, len(self.v)): imsize = [self.t[0], self.t[-1], self.x[ii][-1], self.x[ii][0]] lim = amax(absolute(self.v[ii])) / output.scale_sat plt.imshow(self.v[ii], extent=imsize, vmin=-lim, vmax=lim, cmap=cm.gray, origin='upper', aspect='auto') plt.title("%s-Velocity for Trace #%i" % (self.comp.upper(), ii)) plt.xlabel('Time (s)') plt.ylabel('Offset (km)') #plt.colorbar() plt.savefig("Trace_%i_v%s.pdf" % (ii, self.comp)) plt.clf()
def viz_losses(filename, losses): if '.' not in filename: filename += '.png' x = history['epoch'] legend = losses.keys for v in losses.values: plt.plot(np.arange(len(v)) + 1, v, marker='.') plt.title('Loss over epochs') plt.xlabel('Epochs') plt.xticks(history['epoch'], history['epoch']) plt.legend(legend, loc = 'upper right') plt.savefig(filename)
def plot_hist(a,b): v = np.random.beta(a, b, 10000) s = np.zeros(v.shape[0]) for i in range(v.shape[0]): s[i] = np.random.binomial(20,v[i]) figure() center = scipy.stats.itemfreq(s)[:,0] hist = scipy.stats.itemfreq(s)[:,1] plt.bar(center, hist, align = 'center', width = 0.7) plt.savefig('../Figures/a'+str(a)+'_b'+str(int(b))+'.pdf')
def plot2D(x,y,ylabl,x_ex=None,y_ex=None): #static variable counter plot2D.fig_num += 1 plt.subplot(2,2,plot2D.fig_num) plt.xlabel('$x$ (cm)') plt.ylabel(ylabl) plt.plot(x,y,"b+-",label="Numerical") if (x_ex != None): plt.plot(x_ex,y_ex,"r-x",label="Exact") plt.savefig("var_"+str(plot2D.fig_num)+".pdf")
def plot_confusion_matrix(cm, labels, title='Confusion matrix', cmap=plt.cm.Blues, save=False): plt.figure() plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() tick_marks = np.arange(len(labels)) plt.xticks(tick_marks, labels, rotation=45) plt.yticks(tick_marks, labels) plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label') plt.show() if save: plt.savefig(save)
def print_scatter_data(): import matplotlib.pylab as plt filename = Par.dirname + ('/Scatter.dat') fitnesses = [] self_reliences = [] life_times = [] self_reliences_dead = [] for Agent in Par.Agents: if Agent.dead != True: fitnesses.append(Agent.fitness) Needs = Agent.needs Production = Agent.production selfReli = [0.0]*Par.num_resources for i in range(Par.num_resources): selfReli[i] = Production[i]*Needs[i] self_reliences.append(abs(sum(selfReli))) else: life_time = Agent.t_death - Agent.t_discovery life_times.append(life_time) Needs = Agent.needs Production = Agent.production selfReli = [0.0]*Par.num_resources for i in range(Par.num_resources): selfReli[i] = Production[i]*Needs[i] self_reliences_dead.append(abs(sum(selfReli))) file =open(filename, 'w') for i in range(len(fitnesses)): s= str(fitnesses[i]) +' '+ str(self_reliences[i]) file.write(s) file.write('\n') file.close() plt.scatter(self_reliences, fitnesses) plt.ylabel('Fitness') plt.xlabel('self_reliences') plt.savefig('FitnessVSR.png') plt.close() plt.scatter(self_reliences_dead, life_times) plt.ylabel('LifeTimes') plt.xlabel('self_reliences') plt.savefig('LifeTimeVSR.png') plt.close()
def dataSet(temps, motorFreqs, durations=10.0, numMeas=10, pauseTime=1, sampRates=2048, tempTime=120, motorTime=60, printDataBool=False, plotResults = True, substance='Water' ): initialize(substance) #Initialize ports, files, and constants (see below) for temp in temps: stirFreq = 400.0 #Set stirring speed for bath setBath(temp, stirFreq, minStableTime=tempTime) #Set temperature and wait for stability (see below) for i in range(motorFreqs.size): #Cycle through motorFreqs motorFreq = motorFreqs[i] print 'motorFreq is ' + str(motorFreq) + ' deg/sec.' sampRate = sampRates[i] print 'sampRate is ' + str(sampRate) + ' samples/sec.' duration = durations[i] # before each measurement, we reset the chains by stirring them up, and then letting them reform print 'Spinning rotor for ' + str(motorTime) + ' seconds.' setMotorFreq(stirFreq, sleepTime=motorTime) # Prevent sedimentation print 'Pausing rotor for ' + str(pauseTime) + ' seconds.' setMotorFreq(0, sleepTime=pauseTime) # Pause, perhaps to wait for chains to form breakBool = setMotorFreq(motorFreq, sleepTime=motorTime) #Set motor and checks if motorFreq is too high #(high value may be changed in method setMotorFreq) (see below) if breakBool: break #Break if motorFreq is above threshold rotorFreqs = getData(numMeas, countTime=duration, sampRate=sampRate, printDataBool=printDataBool) #Data retrieval (see below) plt.savefig('Figs/'+time.strftime("%y-%m-%d-%H-%M-%S", time.localtime())+'-'+str(motorFreq)+'.png') data = processData(temp, motorFreq, rotorFreqs) #Data processing (see below) # calculate the std. error: #se = numpy.std(rotorFreqs) / sqrt(numpy.size(rotorFreqs)) # Data recording: temp. - motorFreq. - avg. rotor Freq. - std. err. - sampRate - duration writeToFile( data ) print 'Measurements at ' + str(temp) + ' degrees C completed.' closeUp() if plotResults and len(data) > 1: kplt.plotData(os.getcwd(), Filename, writeFile=True, fit=False)
def plot_data(): import matplotlib.pylab as plt t, suffering = np.loadtxt('suffering.dat', unpack= True, usecols = (0,1)) t, fitness = np.loadtxt('fitness.dat', unpack = True, usecols = (0,1)) plt.plot(t, suffering) plt.xlabel('t') plt.ylabel('suffering') plt.savefig('suffering.png') plt.close() plt.plot(t, fitness) plt.xlabel('t') plt.ylabel('fitness') plt.savefig('fitness.png') plt.close()
def Transit(lc, PGpoints, intrans, starpos): #Takes in shape. Draws it as array. Moves it across the Star. Draws lightcurve. Compares with known ligthcurve movie=0 #Drawing star star = DrawStar(starpos) #Normalising total flux to 1.0 star/=np.sum(star) #Turning the polygon into an array from PIL import Image, ImageDraw img = Image.new('L', (800, 800), 0) ImageDraw.Draw(img).polygon(PGpoints, outline=1, fill=1) objbox=np.array(img) objbox=1-objbox #Setting animation to step 4 pixels at a time step=4.0 lc=lc[lc[:, 0].argsort(), :] timearr=np.linspace(lc[0,0],lc[-1,0],1600) print timearr print lc[:, 0] lightcurvemodel=[] #Drawing initial plots fig=plt.figure(figsize=(6, 12)) ax1=fig.add_subplot(211) ax1.imshow(star[:, :800],cmap='hot'); ax2=fig.add_subplot(212) ax2.plot(lc[:,0],lc[:,1],'--k') ax2.plot(timearr, np.tile(1.0, 1600),'--', color='#CCCCCC') plt.pause(0.002) #Sweep across the star, keeping the object fixed for pix in range(400,2000, step): #Draw an 800x800 box from the star starbox=star[:,(pix-400):(pix+400)] total=objbox*starbox #if np.sum(starbox)!=0:#skipping to where there is some of the star on the screen ax1.cla() ax1.imshow(total,cmap='hot'); #plotting lightcurve rest=np.sum(star[:,:(pix-400)])+np.sum(star[:, (pix+400):]) lightcurvemodel+=[np.sum(total)+np.sum(rest)] ax2.plot(timearr[pix-400],np.array(lightcurvemodel)[-1],'.r') plt.pause(0.002) movie=1 #Setting to record pngs as movie if movie==1: plt.savefig('TransitMovie/Movie'+str(pix)+'.png') plt.pause(1) np.savetxt('OutLightcurve.txt',np.column_stack((timearr[0:len(lightcurvemodel)],np.array(lightcurvemodel))))
def __save(self,n,plot,sfile): p.figure(figsize=sfile) p.xlabel(plot.xlabel) p.ylabel(plot.ylabel) p.xscale(plot.xscale) p.yscale(plot.yscale) p.grid() for curve in plot.curves: if curve[1] == None: p.plot(curve[0],curve[2], label=curve[3]) else: p.plot(curve[0], curve[1], curve[2], label=curve[3]) p.rc('legend', fontsize='small') p.legend(shadow=0, loc='best') p.axes().set_aspect(plot.aspect) if not plot.dir: plot.dir = './plots/' if not plot.name: plot.name = self.__global_name+'_%0*i'%(2,n) if not os.path.isdir(plot.dir): os.mkdir(plot.dir) if plot.pgf: p.savefig(plot.dir+plot.name+'.pgf') else: p.savefig(plot.dir+plot.name+'.pdf', bbox_inches='tight') p.close()
def task4(save=False): knot = [0, 0, 0, 0.3, 0.5, 0.5, 0.6, 1, 1, 1] control = np.array([[0, 0], [2, 3], [7, 5], [9, 2], [13, 1], [10, 1], [7, 1]]) A = spline(knot, 2) for i in range(len(knot) - 1): X = linspace(knot[i], knot[i + 1], 20) Y = A.evalvector(control, X) plt.plot(Y[:, 0], Y[:, 1]) plt.scatter(control[:, 0], control[:, 1]) plt.scatter(6, 3, s=300) plt.scatter(6.1, 3, s=50, color="black") plt.plot((7, 6), (1, 0)) plt.grid() if save: plt.savefig("interpol2.pdf") plt.show()
def plot_top_M_fisherfaces(eigenvec,M,Mpca,mode): if mode == 'None': return None cols = 10 rows = M/cols index = 1 font_size = 10 plt.figure(figsize=(20,20)) for i in range(0,M): plt.subplot(rows,cols,index),plt.imshow(np.reshape(eigenvec[:,i],(46,56)).T, cmap = 'gist_gray') face_title = str("M="+str(i+1)) plt.title(face_title, fontsize=font_size).set_position([0.5,1.05]), plt.xticks([]), plt.yticks([]) # set_position([a,b]) adjust b for height index = index+1 if mode == 'show': plt.show() plt.close() if mode == 'save': title = str("Top 50 Fisherfaces for Mpca="+str(Mpca)) plt.savefig(title) plt.close()
def print_save_all_mean_faces(x_class_mean,global_mean,show,save): rows = 4 cols = 14 index = 1 font_size = 10 plt.figure(figsize=(20,10)) plt.subplot(rows,cols,index), plt.imshow(np.reshape(global_mean,(46,56)).T, cmap = 'gist_gray') title = str("Global Mean") plt.title(title, fontsize=font_size).set_position([0.5,0.95]), plt.xticks([]), plt.yticks([]) index=index+1 for i in range(0,x_class_mean.shape[1]): title = str("Class Mean "+str(i+1)) plt.subplot(rows,cols,index), plt.imshow(np.reshape(x_class_mean[:,i],(46,56)).T, cmap = 'gist_gray') plt.title(title, fontsize=font_size).set_position([0.5,0.95]), plt.xticks([]), plt.yticks([]) index = index+1 if show == 'yes': plt.show() if save == 'yes': plt.savefig('Global and Class Mean') plt.close()
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues, filename='viz\\confusion_matrix.png'): plt.figure() plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label') plt.savefig(filename)
def make_hists(self, X, y): colors = [ 'red', 'tan', 'lime', 'orange', 'black', 'yellow', 'green', 'pink', 'red', 'brown', 'grey', 'purple', 'navy' ] features = (set(self.train_df.keys()) - {'Vote'}) - set(categorical_features) for feature in features: df = pd.DataFrame({ "x": X[feature].values, "class": y.values.flatten() }) _, edges = np.histogram(df["x"], bins=15) histdata = [] labels = [] for n, group in df.groupby("class"): histdata.append(np.histogram(group["x"], bins=edges)[0]) labels.append(n) hist = np.array(histdata) histcum = np.cumsum(hist, axis=0) plt.bar(edges[:-1], hist[0, :], width=np.diff(edges)[0], label=labels[0], align="edge") for i in range(1, len(hist)): plt.bar(edges[:-1], hist[i, :], width=np.diff(edges)[0], bottom=histcum[i - 1, :], color=colors[i], label=labels[i], align="edge") plt.legend(title="class") plt.savefig('hists_label/' + feature + '.jpeg')
def cmp_overall_qoe_per_region(regions, regionName): google_QoE_folder = geographical_data_folder + "google/dataQoE/" azure_QoE_folder = geographical_data_folder + "azure/dataQoE/" amazon_QoE_folder = geographical_data_folder + "amazon/dataQoE/" qoogle_regional_qoes = load_all_session_qoes_per_region(google_QoE_folder) azure_regional_qoes = load_all_session_qoes_per_region(azure_QoE_folder) amazon_regional_qoes = load_all_session_qoes_per_region(amazon_QoE_folder) google_to_draw = [] azure_to_draw = [] amazon_to_draw = [] for r in regions: google_to_draw.extend(qoogle_regional_qoes[r]) azure_to_draw.extend(azure_regional_qoes[r]) amazon_to_draw.extend(amazon_regional_qoes[r]) fig, ax = plt.subplots() draw_cdf(google_to_draw, styles[0], "Google Cloud CDN") draw_cdf(azure_to_draw, styles[1], "Azure CDN (Verizon)") draw_cdf(amazon_to_draw, styles[2], "Amazon CloudFront") ax.set_xlabel(r'Session QoE (0-5)', fontsize=18) ax.set_ylabel(r'Percentage of PlanetLab users', fontsize=18) plt.xlim([0, 5]) plt.ylim([0, 1]) plt.legend(loc=2) imgName = img_folder + "compare_cloud_cdns_QoE_region_" + regionName plt.savefig(imgName + ".jpg") plt.savefig(imgName + ".pdf") plt.savefig(imgName + ".png") plt.show()
def rader_plot(x, y): pi = 3.1415 # number of variable plt.figure(figsize=(8,8)) categories=x.values.tolist() N = len(categories) # We are going to plot the first line of the data frame. # But we need to repeat the first value to close the circular graph: values = [s for s in y.values.tolist()] # What will be the angle of each axis in the plot? (we divide the plot / number of variable) angles = [n / float(N) * 2 * pi for n in range(N)] # Initialise the spider plot ax = plt.subplot(111, polar=True) # Draw one axe per variable + add labels labels yet plt.xticks(angles, categories, color='grey', size=25) # Draw ylabels ax.set_rlabel_position(0) plt.yticks([0.25, 0.5, 0.75, 1], ["25%","50%","75%","100%"], color="grey", size=7) plt.ylim(0,1) angles.append(angles[0]) values.append(values[0]) # Plot data ax.plot(angles, values, linewidth=1, linestyle='solid') # Fill area ax.fill(angles, values, 'b', alpha=0.1) filename = f"{uid}_{status_id}.png" plt.savefig(filename) plt.close() return filename
def main(): df = pd.read_csv(sys.argv[1]) df_fraud = df.loc[df['Class'] == 1] # Only sample 50,000 data points df_gen = df.loc[df['Class'] == 0].head(50000) df_new = pd.concat([df_fraud, df_gen]) # Drop uninformative features df_new = df_new.drop('Time', axis=1).drop("V13", axis=1).drop( "V15", axis=1).drop("V20", axis=1).drop("V22", axis=1).drop( "V23", axis=1).drop("V24", axis=1).drop("V25", axis=1).drop("V26", axis=1).drop("V28", axis=1) t0 = time() # Execute t-sne X_tsne = manifold.TSNE(learning_rate=500, n_iter=600, verbose=4).fit_transform( df_new.drop('Class', axis=1)) plt.figure() plt.scatter(X_tsne[:, 0], X_tsne[:, 1], c=df_new['Class']) plt.xlabel("x-tsne") plt.ylabel("y-tsne") plt.title('T-SNE on Reduced Credit Card Fraud Detection Dataset') purple_patch = mpatches.Patch(color='purple', label='Genuine') yellow_patch = mpatches.Patch(color='yellow', label='Fraudulent') plt.legend(handles=[purple_patch, yellow_patch]) t1 = time() plt.savefig('tsne_50kpoints_600iterations_partialfeature.png', bbox_inches='tight') print(t1 - t0) plt.show()
def cmp_overall_qoe_cps(): google_QoE_folder = geographical_data_folder + "google/dataQoE/" azure_QoE_folder = geographical_data_folder + "azure/dataQoE/" amazon_QoE_folder = geographical_data_folder + "amazon/dataQoE/" qoogle_session_qoes = load_all_session_qoes(google_QoE_folder) azure_session_qoes = load_all_session_qoes(azure_QoE_folder) amazon_session_qoes = load_all_session_qoes(amazon_QoE_folder) fig, ax = plt.subplots() draw_cdf(qoogle_session_qoes, styles[0], "Google Cloud CDN") draw_cdf(azure_session_qoes, styles[1], "Azure CDN (Verizon)") draw_cdf(amazon_session_qoes, styles[2], "Amazon CloudFront") ax.set_xlabel(r'Session QoE (0-5)', fontsize=18) ax.set_ylabel(r'Percentage of PlanetLab users', fontsize=18) plt.xlim([0, 5]) plt.ylim([0, 1]) plt.legend(loc=2) imgName = img_folder + "compare_cloud_cdns_QoE_overall" plt.savefig(imgName + ".jpg") plt.savefig(imgName + ".pdf") plt.savefig(imgName + ".png") plt.show()
def plot_df( df1, plot_title, x_axis, y_axis, plot, save ): # function for plotting high-dimension and low-dimension eigenvalues y1 = df1[df1.columns[1]] x1 = df1[df1.columns[0]] plt.figure(figsize=(8, 8)) plt.plot(x1, y1, color='red') plt.grid(color='black', linestyle='-', linewidth=0.1) # parameters for plot grid plt.xticks(np.arange(0, max(x1) * 1.1, int(max(x1) / 10))) # adjusting the intervals to 250 plt.yticks(np.arange(0, max(y1) * 1.1, int(max(y1) / 10))) plt.title(plot_title).set_position([0.5, 1.05]) plt.xlabel(x_axis) plt.ylabel(y_axis) plt.legend(loc='best') # creating legend and placing in at the top right if save == 'yes': plt.savefig(plot_title) if plot == 'yes': plt.show() plt.close()
def draw_3D(csvfile, savename, x_name, y_name, z_name1, z_name2): import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import pandas as pd from scipy.interpolate import griddata csv = pd.read_csv(csvfile) x = csv[x_name] y = csv[y_name] z1 = csv[z_name1] z2 = csv[z_name2] # xx, yy = np.meshgrid(x, y) # zz = griddata(x, y, z, xx, yy) ax = plt.subplot(111, projection='3d') ax.scatter(x, y, z1) ax.scatter(x, y, z2) # ax.plot_trisurf(x, y, z) ax.set_xlabel('k') ax.set_ylabel('alpha') ax.set_zlabel('acc') plt.savefig('../eps/' + savename) plt.show()
def ecdf_sca(data, attribute, aggregate=False, datalinks=range(0, NUM_DATA_LINK), save=False): if aggregate: selected_ds = data[data.name.isin([ attribute + '-' + str(i) for i in datalinks ])].groupby('run').mean() else: selected_ds = data[data.name == attribute] plot_ecdf(selected_ds.value.to_numpy()) plt.title("ECDF for " + attribute + (" (aggregated mean)" if aggregate else "")) if save: plt.savefig("ecdf_" + attribute + ".pdf", bbox_inches="tight") plt.clf() else: plt.show() return
def udaljenost_tocke(x1,y1,x2,y2,r): plt.xlim([0,10]) plt.ylim([0,10]) kruznica = plt.Circle((x2,y2),r,color='r') plt.gca().add_artist(kruznica) plt.plot(x1,y1, 'bo') x = (x1-x2)**2 + (y1-y2)**2 d = sqrt(x) nacin = int(input('Unesi 1 za prikaz slike, 2 za spremanje')) if znak ==1: plt.show() elif znak ==2: naziv = input('Unesite naziv slike:') plt.savefig(f'{naziv}.pdf') epsilon = 0.01 if d < r: print(f'Točka je unutar kružnice, a udaljenost je {d}.') elif r-epsilon < d < r+epsilon: print(f'Točka se nalazi na kružnici.') else: print(f'Točka je izvan kružnice, a udaljenost je {d}.')
def draw_line_chart12( csvfile, savename, x_name, y_name_1, y_name_2, ): import matplotlib.pyplot as plt import pandas as pd csv = pd.read_csv(csvfile) x = csv[x_name] y1 = csv[y_name_1] y2 = csv[y_name_2] plt.figure() plt.plot(x, y1, marker='o') plt.plot(x, y2, marker='^') plt.xlabel('number of maps') plt.ylabel('accuracy') plt.grid() plt.legend() plt.savefig('../eps/' + savename) plt.show()
def display(self, data, candidates, fname, display): finallist=[] for c in candidates: finallist.append(c[0]) #print finallist part1 = finallist[:len(finallist)/2] part2 = finallist[len(finallist)/2:] meandiff=int(np.sqrt(np.power(np.mean(part2),2)-np.power(np.mean(part1),2))) rangeA = max(part1)-min(part1) rangeB = max(part2)-min(part2) span = int((rangeA+rangeB)/2) dspan = int(meandiff/span) theta = float(meandiff/(rangeA+rangeB)) oneortwo="" if dspan >3 and meandiff > 20 or meandiff>36: oneortwo = "Two distributions \n\n MD: %d \n Span: %d \n Dspan: %d \n theta: %d" % (meandiff, span, dspan, theta) else: oneortwo = "One distribution \n\n MD: %d \n Span: %d \n Dspan: %d \n theta: %d" % (meandiff, span, dspan, theta) cans = np.array(candidates) plt.plot(cans[:,0],cans[:,1],'ro') plt.axhline(max(cans[:,1])/4, color='r') plt.axhline(max(cans[:,1]/2), color='r') plt.axhline(int(max(cans[:,1]))*0.75, color='r') red_patch = mpatches.Patch(color='red', label='75%, 50% and 25% \nof maximum frequency') plt.legend(handles=[red_patch]) plt.ylabel('Frequency of occurence') plt.xlabel('separate items') plt.title('Frequency distribution estimation graph: %s' %(fname)) plt.text(max(data)*1.1, max(cans[:,1])*0.62, oneortwo, fontsize = 11, color = 'r') plt.hist(data,range(int(min(data)),int(max(data)),1)) ofile = fname[0:-3]+"png" print ("Writing outfile: %s") % (ofile) plt.savefig(ofile, bbox_inches='tight') if display == True: plt.show() return;
def plot_board(self): X = self.X fig = plt.figure(figsize=(5, 5)) plt.xlim(-1, 1) plt.ylim(-1, 1) if self.mu and self.clusters: mu = self.mu clus = self.clusters K = self.K for m, clu in clus.items(): cs = cm.spectral(1. * m / self.K) plt.plot(mu[m][0], mu[m][1], 'o', marker='*', markersize=12, color=cs) plt.plot(zip(*clus[m])[0], zip(*clus[m])[1], '.', markersize=8, color=cs, alpha=0.5) else: plt.plot(zip(*X)[0], zip(*X)[1], '.', alpha=0.5) if self.method == '++': tit = 'K-means++' else: tit = 'K-means with random initialization' pars = 'N=%s, K=%s' % (str(self.N), str(self.K)) plt.title('\n'.join([pars, tit]), fontsize=16) plt.savefig('kpp_N%s_K%s.png' % (str(self.N), str(self.K)), bbox_inches='tight', dpi=200)
def forward(self, x): # polar plot rr = self.rr.data.cpu().detach().numpy() theta = self.theta.data.cpu().detach().numpy() ax = plt.subplot(1, 1, 1, projection='polar') ax.scatter(0, 1, c='black') # unactive poles ax.scatter(theta, rr) ax.scatter(-theta, rr) ax.scatter(np.pi - theta, rr) ax.scatter(theta - np.pi, rr) # ax.set_rmax(1.2) ax.set_title("Dictionary", va='bottom') plt.savefig('usedPolesDCT_NFRAMES.png') plt.show() plt.close() dic = creatRealDictionary(self.T, self.rr, self.theta, self.gid) sparsecode = fista(dic, x, 0.05, 100, self.gid) return Variable(sparsecode)
def plot_scores(scores): plt.figure(figsize=(15, 6)) names, val_scores = [name for name, _, _, _, _, _, _ in scores ], [score for _, score, _, _, _, _, _ in scores] ax = sns.barplot(x=names, y=val_scores) for p, score in zip(ax.patches, val_scores): height = p.get_height() ax.text(p.get_x() + p.get_width() / 2., height + 0.005, '{:1.3f}'.format(score), ha="center", fontsize=14) plt.xlabel('method', fontsize=18) plt.ylabel('Mean Val. Accuracy', fontsize=18) plt.xticks(rotation=90, fontsize=16) plt.yticks(fontsize=16) plt.ylim(0.6, 1) path = WORKING_PATH + "/___comparison.png" plt.savefig(path) plt.clf() return path
def plotdata(trl, tel, tea): xlist = range(len(trl)) ax1 = plt.subplot(2, 1, 1) plt.plot(xlist, trl, 'r-', label='train loss') plt.plot(xlist, tel, 'b-', label='validation loss') plt.ylabel('loss value') plt.title('loss graph') plt.legend(loc=1) ax2 = plt.subplot(2, 1, 2) plt.plot(xlist, tea, 'b-', label='validation acc') #plt.ylim(0, 100) #plt.xlim(0, 100) plt.yticks(range(0, 101, 10)) plt.grid(True) plt.ylabel('acc(%)') plt.title('acc graph') plt.legend(loc=1) plt.tight_layout() plt.savefig('batchNorWithxavier.png', dpi=300) plt.close()
def draw_line_chart14(csvfile, savename, x_name, y_name1, y_name2): import matplotlib.pyplot as plt import pandas as pd csv = pd.read_csv(csvfile) x = csv[x_name] y1 = csv[y_name1] y2 = csv[y_name2] csv2 = pd.read_csv("../log/mnist_mapid_acc10.csv") y3 = csv2[y_name1] y4 = csv2[y_name2] plt.figure() plt.plot(x, y1, marker=".") plt.plot(x, y2, marker=".") plt.plot(x, y3, marker=".") plt.plot(x, y4, marker='.') plt.xlabel('number of maps') plt.ylabel('accuracy') plt.grid() plt.legend() plt.savefig('../eps/' + savename) plt.show()
def plot3d(data): fig = plt.figure() ax = fig.gca(projection='3d') Z, T1, T2, T3, X, Y = zip(*data) X = np.array(X) Y = np.array(Y) Z = np.array(Z) print X, Y, Z print "XYZ printed" print X.shape print Y.shape print Z.shape ax.plot_surface(X, Y, Z) #,rstride=8, cstride=8, alpha=0.3) #cset = ax.contour(X, Y, Z, zdir='z', offset=-100) #cset = ax.contour(X, Y, Z, zdir='x', offset=-40) #cset = ax.contour(X, Y, Z, zdir='y', offset=40) ax.set_xlabel('Latitude') #ax.set_xlim3d(-40, 40) ax.set_ylabel('Longitude') #ax.set_ylim3d(-40, 40) ax.set_zlabel('Time') #ax.set_zlim3d(-100, 100) plt.savefig("fig1.ps")
def cm_plot(original_label, predict_label, kunm, pic=None): cm = confusion_matrix(original_label, predict_label) print('kappa:', kappa(cm)) plt.figure() plt.matshow(cm, cmap=plt.cm.Blues) plt.colorbar() for x in range(len(cm)): for y in range(len(cm)): plt.annotate(cm[x, y], xy=(x, y), horizontalalignment='center', verticalalignment='center') plt.ylabel('True label') plt.xlabel('Predicted label') plt.title('confusion matrix') if pic is not None: plt.savefig(str(pic) + '.svg') # plt.xticks(('Wake','N1','N2','N3','REM')) # plt.yticks(('Wake','N1','N2','N3','REM')) plt.savefig(savepath + "cnnmatrix" + str(kunm) + ".svg") plt.show()
def plotFeatImportance(pathOut, imp, oob, oos, method, tag=0, simNum=0, **kargs): #plot mean imp bars with std import matplotlib as mpl mpl.figure(figsize=(10, imp.shape[0] / 5.)) imp = imp.sort_values('mean', ascending=True) ax = imp['mean'].plot(kind='barh', color='b', alpha=.25, xerr=imp['std'], error_kw={'ecolor': 'r'}) if method == 'MDI': mpl.xlim([0, imp.sum(axis=1).max()]) mpl.axvline(1. / imp.shape[0], linewidth=1, color='r', linestyle='dotted') ax.get_yaxis().set_visible(False) for i, j in zip(ax.patches, imp.index): ax.text(i.get_width() / 2, i.get_y() + i.get_height() / 2, j, ha='center', va='center', color='black') mpl.title = ('tag=' + tag + '|simNum=' + str(simNum) + '|oob=' + str(round(oob, 4)) + '|oos=' + str(round(oos, 4))) mpl.savefig(pathOut + 'featImportance_' + str(simNum) + '.png', dpi=100) mpl.clf() mpl.close() return
def plot_response_time_variousT(): dataframe = pd.DataFrame() index = [] for k in capacity_change_time: for mode in modes: index.append(f"{mode},t={k}s") dataframe['index'] = index for mode in modes: index = [] meanResponseTime = [] for i in capacity_change_time: df = scalar_df_parse( f"C:\\Users\\Leonardo Poggiani\\Documents\\GitHub\\PECSNproject\\csv\\pool_classico_varia_T\\{mode}{i}.csv" ) response = df[df.name == "responseTime"] meanResponseTime.append(response.value.mean()) index.append(i) plt.bar(index, meanResponseTime) plt.title(f"{mode}") plt.rcParams["figure.figsize"] = (12, 10) plt.xticks(rotation=25) plt.show() plt.xlabel("Value of t") plt.ylabel("Response time") plt.title("Comparison of various values of t") plt.legend(loc='best') plt.savefig( "C:\\Users\\Leonardo Poggiani\\Documents\\GitHub\\PECSNproject\\analysis\\variandoT\\responseTimeAlVariareDiT2.png" )
def plot_per_historgram(per_path:str, save_path:str=None): """ This function plots PER values as a histogram. The plot is saved to `save_path`. Args: per_path (str): path to per csv file with one column as the sample_id and the other the per value save_path (str): path to save the histrogram plot """ import csv import matplotlib.pyplot as plt import numpy as np with open(per_path, 'r') as fid: reader = csv.reader(fid, delimiter=',') per_list = [float(row[1]) for row in reader] plt.hist(per_list, bins=10, range=(0.0, 1.0)) plt.title("histogram of 2020-10-29 model PER values") plt.xlabel("PER bins") plt.ylabel("# of records") plt.xticks(np.arange(0, 1.1, step=0.1)) #plt.yticks(labels=per_list) if save_path == None: save_path = "PER_histogram.png" plt.savefig(save_path)
def loss_store2(self, x_train, x_gene): with open('./result/genefinalfig/x_train.pickle', 'wb') as fp: pickle.dump(x_train, fp) with open('./result/genefinalfig/generated.pickle', 'wb') as fp: pickle.dump(x_gene, fp) bins = 100 plt.hist(x_gene, bins, facecolor='red', alpha=0.5) plt.title('Histogram of distribution of generated data') plt.xlabel('Generated data value') plt.ylabel('Frequency') plt.savefig( './result/genefinalfig/WGAN-Generated-data-distribution.jpg') plt.close() with open('./result/lossfig/wdis.pickle', 'wb') as fp: pickle.dump(self.wdis_store, fp) t = arange(len(self.wdis_store)) plt.plot(t, self.wdis_store, 'r--') plt.xlabel('Iterations') plt.ylabel('Wasserstein distance') plt.savefig('./result/lossfig/WGAN-W-distance.jpg') plt.close() rv_pre, gv_pre, rv_pro, gv_pro = dwp(x_train, x_gene, self.testX, self.db) print 'Totally ' + str(len(rv_pre)) + ' of coordinates are left' with open('./result/genefinalfig/rv_pre.pickle', 'wb') as fp: pickle.dump(rv_pre, fp) with open('./result/genefinalfig/gv_pre.pickle', 'wb') as fp: pickle.dump(gv_pre, fp) with open('./result/genefinalfig/rv_pro.pickle', 'wb') as fp: pickle.dump(rv_pro, fp) with open('./result/genefinalfig/gv_pro.pickle', 'wb') as fp: pickle.dump(gv_pro, fp) rv_pre, gv_pre, rv_pro, gv_pro = fig_add_noise(rv_pre), fig_add_noise( gv_pre), fig_add_noise(rv_pro), fig_add_noise(gv_pro) plt.scatter(rv_pre, gv_pre) plt.title('Dimension-wise prediction, lr') plt.xlabel('Real data') plt.ylabel('Generated data') plt.savefig('./result/genefinalfig/WGAN-dim-wise-prediction.jpg') plt.close() plt.scatter(rv_pro, gv_pro) plt.title('Dimension-wise probability, lr') plt.xlabel('Real data') plt.ylabel('Generated data') plt.savefig('./result/genefinalfig/WGAN-dim-wise-probability.jpg') plt.close()
def img_gen(img, zca=False, rotation=0., w_shift=0., h_shift=0., shear=0., zoom=0., h_flip=False, v_flip=False, preprocess_fcn=None, batch_size=9): """ define function to generate images """ datagen = ImageDataGenerator(zca_whitening=zca, rotation_range=rotation, width_shift_range=w_shift, height_shift_range=h_shift, shear_range=shear, zoom_range=zoom, fill_mode='nearest', horizontal_flip=h_flip, vertical_flip=v_flip, preprocessing_function=preprocess_fcn, data_format=K.image_data_format()) datagen.fit(img) i = 0 for img_batch in datagen.flow(img, batch_size=9, shuffle=False): for img in img_batch: plt.subplot(330 + 1 + i) plt.imshow(img) i += 1 if i >= batch_size: break # plt.show() plt.savefig('img/trans/cats.png')
def show_hough_line(img, accumulator, thetas, rhos, save_path=None): ''' set up show image with matplotlib ''' fig, ax = plt.pyplot.subplots(1, 2, figsize=(10, 10)) ax[0].imshow(img, cmap=plt.cm.gray) ax[0].set_title('Input image') ax[0].axis('image') ax[1].imshow( accumulator, cmap='jet', extent=[np.rad2deg(thetas[-1]), np.rad2deg(thetas[0]), rhos[-1], rhos[0]]) ax[1].set_aspect('equal', adjustable='box') ax[1].set_title('Hough transform') ax[1].set_xlabel('Angles') ax[1].set_ylabel('Distance') ax[1].axis('image') # plt.axis('off') if save_path is not None: plt.savefig(save_path, bbox_inches='tight') plt.show()
def show_image(img_ndarray, id): ''' Visualize images resulted from calling vis_smpl_params in Jupyternotebook :param img_ndarray: Nx400x400x3 ''' import matplotlib as plt import os import numpy as np import cv2 fig = plt.figure(figsize=(4, 4), dpi=300) ax = fig.gca() img = img_ndarray.astype(np.uint8) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) ax.imshow(img) plt.axis('off') if not os.path.isdir("images"): os.makedirs("images") fig_name = "images/fig" + str(id) + ".png" plt.savefig(fig_name) plt.close()
def draw_bar(savename): import matplotlib.pyplot as plt size = 3 x = np.arange(size) a = [34.89, 33.87, 72.37] b = [551.72, 552.87, 698.34] xl = [ 'training time \n on MNIST', 'training time on \n Fashion-MNIST', 'training time \n on CIFAR-10' ] total_width, n = 0.54, 2 width = total_width / n x = x - (total_width - width) plt.figure() plt.bar(x, a, width=width) plt.bar(x + width, b, width=width) plt.ylabel('time(s)') plt.xticks( x, xl, ) plt.legend() plt.savefig('../eps/' + savename) plt.show()
def plotdatatree(treeID, scale1, mass1): plot_title="Mass Accretion History Tree " + str(treeID) #Can code the number in with treemax x_axis="scale time" y_axis="total mass" figure_name=os.path.expanduser('~/figureTree' + str(treeID)) #Choose which type of plot you would like: Commented out. plt.plot(scale1, mass1, linestyle="-", marker="o") #plt.scatter(scale1, mass1, label="first tree") plt.title(plot_title) plt.xlabel(x_axis) plt.ylabel(y_axis) #plt.yscale("log") plt.savefig(figure_name) #In order to Plot only a single tree on a plot must clear lists before loop. #Comment out to over plot curves. plt.clf() clearmass = [] clearscale = [] return clearmass, clearscale
contour_font = 10 fig = plt.figure(figsize=(6, 5)) ax_ray = fig.add_subplot(111) dist = [20, 60, 85, 95] dist = [8, 27, 48, 67, 89, 99] dist = [30, 60, 80, 95] for i in range(len(dist)): s = str(str(dist[i]) + "_SURF96.out") data = np.loadtxt(s, usecols=(5, 6, 7)) ax_ray.plot(data[:, 0], data[:, 1], label=str(dist[i] * 10) + "km") ax_ray.grid(True, linestyle='--', color='black', linewidth=0.15) plt.xticks(fontsize=axes_label_font_size) plt.yticks(fontsize=axes_label_font_size) plt.xlabel('Time period ($s$)', fontsize=axes_label_font_size, fontweight='bold') plt.ylabel('Phase velocity ($km/s$)', fontsize=axes_label_font_size, fontweight='bold') plt.legend(fancybox=True, shadow=True, framealpha=0.5, loc='lower right', fontsize=legend_font_size) plt.savefig('Rayleigh_Phase_dispersion.jpg', dpi=400) plt.show()
exBestVersions = [] exNomaxfuse = [] orthBestVersions = [] orthNomaxfuse = [] N = 6 #number of matrix orders we consider: 50, 100, 250, 500, 1000, 2000 ind = np.arrange(N) width = .20 #bar width fig = pl.figure() ax = fig.add_subplot(111) #add the bars to the figure along the axis #define the bars in the bar graph r1 = ax.bar(ind, gaNomaxfuse, width, color = 'b') r2 = ax.bar(ind+width, gaBestVersions, width, color = 'y') r3 = ax.bar(ind+(2*width), exNomaxfuse, width, color = 'b') r4 = ax.bar(ind+(3*width), exBestVersions, width, color = 'y') r5 = ax.bar(ind+(4*width), orthNomaxfuse, width, color = 'b') r6 = ax.bar(ind+(5*width), orthBestVersions, width, color = 'y') ax.set_ylabel('Time (sec)') ax.set_xlabel('Nomaxfuse and best version by search strategy between n = 50 and n = 2000') ax.set_title('Timing by search strategy and matrix size') ax.set_xticks(ind+width) ax.set_xticklabels( ('50', '100', '250', '500', '1000', '2000') ) ax.legend() plt.savefig('searchstratsresults.pdf')
import pandas as pd import matplotlib as plt permutation3 = pd.read_csv('Results/Permutation_3.csv') permutation4 = pd.read_csv('Results/Permutation_4.csv') permutation5 = pd.read_csv('Results/Permutation_5.csv') permutation3[' Median set size'].hist() plt.savefig('dist_permutation_3.pdf') #do the rest
axes[0,0].set_xlabel('u-g') axes[0,0].set_xlabel('g-r') axes[0,1].plot(g-r,r-i,marker='o',linestyle='') axes[0,1].set_xlabel('g-r') axes[0,1].set_xlabel('r-i') axes[1,0].plot(r-i,i-z,marker='o',linestyle='') axes[1,0].set_xlabel('r-i') axes[1,0].set_xlabel('i-z') axes[1,1].plot(u-g,r-i,marker='o',linestyle='') axes[1,1].set_xlabel('u-g') axes[1,1].set_xlabel('r-i') plt.savefig('scatter_sdss_photometry.png') #Here are the contour plots #Step 1: use np.histogram2d to create "density" plots of the points H1, xedges1, yedges1 = np.histogram2d((u-g), (g-r)) H2, xedges2, yedges2 = np.histogram2d((g-r),(r-i)) H3, xedges3, yedges3 = np.histogram2d((r-i),(i-z)) H4, xedges4, yedges4 = np.histogram2d((u-g),(r-i)) fig,axes = plt.subplots(2,2) extent1 = [yedges1[0], yedges1[-1], xedges1[0], xedges1[-1]] #This draws contours around the density cset = axes[0,0].contour(H1,colors=['black','green','blue','red'], linewidths=(1.9, 1.6, 1.5, 1.4),extent=extent1) #Label the contours!
## sc = SparkContext( appName="Wikipedia Count" ) lines = sc.textFile(infile) <<<<<<< HEAD counts = lines.map(lambda line:line.split('\t')[2]).map(lambda x:(x[0:7],1)).reduceByKey(add) counts_by_month = counts.sortBy(lambda x: x[0]) length = counts_by_month.count() x = range(0, length) lables = counts_by_month.map(lambda x : x[0]).collect() y = counts_by_month.map(lambda x : x[1]).collect() plt.plot(np.array(x),np.array(y)) plt.xticks(x,lables,rotation = 'vertical') plt.savefig("counts_by_month.pdf") counts_by_month.collect() ======= counts = lines.map() >>>>>>> 65a15a888eef7c1f146383d61b9f57882504af41 ## YOUR CODE GOES HERE ## PUT YOUR RESULTS IN counts """with open("wikipedia_by_month.txt","w") as fout: for (date, count) in counts_by_month: fout.write("{}\t{}\n".format(date,count))""" ##
import sys import numpy as np import matplotlib as plt import math a=sys.argv[1] data=np.loadtxt(a) x=data[:,3] y=data[:,4] figura=plt.polyfit(x,y,1) plt.scatter(x,y) plt.plot(x,y*figura[0]*(y**4) + figura[i]*(y**3), figura[2]*(y**2), figura[3]*y + fit[4]) plt.xlabel("Pasos (x)", frontsize=20) plt.ylabel("Distancia (y)", frontsize =20) plt.tittle("Numero de Pasos VS Distancia con la Regresion", frontsize =15) plt.savefig('ajuste.png')
from mpl_toolkits.mplot3d import axes3d import matplotlib as plt plt.use('PS') from pylab import * fig = plt.figure() ax = fig.gca(projection='3d') X, Y, Z = axes3d.get_test_data(0.05) print "X - data" print X print "Y - data" print Y print "Z - data" print Z ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3) cset = ax.contour(X, Y, Z, zdir='z', offset=-100) cset = ax.contour(X, Y, Z, zdir='x', offset=-40) cset = ax.contour(X, Y, Z, zdir='y', offset=40) ax.set_xlabel('X') ax.set_xlim3d(-40, 40) ax.set_ylabel('Y') ax.set_ylim3d(-40, 40) ax.set_zlabel('Z') ax.set_zlim3d(-100, 100) plt.savefig("test.ps")
def train_dcgan_labeled(gen, dis, epoch0=0): print('CHAINER begin training'); sys.stdout.flush() o_gen = optimizers.Adam(alpha=0.0002, beta1=0.5) o_dis = optimizers.Adam(alpha=0.0002, beta1=0.5) print('CHAINER begin gen'); sys.stdout.flush() o_gen.setup(gen) o_dis.setup(dis) print('CHAINER begin add'); sys.stdout.flush() o_gen.add_hook(chainer.optimizer.WeightDecay(0.00001)) o_dis.add_hook(chainer.optimizer.WeightDecay(0.00001)) print('CHAINER begin zvis'); sys.stdout.flush() # zvis = (xp.random.uniform(-1, 1, (100, nz), dtype=np.float32)) print('CHAINER begin for'); sys.stdout.flush() for epoch in range(epoch0,n_epoch): print("epoch:",epoch) sys.stdout.flush() perm = np.random.permutation(n_train) sum_l_dis = np.float32(0) sum_l_gen = np.float32(0) for i in range(0, n_train, batchsize): # discriminator # 0: from dataset # 1: from noise #print "load image start ", i x2 = np.zeros((batchsize, 3, 96, 96), dtype=np.float32) for j in range(batchsize): #try: rnd = np.random.randint(len(dataset)) rnd2 = np.random.randint(2) img = np.asarray(Image.open(io.BytesIO(dataset[rnd])).convert('RGB')).astype(np.float32).transpose(2, 0, 1) x2[j,:,:,:] = (img[:,0:96,0:96]-128.0)/128.0 #except: # print('read image error occured', fs[rnd]) #print "load image done" # train generator z = Variable(xp.random.uniform(-1, 1, (batchsize, nz), dtype=np.float32)) x = gen(z) yl = dis(x) L_gen = F.softmax_cross_entropy(yl, Variable(xp.zeros(batchsize, dtype=np.int32))) L_dis = F.softmax_cross_entropy(yl, Variable(xp.ones(batchsize, dtype=np.int32))) # train discriminator x2 = Variable(cuda.to_gpu(x2)) yl2 = dis(x2) L_dis += F.softmax_cross_entropy(yl2, Variable(xp.zeros(batchsize, dtype=np.int32))) #print "forward done" o_gen.zero_grads() L_gen.backward() o_gen.update() o_dis.zero_grads() L_dis.backward() o_dis.update() sum_l_gen += L_gen.data.get() sum_l_dis += L_dis.data.get() #print "backward done" if i%image_save_interval==0: pylab.rcParams['figure.figsize'] = (16.0,16.0) pylab.clf() vissize = 100 z = zvis z[50:,:] = (xp.random.uniform(-1, 1, (50, nz), dtype=np.float32)) z = Variable(z) x = gen(z, test=True) x = x.data.get() for i_ in range(100): tmp = ((np.vectorize(clip_img)(x[i_,:,:,:])+1)/2).transpose(1,2,0) pylab.subplot(10,10,i_+1) pylab.imshow(tmp) pylab.axis('off') pylab.savefig('%s/vis_%d_%d.png'%(out_image_dir, epoch,i)) serializers.save_hdf5("%s/dcgan_model_dis_%d.h5"%(out_model_dir, epoch),dis) serializers.save_hdf5("%s/dcgan_model_gen_%d.h5"%(out_model_dir, epoch),gen) serializers.save_hdf5("%s/dcgan_state_dis_%d.h5"%(out_model_dir, epoch),o_dis) serializers.save_hdf5("%s/dcgan_state_gen_%d.h5"%(out_model_dir, epoch),o_gen) print('epoch end', epoch, sum_l_gen/n_train, sum_l_dis/n_train)
def main(args): files=args.cgfiles #Uncomment the following line to display the files in a random order. #random.shuffle(files) #Prepare the pyplot figure totalFigures=len(files) figuresPerLine=int(math.ceil(math.sqrt(totalFigures))) fig, ax=plt.subplots(int(math.ceil(totalFigures/figuresPerLine)),figuresPerLine, squeeze=False, figsize=(8,8)) #Background color of figure (not plot) if args.style=="WOB": fig.patch.set_facecolor('black') #Plot one projection per file. for i, file_ in enumerate(files): #get the subplot axes (Note: axes != axis in matplotlib) current_axes=ax[i//figuresPerLine, i%figuresPerLine] #Parse the file cg=ftmc.CoarseGrainRNA(file_) # Random projection direction, if no direction present in the file if args.proj_direction: direction=list(map(float, args.proj_direction.split(","))) elif cg.project_from is not None: direction=cg.project_from else: direction=ftuv.get_random_vector() #Generate the projection object proj=ftmp.Projection2D(cg, direction, rotation=180, project_virtual_atoms=args.virtual_atoms) #Simulate a reduced resolution of the image. if args.condense: proj.condense(args.condense) target_elems=[] if args.show_distances: try: num_elems=int(args.show_distances) except: target_elems=args.show_distances.split(",") else: if num_elems>len(proj._coords.keys()): raise ValueError("--show-distances must not be greater {} for the current projection ({}:'{}')".format(len(proj._coords.keys()), i, file_)) elems=list(proj._coords.keys()) random.shuffle(elems) while len(target_elems)<num_elems: r=random.random() if r<0.4: hairpins=[ x for x in elems if x[0]=="h" and x not in target_elems ] if hairpins: target_elems.append(hairpins[0]) continue if r<0.6: multiloops=[ x for x in elems if x[0]=="m" and x not in target_elems ] if multiloops: target_elems.append(multiloops[0]) continue others=[ x for x in elems if x not in target_elems] target_elems.append(others[0]) comb=list(it.combinations(target_elems, 2)) #print(comb, target_elems) line2dproperties={} if args.style=="BOW": line2dproperties["color"]="black" elif args.style=="WOB": line2dproperties["color"]="white" #Plot the projection # proj.plot(current_axes, margin=15, linewidth=3, add_labels=set(target_elems), line2dproperties=line2dproperties, show_distances=comb, print_distances=args.print_distances) #Uncomment to set a substring of the filename as a title #current_axes.set_title(file[-15:]) #Hide the x- and y axis. current_axes.get_xaxis().set_visible(False) current_axes.get_yaxis().set_visible(False) #Print the projection direction and the filename in the plot. if args.show_direction or args.p: current_axes.text(0.01,0.01,"Projection direction: ({},{},{})".format(round(direction[0],3), round(direction[1],3), round(direction[2],3)), transform=current_axes.transAxes) if args.show_filename or args.p: current_axes.text(0.01,0.99,"File: {}".format(file_), transform=current_axes.transAxes, verticalalignment='top',) #Change the backgroundcolor of the plot area. if args.style=="WOB": current_axes.set_axis_bgcolor('black') #Hide additional subplots with no projection on them. for i in range(len(files),int(math.ceil(totalFigures/figuresPerLine))*figuresPerLine): ax[i//figuresPerLine, i%figuresPerLine].axis('off') # Reduce the space outside of the plots and between the subplots. plt.subplots_adjust(left=0.025, right=0.975, bottom=0.025, top=0.975, wspace=0.05, hspace=0.05) if args.out: for ofname in args.out: if args.out_path: ofname=os.path.join(args.out_path, ofname) ofname=os.path.expanduser(ofname) matplotlib.savefig(ofname,format=ofname[-3:]) if not args.out or args.show: #Show the plot and clear it from the internal memory of matplotlib. plt.show()
ax4 = subplot(325) ax5 = subplot(326) ind = 4 ax5.set_xticklabels(xlabels) ax5.get_yaxis().set_ticks([]) plot(gfv[kmeans.labels_==ind].T,lw=0.5,color='grey') plot(kmeans.cluster_centers_.T[:,ind],lw=2,color='#4B088A') ax5 = subplot(326) text(-14.5, 11, 'Total Number of Employees\n k = 5', size=16) # save plot to pdf: from matplotlib.backends.backend_pdf import PdfPages pp = PdfPages('employees.pdf') plt.savefig(pp, format='pdf') pp.close() # create geodataframe of different classes: #gdf = gp.GeoDataFrame.from_file('../output/nyc-zip-code-extend.json') gdf = gp.GeoDataFrame.from_file('../data/nyc_zipcta/nyc_zipcta.shp') # dictionary of cluster labels for each zip code labels = {} for i in range(0, len(zcta)): labels[zcta[i]] = kmeans.labels_[i] # add "clusterLabels" to existing gdf labelList = [] zipList = [] for i in range(0, len(gdf)):
def show_overfitting(request): dic = {} # 实验一:对比不同模型的cross-validation结果 # 用load_wine方法导入数据 wine_data = datasets.load_wine() # print(wine_data.feature_names) data_input = wine_data.data data_output = wine_data.target rf_class = RandomForestClassifier() lr_class = LogisticRegression() svm_class = svm.LinearSVC() # print(cross_val_score(rf_class, data_input, data_output, scoring='accuracy', cv=4)) # 1.使用随机森林方法观察准确率 accuracy_rf = cross_val_score( rf_class, data_input, data_output, scoring='accuracy', cv=10).mean() * 100 print('Accuracy of Random Forest is:', accuracy_rf) # 2.使用支持向量机方法观察准确率 accuracy_svm = cross_val_score( svm_class, data_input, data_output, scoring='accuracy', cv=10).mean() * 100 print('Accuracy of SVM is:', accuracy_svm) # 3.使用逻辑回归方法观察准确率 accuracy_lr = cross_val_score( lr_class, data_input, data_output, scoring='accuracy', cv=10).mean() * 100 print('Accuracy of LogisticRegression is:', accuracy_lr) rcParams['figure.figsize'] = 12, 10 x = np.array([1.4 * i * np.pi / 180 for i in range(0, 300, 4)]) np.random.seed(20) # 固定每次生成的随机数 y = np.sin(x) + np.random.normal(0, 0.2, len(x)) data = pd.DataFrame(np.column_stack([x, y]), columns=['x', 'y']) plt.plot(data['x'], data['y'], '.') file = "static/img/han01.png" dic["pic1"] = "/" + file plt.savefig(file) # plt.show() for i in range(2, 16): # power of 1 is already there colname = 'x_%d' % i # new var will be x_power data[colname] = data['x']**i # print(data.head()) def linear_regression(data, power, models_to_plot): # initialize predictors: predictors = ['x'] if power >= 2: predictors.extend(['x_%d' % i for i in range(2, power + 1)]) # Fit the model linreg = LinearRegression(normalize=True) linreg.fit(data[predictors], data['y']) y_pred = linreg.predict(data[predictors]) # Check if a plot is to be made for the entered power if power in models_to_plot: plt.subplot(models_to_plot[power]) plt.tight_layout() plt.plot(data['x'], y_pred) plt.plot(data['x'], data['y'], '.') plt.title('Plot for power: %d' % power) # Return the result in pre-defined_format rss = sum((y_pred - data['y'])**2) ret = [rss] ret.extend([linreg.intercept_]) ret.extend(linreg.coef_) return ret col = ['rss', 'intercept'] + ['coef_x_%d' % i for i in range(1, 16)] ind = ['model_pow_%d' % i for i in range(1, 16)] coef_matrix_simple = pd.DataFrame(index=ind, columns=col) # 注意上行代码的columns不能携程column单数,画图就无法画出来了 # 定义作图的位置与模型的复杂度 models_to_plot = {1: 231, 3: 232, 6: 233, 8: 234, 11: 235, 14: 236} # 画出来 for i in range(1, 16): coef_matrix_simple.iloc[i - 1, 0:i + 2] = linear_regression( data, power=i, models_to_plot=models_to_plot) file = "static/img/han02.png" dic["pic2"] = "/" + file plt.savefig(file) # plt.show() # 定义作图的位置与模型的复杂度 models_to_plot = { 1e-15: 231, 1e-10: 232, 1e-4: 233, 1e-3: 234, 1e-2: 235, 5: 236 } def ridge_regression(data, predictors, alpha, models_to_plot={}): # Fit the model:1.初始化模型配置 2.模型拟合 3.模型预测 ridgereg = Ridge(alpha=alpha, normalize=True) ridgereg.fit(data[predictors], data['y']) # predictors的内容实际是data(定义的一种DataFrame数据结构)的某列名称 y_pred = ridgereg.predict(data[predictors]) # Check if a plot is to be made for the entered alpha if alpha in models_to_plot: plt.subplot(models_to_plot[alpha]) plt.tight_layout() plt.plot(data['x'], y_pred) # 画出拟合曲线图 plt.plot(data['x'], data['y'], '.') # 画出样本的散点图 plt.title('Plot for alpha: %.3g' % alpha) # Return the result in pre-defined format rss = sum((y_pred - data['y'])**2) ret = [rss] ret.extend([ridgereg.intercept_]) ret.extend(ridgereg.coef_) return ret predictors = ['x'] predictors.extend(['x_%d' % i for i in range(2, 16)]) # Set the different values of alpha to be tested alpha_ridge = [1e-15, 1e-10, 1e-8, 1e-4, 1e-3, 1e-2, 1, 5, 10, 20] # Initialize the dataframe for storing coeficients col = ['rss', 'intercept'] + ['coef_x_%d' % i for i in range(1, 16)] ind = ['alpha_%.2g' % alpha_ridge[i] for i in range(0, 10)] coef_matrix_ridge = pd.DataFrame(index=ind, columns=col) models_to_plot = { 1e-15: 231, 1e-10: 232, 1e-4: 233, 1e-3: 234, 1e-2: 235, 5: 236 } for i in range(10): coef_matrix_ridge.iloc[i, ] = ridge_regression(data, predictors, alpha_ridge[i], models_to_plot) file = "static/img/han03.png" dic["pic3"] = "/" + file plt.savefig(file) # plt.show() def lasso_regression(data, predictors, alpha, models_to_plot={}): # Fit the model lassoreg = Lasso(alpha=alpha, normalize=True, max_iter=1e5) lassoreg.fit(data[predictors], data['y']) y_pred = lassoreg.predict(data[predictors]) # Check if a plot is to be made for the entered alpha if alpha in models_to_plot: plt.subplot(models_to_plot[alpha]) plt.tight_layout() plt.plot(data['x'], y_pred) plt.plot(data['x'], data['y'], '.') plt.title('Plot for alpha:%.3g' % alpha) # Return the result in pre-defined format rss = sum((y_pred - data['y'])**2) ret = [rss] ret.extend([lassoreg.intercept_]) ret.extend(lassoreg.coef_) return ret predictors = ['x'] predictors.extend(['x_%d' % i for i in range(2, 16)]) # Define the alpha values to test alpha_lasso = [1e-15, 1e-10, 1e-8, 1e-5, 1e-4, 1e-3, 1e-2, 1, 5, 10] # Initialize the dataframe to store coefficients col = ['rss', 'intercept'] + ['coef_x_%d' % i for i in range(1, 16)] ind = ['alpha_%.2g' % alpha_lasso[i] for i in range(0, 10)] coef_matrix_lasso = pd.DataFrame(index=ind, columns=col) # Define the models_to_plot models_to_plot = { 1e-10: 231, 1e-5: 232, 1e-4: 233, 1e-3: 234, 1e-2: 235, 1: 236 } # Iterate over the 10 alpha values: for i in range(10): coef_matrix_lasso.iloc[i, ] = lasso_regression(data, predictors, alpha_lasso[i], models_to_plot) file = "static/img/han04.png" dic["pic4"] = "/" + file plt.savefig(file) # plt.show() return render(request, "overfitting.html", dic)
print t print v initialize(v, s, t, dt, n) calculate(v, s, t, dt, n) store(v, t, n) #plot plt.figure(1) plt.subplot(211) plt.plot(t, v,"g-", linewidth=2.0) plt.scatter(t, v) plt.title('The Velocity of a Free Falling Object') plt.xlabel('Time($t$)', fontsize=14) plt.ylabel('Velocity($m/s$)', fontsize=14) plt.text(3,-60,r'$g = 9.8 m/s^2$', fontsize=16) plt.grid(True) plt.subplot(212) plt.plot(t, s,"g-", linewidth=2.0) plt.scatter(t, s) plt.title('The Displacement of a Free Falling Object') plt.xlabel('Time($t$)', fontsize=14) plt.ylabel('Displacement($m$)', fontsize=14) plt.text(3,-300,r'$g = 9.8 m/s^2$', fontsize=16) plt.grid(True) plt.show() plt.savefig("ex1.jpg") read()
def plot_trace(x_l, n_l, file_path): figure() plot(n_l,x_l) plt.savefig(file_path)
def plot_hist(x_l, file_path): figure() n, bins, patches = hist(x_l, 20, histtype='bar') plt.savefig(file_path)
counts = lines.map(lambda date: date.split('\t')[2]) \ .map(lambda month:month[0:7]) \ .map(lambda x: (x,1)) \ .reduceByKey(add) month_sorted = counts.sortBy(lambda x: x[0]).collect() ## YOUR CODE GOES HERE ## PUT YOUR RESULTS IN counts with open("wikipedia_by_month.txt","w") as fout: for (date, count) in month_sorted: fout.write("{}\t{}\n".format(date,count)) ## ## Terminate the Spark job ## ############# # plot ############# data = pandas.read_csv("wikipedia_by_month.txt", sep='\t', header = None, names=['date', 'count']) fig = pyplot.figure() index = np.arange(len(data[0])) pyplot.bar(index, data['count'], width = width) fig.autofmt_xdate() plt.xticks(index, data['date'], rotation = 'vertical') plt.savefig("figure.pdf") sc.stop()
t = pickle.load(pickle_file) v = pickle.load(pickle_file) initialize(v, s, t, dt, n) calculate(v, s, t, dt, n) store(v, t, n) #plot plt.figure(1) plt.subplot(211) plt.plot(t, v,"g-", linewidth=1.0) plt.scatter(t, v) plt.title('The Velocity of a Free Falling Object') #plt.xlabel('Time($t$)', fontsize=12) (cancel because words overlap) plt.ylabel('Velocity($m/s$)', fontsize=12) plt.text(3,0,r'$g = 9.8 m/s^2$', fontsize=16) plt.grid(True) plt.subplot(212) plt.plot(t, s,"g-", linewidth=1.0) plt.scatter(t, s) plt.title('The Displacement of a Free Falling Object') plt.xlabel('Time($t$)', fontsize=12) plt.ylabel('Displacement($m$)', fontsize=12) plt.text(3,0,r'$g = 9.8 m/s^2$', fontsize=16) plt.grid(True) plt.show() plt.savefig("ex4.jpg") read()