def main(): # prepare data trainingSet = [] testSet = [] split = 0.80 # path = r'C:/Users/ASUS/PycharmProjects/knntest/iris' loadDataset("C:\\Users\\ASUS\\Desktop\\mnist_test_1.csv", split, trainingSet, testSet) print('Train set: ' + repr(len(trainingSet))) print('Test set: ' + repr(len(testSet))) # generate predictions predictions = [] k = 5 xvalues = [] pred = [] actual = [] for x in range(len(testSet)): neighbors = getNeighbors(trainingSet, testSet[x], k) # print(neighbors) result = getResponse(neighbors) predictions.append(result) print('> predicted=' + repr(result) + ', actual=' + repr(testSet[x][-1])) xvalues.append(x) pred.append(result) actual.append(testSet[x][-1]) #plt.plot(x,repr(testSet[x][-1]),color="chocolate",label="Actual Values") accuracy = getAccuracy(testSet, predictions) plt.plot(xvalues, pred, ".", color="r", label="Predicted Values") plt.plot(xvalues, actual, ".", color="b", label="Actual Values") plt.show() print('Accuracy: ' + repr(accuracy) + '%')
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 compare_MC_exact(): max_error = [] sum_error = [] walkers = [] for i in range(500,20000,500): d1 = Diffusion(t=0.2) t_exact,u_exact = d1.exact() t_uniform , mc_uniform = d1.MC_uniform(i) print len(u_exact) print len(mc_uniform) diff = u_exact[:] - mc_uniform[:] temp = max(abs(diff)) max_error.append(temp) temp = sum(diff) sum_error.append(temp) temp = i print temp walkers.append(temp) from matplotlib.pylab import plt plt.figure(2) plt.plot(walkers,max_error,'o-') plt.xlabel('Number of walkers') plt.ylabel('Maximum error') plt.savefig('mcuniform_error1.eps') plt.figure(3) plt.plot(walkers,sum_error,'o-') plt.xlabel('Number of walkers') plt.ylabel('Accumulated error') plt.savefig('mcuniform_error2.eps')
def display(self, current=None, target=None): """ Plots the state of the task. If <show> = False, doesn't plot anything and the simulation can run faster. """ if not self.show: return # Scatter plot of points, color coded by class pts = self.points size = 35 for cluster, color in cluster_colors.iteritems(): class_points = [x for x in pts if x.cluster == cluster] plt.scatter([p.x for p in class_points], [p.y for p in class_points], c=color, s=size) # Draw the connections if self.connectivity_matrix is not None: for start_ix, connections in enumerate(self.connectivity_matrix): for connect_ix, connected in enumerate(connections): if connected and connect_ix != start_ix: plt.plot(*zip((pts[start_ix].x, pts[start_ix].y), (pts[connect_ix].x, pts[connect_ix].y)), c='k', linewidth=0.5) # Show where the message is going and where it currently is if current and target: plt.scatter(pts[current].x, pts[current].y, c='m', s=150) plt.scatter(pts[target].x, pts[target].y, c='y', s=190) plt.show()
def calibration_plot(prob, ytest): # stolen from stackoverflow! outcome = ytest data = pd.DataFrame(dict(prob=prob, outcome=outcome)) #group outcomes into bins of similar probability bins = np.linspace(0, 1, 20) cuts = pd.cut(prob, bins) binwidth = bins[1] - bins[0] #freshness ratio and number of examples in each bin cal = data.groupby(cuts).outcome.agg(['mean', 'count']) cal['pmid'] = (bins[:-1] + bins[1:]) / 2 cal['sig'] = np.sqrt(cal.pmid * (1 - cal.pmid) / cal['count']) #the calibration plot ax = plt.subplot2grid((3, 1), (0, 0), rowspan=2) p = plt.errorbar(cal.pmid, cal['mean'], cal['sig']) plt.plot(cal.pmid, cal.pmid, linestyle='--', lw=1, color='k') plt.ylabel("Empirical P(Product)") #the distribution of P(fresh) ax = plt.subplot2grid((3, 1), (2, 0), sharex=ax) plt.bar(left=cal.pmid - binwidth / 2, height=cal['count'], width=.95 * (bins[1] - bins[0]), fc=p[0].get_color()) plt.xlabel("Predicted P(Product)") plt.ylabel("Number")
def display(self, current=None, target=None): """ Plots the state of the task. If <show> = False, doesn't plot anything and the simulation can run faster. """ if not self.show: return # Scatter plot of points, color coded by class pts = self.points size = 35 for cluster, color in cluster_colors.iteritems(): class_points = [x for x in pts if x.cluster == cluster] plt.scatter([p.x for p in class_points], [p.y for p in class_points], c=color, s=size) # Draw the connections if self.connectivity_matrix is not None: for start_ix, connections in enumerate(self.connectivity_matrix): for connect_ix, connected in enumerate(connections): if connected and connect_ix != start_ix: plt.plot(*zip( (pts[start_ix].x, pts[start_ix].y), (pts[connect_ix].x, pts[connect_ix].y)), c='k', linewidth=0.5) # Show where the message is going and where it currently is if current and target: plt.scatter(pts[current].x, pts[current].y, c='m', s=150) plt.scatter(pts[target].x, pts[target].y, c='y', s=190) plt.show()
def exact(): W = Lea.fastMax(W0 + U, 0) for k in range(1, 21): if k % 5 == 0: plt.plot(W.support(), W.pmf(), label="k={}".format(k)) W = Lea.fastMax(W + U, 0) return W.support(), W.pmf()
def MRI(): T1 = 800*10**-3 T2 = 100*10**-3 a = pi/2 t = np.linspace(0, 1, 100) # Initalize and Rotate M = np.array([0,0,1]) R = np.array([[1, 0, 0], [0, cos(a), sin(a)], [0, -sin(a), cos(a)]]) M = R.dot(M) # T1/T2 Relaxationq M_z = 1 + (M[2] - 1) * np.exp(-t/T1) M_x = M[0]*np.exp(-t/T2) M_y = M[1]*np.exp(-t/T2) M_xy = np.sqrt(M_x*M_x + M_y*M_y) plt.subplot(121) plt.plot(t, M_xy) plt.subplot(122) plt.plot(t, M_z) plt.show()
def AddRegressor(): rng = np.random.RandomState(1) # 和random_state的用法一致 在这可以固定生成的随机数 x = np.sort(5 * rng.rand(80, 1), axis=0) # rng.rand(80,1) 生成80行1列的随机数 乘以5就是生成0-5的随机数 y = np.sin(x).ravel() # ravel()降维 y[::5] += 0.3 * (0.5 - rng.rand(16)) # plt.show() reg1 = tree.DecisionTreeRegressor(max_depth=2) reg2 = tree.DecisionTreeRegressor(max_depth=5) reg1.fit(x, y) reg2.fit(x, y) test = np.arange(0.0, 5.0, 0.01)[:, np.newaxis] y1 = reg1.predict(test) y2 = reg2.predict(test) #plt.figure() plt.figure() plt.scatter(x, y, label='dian') plt.plot(test, y1, color='red', label='max_depth=2') plt.plot(test, y2, color='yellow', label="max_depth=5") plt.xlabel('data') plt.ylabel('target') plt.legend(loc='upper right') plt.show()
def exact(): W = lea.max_of(W0 + U, 0, fast=True) for k in range(1, 21): if k % 5 == 0: plt.plot(W.support, W.ps, label="k={}".format(k)) W = lea.max_of(W + U, 0, fast=True) return W.support, W.ps
def plot_accuracy(self, train_acc, test_acc): n = len(train_acc) plt.plot(range(n), train_acc, label="train") plt.plot(range(n), test_acc, label="test") plt.legend(loc='lower right') plt.title("Overfit") plt.ylabel('accuracy') plt.xlabel('epoch') plt.show()
def Test2(): t = np.linspace(0,10,100) x = np.sin(t) plt.plot(t, x) plt.title('sin(x)') plt.xlabel('time (s)') plt.ylabel('magnitude') plt.show()
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 plotting(self, pressurelattice, invasionlattice, pressure, number_of_clusters): from matplotlib.pylab import plt #Plotting the invasionlattice plt.figure(2) plt.imshow(invasionlattice, cmap='Greys', interpolation='nearest') plt.title("Invasion lattice") plt.colorbar() plt.savefig(self.pathsimulation + "invasionlattice.png", bbox_inches="tight") plt.close() #plotting the pressure plt.figure(5) plt.plot(pressure) plt.xlabel('Time') plt.ylabel('Pressure') plt.title('P(t)') plt.savefig(self.pathsimulation + "pressure.png", bbox_inches="tight") plt.close() #plotting the clusterlattice plt.figure(6) plt.imshow(self.lw, interpolation='nearest') plt.title("Clusterlattice") plt.colorbar() plt.savefig(self.pathsimulation + "clusterlattice.png", bbox_inches="tight") plt.close() #Temporal diagram plt.figure(7) plt.imshow(self.temporalplot, interpolation='nearest') plt.title('Temporal diagram') plt.colorbar() plt.savefig(self.pathsimulation + "temporal.png", bbox_inches="tight") plt.close() #Plotting pressure distribution in the cell. plt.figure(3) plt.hist(pressurelattice.ravel(), bins=30, fc='k', ec='k') plt.savefig(self.pathsimulation + "pressuredistribution.png", bbox_inches="tight") plt.close() #Plotting the number of clusters as a function of interation. plt.figure(1) plt.plot(number_of_clusters) plt.xlabel('Iteration number/time') plt.ylabel('Number of clusters') plt.title('Number_of_cluster(t)') plt.savefig(self.pathsimulation + "clusterN.png", bbox_inches="tight") plt.close()
def plot(ranks): plt.figure(figsize=(20, 10)) plt.title("A2.3 PageRank (s-Sensibility)") plt.xlabel("Node ID") plt.ylabel("Pagerank") for row in ranks: plt.plot(row) plt.legend(['s = %.2f' % s for s in numpy.arange(0.0, 1.0, 0.05)], loc='upper right', prop={'size': 7}) plt.savefig("submission/pagerank.png")
def _build_plot_static(self, metric_group): metrics = self.get_metrics_history() if metric_group not in metrics: raise ValueError("can't find metric group") for name, values in metrics[metric_group].items(): plt.plot(values, label=name) plt.legend(loc='best') plt.title(metric_group, fontsize=16, fontweight='bold') plt.xlabel("Generations") plt.show() return None
def plot_graph(self, auroc_results, y_label: str): for ad_key, ad_label in AD_NAMES.items(): plt.plot(self.x_axis, numpy.asarray(auroc_results[ad_key], ), label=ad_label) plt.legend(loc='lower left') plt.xlabel(self.x_axis_label) plt.ylabel(ylabel=y_label) to_print = plt.gcf() plt.show() file_name_with_metric = y_label + "-" + self.file_name to_print.savefig(file_name_with_metric, bbox_inches='tight')
def simulate(): count = Counter() N = 1000 W = max(W0 + U.random(), 0) for k in range(1, N + 1): W = max(W + U.random(), 0) count[W] += 1 if k % (N // 5) == 0: # make 5 plots x = [w for w in count] tot = sum(count.values()) y = [count[w] / tot for w in count] plt.plot(x, y, label="N={}".format(k)) return x, y
def plotting(self,pressurelattice,invasionlattice,pressure,number_of_clusters): from matplotlib.pylab import plt #Plotting the invasionlattice plt.figure(2) plt.imshow(invasionlattice, cmap='Greys',interpolation='nearest') plt.title("Invasion lattice") plt.colorbar() plt.savefig(self.pathsimulation + "invasionlattice.png", bbox_inches="tight") plt.close() #plotting the pressure plt.figure(5) plt.plot(pressure) plt.xlabel('Time') plt.ylabel('Pressure') plt.title('P(t)') plt.savefig(self.pathsimulation +"pressure.png", bbox_inches="tight") plt.close() #plotting the clusterlattice plt.figure(6) plt.imshow(self.lw, interpolation='nearest') plt.title("Clusterlattice") plt.colorbar() plt.savefig(self.pathsimulation +"clusterlattice.png", bbox_inches="tight") plt.close() #Temporal diagram plt.figure(7) plt.imshow(self.temporalplot,interpolation='nearest') plt.title('Temporal diagram') plt.colorbar() plt.savefig(self.pathsimulation +"temporal.png", bbox_inches="tight") plt.close() #Plotting pressure distribution in the cell. plt.figure(3) plt.hist(pressurelattice.ravel(), bins=30, fc='k', ec='k') plt.savefig(self.pathsimulation +"pressuredistribution.png", bbox_inches="tight") plt.close() #Plotting the number of clusters as a function of interation. plt.figure(1) plt.plot(number_of_clusters) plt.xlabel('Iteration number/time') plt.ylabel('Number of clusters') plt.title('Number_of_cluster(t)') plt.savefig(self.pathsimulation +"clusterN.png", bbox_inches="tight") plt.close()
def random_tensor(shape=None, sparsity=0.5, seed=1234, distributed='normal', normal=None, uniform=None, plot=True): np.random.seed(seed) tensor_size = prod(shape) if distributed == 'normal': mean, std = normal data = np.random.normal(mean, std, tensor_size) if plot == True: n, bins, patches = plt.hist(data, 30, normed=True, facecolor='blue', alpha=0.5) y = mlab.normpdf(bins, mean, std) plt.plot(bins, y, 'r--') plt.xlabel('Expectation') plt.ylabel('Probability') plt.title('Histogram of Normal Distribution:$\mu =' + str(mean) + '$, $\sigma=' + str(std) + '$') plt.axvline(norm.ppf(sparsity) * std + mean) plt.show() data[data <= norm.ppf(sparsity) * std + mean] = 0 if distributed == 'uniform': low, high = uniform data = np.random.uniform(low, high, size=tensor_size) if plot == True: n, bins, patches = plt.hist(data, 30, normed=True, facecolor='blue', alpha=0.5) plt.xlabel('Expectation') plt.ylabel('Probability') plt.title('Histogram of Uniform Distribution:$low =' + str(low) + '$, $high =' + str(high) + '$') plt.axvline((high - low) * sparsity) plt.show() data[data <= (high - low) * sparsity] = 0 return tensor(data.reshape(shape))
def Michele_spectra(GCssorted): for i in range(0,100): hdu = fits.open(GCssorted['ORIGINALFILE'].iloc[i]) header1 = hdu[1].header data = hdu[1].data hdu.close() lamRange = header1['CRVAL1'] + np.array([0., header1['CD1_1'] * (header1['NAXIS1'] - 1)]) zp = 1. + (GCssorted['VREL'].iloc[i] / 299792.458) wavelength = np.linspace(lamRange[0],lamRange[1], header1['NAXIS1']) / zp df = pd.DataFrame({'wavelength':wavelength, 'counts':data}) df.to_csv('/Volumes/VINCE/OAC/highSN/{}.csv'.format(GCssorted['ID'].iloc[i])) plt.plot(wavelength,data) return df
def plot_average(collected_results, versions, args, plot_std=True): test_type = args.test_type model_name = args.model means, stds = [], [] for version in versions: data = collected_results[version] if (plot_std): means.append(np.mean(data)) stds.append(np.std(data)) else: means.append(data) means = np.array(means) stds = np.array(stds) if (test_type == "size" or test_type == "allsize"): x = ["0%", "20%", "40%", "60%", "80%", "100%"] elif (test_type == "accdomain" or test_type == "moredomain"): x = [0, 1, 2, 3, 4] else: x = versions color = 'blue' plt.plot(x, means, color=color) if (plot_std): plt.fill_between(x, means - stds, means + stds, alpha=0.1, edgecolor=color, facecolor=color, linewidth=1, antialiased=True) plt.xticks(np.arange(len(x)), x, fontsize=18) plt.yticks(fontsize=18) plt.xlabel(XLABELS[test_type], fontsize=18) plt.ylabel('average absolute effect size', fontsize=18) plt.title("Influence of {} on bias removal \nfor {}".format( TITLES[test_type], MODEL_FORMAL_NAMES[model_name]), fontsize=18) plt.tight_layout() plot_path = os.path.join( args.eval_results_dir, "plots", "{}-{}-avg{}.png".format(model_name, test_type, "-std" if plot_std else "")) plt.savefig(plot_path)
def saveMagPhasePlot(self, t, x, filename): mag = absolute(x).astype('float') phase = angle(x).astype('float') plt.subplot(211) plt.plot(t, mag) plt.ylabel('Magitude') plt.title('SSPF Sequence') plt.grid(True) plt.subplot(212) plt.plot(t, phase) plt.xlabel('Off-Resonance (Hz)') plt.ylabel('Phase') plt.grid(True) plt.savefig(filename)
def showMagPhasePlot(self, t, x): mag = absolute(x) phase = angle(x) plt.subplot(211) plt.plot(t, mag) plt.ylabel('Magitude') plt.title('SSPF Sequence') plt.grid(True) plt.subplot(212) plt.plot(t, phase) plt.xlabel('Off-Resonance (Hz)') plt.ylabel('Phase') plt.grid(True) plt.show()
def plot_score_dist(spacing, std_along, prob_miss, max_distance): from matplotlib.pylab import plt plt.close("Score Dist") plt.figure("Score Dist") d = np.linspace(0, max_distance, 500) plt.plot(d, [score_dist(di, spacing, std_along, prob_miss) for di in d]) plt.vlines(spacing, 0, 1) plt.vlines(spacing * 2, 0, 1, ls='--') plt.annotate("Miss-detect the next mine", (spacing * 2, 0.5), (12, 0), textcoords='offset points') plt.ylabel('$p(d)$') plt.xlabel('$d$') plt.grid() plt.xticks(np.arange(max_distance)) plt.xlim(0, max_distance) plt.savefig('score_dist.pdf')
def plot_error(self, train_errors): n = len(train_errors) training, = plt.plot(range(n), train_errors, label="Training Error") plt.legend(handles=[training]) plt.title("Error Plot") plt.ylabel('Error') plt.xlabel('Iterations') plt.show()
def pretty_plot_metrics(name, data): plt.plot(data['labeled_acc'], '-', color='b', label='Labeled Accuracy') plt.plot(data['labeled_loss'], ':', color='b', label='Labeled Loss') plt.plot(data['unlabeled_acc'], '-', color='r', label='Unsupervised Accuracy') plt.plot(data['unlabeled_loss'], ':', color='r', label='Unsupervised Loss') plt.legend() plt.savefig(name + '.png')
def f3(): xs = [0, 1, 2, 5] ys = [0, 0, 1, 1] fig, ax = plt.subplots() ax.set_yticklabels([]) ax.set_xticklabels([]) plt.plot(xs, ys, label="$f_n(x)$") plt.xticks(xs, ['', r'$n-1$', r'$n$', '']) plt.yticks([1], ['$1$']) 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') ax.axis('equal') plt.show()
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 plt_score(history): plt.figure() plt.plot() plt.plot(history.history['acc']) plt.plot(history.history['val_acc']) plt.title('model accuracy') plt.ylabel('accuracy') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.savefig('acc.png') # loss plt.figure() plt.plot(history.history['loss']) plt.plot(history.history['val_loss']) plt.title('model loss') plt.ylabel('loss') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.savefig('loss.png')
def visualize(img, G, vertices): plt.imshow(img, cmap='gray') # draw edges by pts for (s, e) in G.edges(): vals = flatten([[v] for v in G[s][e].values()]) for val in vals: ps = val.get('pts', []) plt.plot(ps[:, 1], ps[:, 0], 'green') # draw node by o node, nodes = G.node(), G.nodes # deg = G.degree # ps = np.array([node[i]['o'] for i in nodes]) ps = np.array(vertices) plt.plot(ps[:, 1], ps[:, 0], 'r.') # title and show plt.title('Build Graph') plt.show()
def plotting(sim_context1,sim_context2,diff,data_df,total_samples): plt.plot(sim_context1,label="Context 1") plt.plot(sim_context2,label="Context 2") x_labels_word1 = data_df["word1"] x_labels_word2 = data_df["word2"] xlabels = [0] * total_samples xticks_x = [0] * total_samples for wp in range (total_samples): xlabels[wp] = x_labels_word1[wp]+ "\n"+x_labels_word2[wp] xticks_x[wp] = wp+1 plt.plot(diff,label="Difference") plt.legend(loc='center right') # Add title and x, y labels plt.title("Elmo Embedding Model Results", fontsize=16, fontweight='bold') plt.xlabel("Word") plt.ylabel("Similarity") plt.xticks(xticks_x, xlabels) plt.show()
def __init__(self, dir_name='/home/steve/Data/FusingLocationData/0010/'): if not dir_name[-1] is '/': dir_name = dir_name + '/' imu_left = np.loadtxt(dir_name + 'LEFT_FOOT.data', delimiter=',') imu_right = np.loadtxt(dir_name + 'RIGHT_FOOT.data', delimiter=',') imu_head = np.loadtxt(dir_name + 'HEAD.data', delimiter=',') uwb_head = np.loadtxt(dir_name + 'uwb_result.csv', delimiter=',') beacon_set = np.loadtxt(dir_name + 'beaconSet.csv', delimiter=',') print('average time interval of left:', float(imu_left[-1, 1] - imu_left[0, 1]) / float(imu_left.shape[0])) print('average time interval of right:', float(imu_right[-1, 1] - imu_right[0, 1]) / float(imu_right.shape[0])) print('average time interval of head:', float(imu_head[-1, 1] - imu_head[0, 1]) / float(imu_head.shape[0])) plt.figure() plt.plot(imu_left[1:, 1] - imu_left[:-1, 1], label='time left') plt.plot(imu_right[1:, 1] - imu_right[:-1, 1], label='time right') # time_diff = imu_left[1:,1] - imu_left[:-1,1] # plt.plot(time_diff-time_diff.mean(),label='time diff') plt.plot(imu_head[1:, 1] - imu_head[:-1, 1], label=' time head') plt.grid() plt.legend() plt.show()
def visualize(img, G, vertices, fn): plt.imshow(img) # draw edges by pts for (s, e) in G.edges(): vals = flatten([[v] for v in G[s][e].values()]) for val in vals: ps = val.get('pts', []) plt.plot(ps[:, 1], ps[:, 0], 'green') # draw node by o #node, nodes = G.node(), G.nodes # deg = G.degree # ps = np.array([node[i]['o'] for i in nodes]) ps = np.array(vertices) plt.plot(ps[:, 1], ps[:, 0], 'r.') # title and show img_name = fn.split('.')[0] + ".png" map_name = fn.split('.')[0] + "_roads.png" io.imsave(img_name, img) plt.savefig(map_name)
def print_all_results(collected_results, versions, args): test_type = args.test_type model_name = args.model for test_name in collected_results: test_results = collected_results[test_name] x, y = [], [] for version in versions: if (version in test_results): x.append(version) y.append(test_results[version]['mean']) plt.plot(x, y, label=test_name) plt.xticks(np.arange(len(x)), x) plt.xlabel(XLABELS[test_type]) plt.ylabel('average absolute effect size') plt.legend(loc='best') plt.title("SEAT effect sizes on {} with {}".format(model_name, TITLES[test_type])) plot_path = os.path.join(args.eval_results_dir, "plots", "{}-{}.png".format(model_name, test_type)) plt.savefig(plot_path)
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 f4(): capped_xs = np.linspace(-0.94, 0.8, 1000) xs = np.linspace(-1, 1, 1000) ys = [1 / (1 - x) for x in capped_xs] def getN(N): def fN(x): total = 0.0 for i in range(N + 1): total += x**i return total return fN yss = [] for N in range(1, 4): fN = getN(N) yss.append([fN(x) for x in xs]) fig, ax = plt.subplots() ax.set_yticklabels([]) ax.set_xticklabels([]) plt.plot(capped_xs, ys, label=r"$\frac{1}{1-x}$") for i, ys in enumerate(yss): plt.plot(xs, ys, label="$N={}$".format(i + 1), alpha=0.3) plt.plot([1, 1], [0, 5], "k--", alpha=0.3) plt.plot(-1, 0.5, 'o', markerfacecolor='None', markeredgecolor='C0', markersize=5) plt.xticks([-1, 1], ['-1', '1']) 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') ax.axis('equal') plt.show()
def SSFP(): T1 = 800*10**-3 T2 = 100*10**-3 a = pi/2 Tr = .01 t = np.linspace(0, Tr, 100) M = np.array([0,0,1]) R = np.array([[1, 0, 0], [0, cos(a), sin(a)], [0, -sin(a), cos(a)]]) Mx = [] My = [] Mz = [] Nr = 10 for n in range(Nr): # Initalize and Rotate M = R.dot(M) # T1/T2 Relaxationq M_z = 1 + (M[2] - 1) * np.exp(-t/T1) M_x = M[0]*np.exp(-t/T2) M_y = M[1]*np.exp(-t/T2) M_xy = np.sqrt(M_x*M_x + M_y*M_y) M[0] = M_x[-1] M[1] = M_y[-1] M[2] = M_z[-1] Mx = np.append(Mx, M_x) My = np.append(My, M_y) Mz = np.append(Mz, M_z) tnew = np.linspace(0, 10*Tr, 1000) plt.subplot(131) plt.plot(tnew, Mx) plt.subplot(132) plt.plot(tnew, My) plt.subplot(133) plt.plot(tnew, Mz) plt.show()
ax[1][2].set_xlabel(r'$v [km s^{-1}]$') ax[1][2].set_ylabel('(u - g)') ax[1][2].set_xticks(np.arange(-500, 2600,500)) ax[0][2].scatter(GCs['VREL_helio'], GCs['g_auto'] - GCs['i_auto'], s=13, c='red', edgecolor='none') ax[0][2].scatter(stars['VREL_helio'], stars['g_auto'] - stars['i_auto'], s=13, c='green', marker='x', edgecolor='none') ax[0][2].set_xlabel(r'$v [km s^{-1}]$') ax[0][2].set_ylabel('(g - i)') ax[0][2].set_xticks(np.arange(-500, 2600,500)) # - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - x = GCs['R'].copy().sort_values() y = np.arange(1,len(GCs)+1) plt.plot(x,y,label='GCs') x = stars['R'].copy().sort_values() y = np.arange(1,len(stars)+1) plt.plot(x,y,label='Stars') plt.legend() # - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - mask = (((result['g_auto'] - result['r_auto']) < (0.2 + 0.6 * (result['g_auto'] - result['i_auto']))) & ((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) &
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')
from sklearn.learning_curve import learning_curve train_sizes, train_scores, test_scores = learning_curve(model,X,y,train_sizes=np.logspace(-3,0,100)) 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)) #
def drawErrorPlot(self, error_list): plt.xlim(0, len(error_list)) plt.xlabel('Squared Error') plt.plot(error_list) plt.show()
return w, fit_score def predict(x, X, w, L): f = dot( kerMat(x, X), w ) v = solve(L, kerMat(x, X)) var = kerMat( x, x ) - dot(v.T, v) return f, var # Generating Training Data x = linspace(0, 2*pi, 50) y = sin(x) # Modeling: Parameters estimation noise_level = 0.01 w = solve( kerMat(x, x) + noise_level * eye(x.size), y ) log_marginal_likelihood = - dot(y, w)/2 - sum( log( diag( cholesky(kerMat(x, x) + noise_level * eye(x.size)) ) ) ) - x.size*log(2*pi)/2 print log_marginal_likelihood # Prediction x_star = linspace(0, 2*pi, 1000) y_star = dot( kerMat(x_star, x), w ) var_star = kerMat( x_star, x_star ) - dot( kerMat( x_star, x ), solve( kerMat( x, x ), kerMat( x, x_star ) ) ) # Plotting plt.plot(x_star, y_star) #plt.plot(x_star, y_star+100*diag(var_star)) #plt.plot(x_star, y_star-100*diag(var_star)) plt.plot(x_star, sin(x_star)) for i in range(x.size): plt.scatter( x[i], y[i] ) plt.show()
self.press(event) def is_just_outside(fig, event): x, y = event.x, event.y print "x:", x, y for ax in fig.axes: xAxes, yAxes = ax.transAxes.inverted().transform([x, y]) print "xAxes:", xAxes, yAxes # if (-0.02 < xAxes < 0) | (1 < xAxes < 1.02): # print "just outside x-axis" # if (-0.02 < yAxes < 0) | (1 < yAxes < 1.02): # print "just outside y-axis" if (xAxes < 0) | (1 < xAxes): print "just outside x-axis" if (yAxes < 0) | (1 < yAxes): print "just outside y-axis" x = np.linspace(-np.pi, np.pi, 100) y = np.sin(x) fig = plt.figure() plt.plot(x, y) ax = fig.add_subplot(111) fig.canvas.mpl_connect('button_press_event', lambda e: is_just_outside(fig, e)) circ = patches.Circle((0.6, 0.5), 0.25, transform=ax.transAxes, facecolor='yellow', alpha=0.5) ax.add_patch(circ) plt.show()
def show(self, w): """ illustrate the learning curve Parameters ---------- w : int, window size for smoothing the curve """ if len(self.tn_mc) > 0: # malis training, increase number of subplots nsp = 5 else: nsp = 3 # print the maximum iteration self.print_max_update() # using K as iteration unit tn_it = self.tn_it for i in xrange(len(tn_it)): tn_it[i] = tn_it[i] / float(1000) tt_it = self.tt_it for i in xrange(len(tt_it)): tt_it[i] = tt_it[i] / float(1000) # plot data plt.subplot(1,nsp, 1) plt.plot(tn_it, self.tn_err, 'b.', alpha=0.2) plt.plot(tt_it, self.tt_err, 'r.', alpha=0.2) # plot smoothed line xne,yne = self._smooth( tn_it, self.tn_err, w ) xte,yte = self._smooth( tt_it, self.tt_err, w ) plt.plot(xne, yne, 'b') plt.plot(xte, yte, 'r') plt.xlabel('iteration (K)'), plt.ylabel('cost energy') plt.subplot(1,nsp,2) plt.plot(tn_it, self.tn_cls, 'b.', alpha=0.2) plt.plot(tt_it, self.tt_cls, 'r.', alpha=0.2) # plot smoothed line xnc, ync = self._smooth( tn_it, self.tn_cls, w ) xtc, ytc = self._smooth( tt_it, self.tt_cls, w ) plt.plot(xnc, ync, 'b', label='train') plt.plot(xtc, ytc, 'r', label='test') plt.xlabel('iteration (K)'), plt.ylabel( 'classification error' ) if len(tn_it) == len( self.tn_re ): plt.subplot(1, nsp, 3) plt.plot(tn_it, self.tn_re, 'b.', alpha=0.2) plt.plot(tt_it, self.tt_re, 'r.', alpha=0.2) # plot smoothed line xnr, ynr = self._smooth( tn_it, self.tn_re, w ) xtr, ytr = self._smooth( tt_it, self.tt_re, w ) plt.plot(xnr, ynr, 'b', label='train') plt.plot(xtr, ytr, 'r', label='test') plt.xlabel('iteration (K)'), plt.ylabel( 'rand error' ) if len(tn_it) == len( self.tn_mc ): plt.subplot(1, nsp, 4) plt.plot(tn_it, self.tn_mc, 'b.', alpha=0.2) plt.plot(tt_it, self.tt_mc, 'r.', alpha=0.2) # plot smoothed line xnm, ynm = self._smooth( tn_it, self.tn_mc, w ) xtm, ytm = self._smooth( tt_it, self.tt_mc, w ) plt.plot(xnm, ynm, 'b', label='train') plt.plot(xtm, ytm, 'r', label='test') plt.xlabel('iteration (K)'), plt.ylabel( 'malis weighted cost energy' ) if len(tn_it) == len( self.tn_me ): plt.subplot(1, nsp, 5) plt.plot(tn_it, self.tn_me, 'b.', alpha=0.2) plt.plot(tt_it, self.tt_me, 'r.', alpha=0.2) # plot smoothed line xng, yng = self._smooth( tn_it, self.tn_me, w ) xtg, ytg = self._smooth( tt_it, self.tt_me, w ) plt.plot(xng, yng, 'b', label='train') plt.plot(xtg, ytg, 'r', label='test') plt.xlabel('iteration (K)'), plt.ylabel( 'malis weighted pixel error' ) plt.legend() plt.show() return
def show(self, w): """ illustrate the learning curve Parameters ---------- w : int, window size for smoothing the curve """ # plot data plt.subplot(121) plt.plot(self.tn_it, self.tn_err, 'b.', alpha=0.2) plt.plot(self.tt_it, self.tt_err, 'r.', alpha=0.2) # plot smoothed line xne,yne = self._smooth( self.tn_it, self.tn_err, w ) xte,yte = self._smooth( self.tt_it, self.tt_err, w ) plt.plot(xne, yne, 'b') plt.plot(xte, yte, 'r') plt.xlabel('iteration'), plt.ylabel('cost energy') plt.subplot(122) plt.plot(self.tn_it, self.tn_cls, 'b.', alpha=0.2) plt.plot(self.tt_it, self.tt_cls, 'r.', alpha=0.2) # plot smoothed line xnc, ync = self._smooth( self.tn_it, self.tn_cls, w ) xtc, ytc = self._smooth( self.tt_it, self.tt_cls, w ) plt.plot(xnc, ync, 'b', label='train') plt.plot(xtc, ytc, 'r', label='test') plt.xlabel('iteration'), plt.ylabel( 'classification error' ) plt.legend() plt.show() return