def plot_feature_comparison(title, trajectory, features): plotCount = features.shape[1] pointCount = features.shape[0] fig = plt.figure() plt.title(title) #plt.ion() #plt.show() max = np.amax(features) min = np.amin(features) print "min=" + str(min) + ", max=" + str(max) for i in range(plotCount): plt.subplot(plotCount/2, 2, 1+i) f = features[:,i] for k in range(pointCount): color = '' if(f[k] > max * 0.6): color = 'r' elif(f[k] > max * 0.3): color = 'y' elif(f[k] < min * 0.3): color = 'b' elif(f[k] < min * 0.6): color = 'g' if (color != ''): plt.plot(trajectory[k,0], trajectory[k,1], color+'.', markersize=20) #plt.draw() plt.plot(trajectory[:,0], trajectory[:,1], 'k') plt.show() #raw_input() return
def show_cmaps(names): matplotlib.rc('text', usetex=False) a=np.outer(np.arange(0,1,0.01),np.ones(10)) # pseudo image data f=figure(figsize=(10,5)) f.subplots_adjust(top=0.8,bottom=0.05,left=0.01,right=0.99) # get list of all colormap names # this only obtains names of built-in colormaps: maps=[m for m in cm.datad if not m.endswith("_r")] # use undocumented cmap_d dictionary instead maps = [m for m in cm.cmap_d if not m.endswith("_r")] maps.sort() # determine number of subplots to make l=len(maps)+1 if names is not None: l=len(names) # assume all names are correct! # loop over maps and plot the selected ones i=0 for m in maps: if names is None or m in names: i+=1 ax = subplot(1,l,i) ax.axis("off") imshow(a,aspect='auto',cmap=cm.get_cmap(m),origin="lower") title(m,rotation=90,fontsize=10,verticalalignment='bottom') # savefig("colormaps.png",dpi=100,facecolor='gray') show()
def showCumulOverlap(mode, modes, *args, **kwargs): """Show cumulative overlap using :func:`~matplotlib.pyplot.plot`. :type mode: :class:`.Mode`, :class:`.Vector` :arg modes: multiple modes :type modes: :class:`.ModeSet`, :class:`.ANM`, :class:`.GNM`, :class:`.PCA` """ import matplotlib.pyplot as plt if not isinstance(mode, (Mode, Vector)): raise TypeError('mode must be NMA, ModeSet, Mode or Vector, not {0}' .format(type(mode))) if not isinstance(modes, (NMA, ModeSet)): raise TypeError('modes must be NMA, ModeSet, or Mode, not {0}' .format(type(modes))) cumov = (calcOverlap(mode, modes) ** 2).cumsum() ** 0.5 if isinstance(modes, NMA): arange = np.arange(0.5, len(modes)+0.5) else: arange = modes.getIndices() + 0.5 show = plt.plot(arange, cumov, *args, **kwargs) plt.title('Cumulative overlap with {0}'.format(str(mode))) plt.xlabel('{0} mode index'.format(modes)) plt.ylabel('Cumulative overlap') plt.axis((arange[0]-0.5, arange[-1]+0.5, 0, 1)) if SETTINGS['auto_show']: showFigure() return show
def make_bar( x, y, f_name, title=None, legend=None, x_label=None, y_label=None, x_ticks=None, y_ticks=None, ): fig = plt.figure() if title is not None: plt.title(title, fontsize=16) if x_label is not None: plt.ylabel(x_label) if y_label is not None: plt.xlabel(y_label) if x_ticks is not None: plt.xticks(x, x_ticks) if y_ticks is not None: plt.yticks(y_ticks) plt.bar(x, y, align="center") if legend is not None: plt.legend(legend) plt.savefig(f_name) plt.close(fig)
def showNormDistFunct(model, coords, *args, **kwargs): """Show normalized distance fluctuation matrix using :func:`~matplotlib.pyplot.imshow`. By default, *origin=lower* keyword arguments are passed to this function, but user can overwrite these parameters.""" import math import matplotlib import matplotlib.pyplot as plt normdistfunct = model.getNormDistFluct(coords) if not 'origin' in kwargs: kwargs['origin'] = 'lower' matplotlib.rcParams['font.size'] = '14' fig = plt.figure(num=None, figsize=(10,8), dpi=100, facecolor='w') show = plt.imshow(normdistfunct, *args, **kwargs), plt.colorbar() plt.clim(math.floor(np.min(normdistfunct[np.nonzero(normdistfunct)])), \ round(np.amax(normdistfunct),1)) plt.title('Normalized Distance Fluctution Matrix') plt.xlabel('Indices', fontsize='16') plt.ylabel('Indices', fontsize='16') if SETTINGS['auto_show']: showFigure() return show
def plot_predict_is(self,h=5,**kwargs): """ Plots forecasts with the estimated model against data (Simulated prediction with data) Parameters ---------- h : int (default : 5) How many steps to forecast Returns ---------- - Plot of the forecast against data """ figsize = kwargs.get('figsize',(10,7)) plt.figure(figsize=figsize) date_index = self.index[-h:] predictions = self.predict_is(h) data = self.data[-h:] t_params = self.transform_z() plt.plot(date_index,np.abs(data-t_params[-1]),label='Data') plt.plot(date_index,predictions,label='Predictions',c='black') plt.title(self.data_name) plt.legend(loc=2) plt.show()
def showOverlap(mode, modes, *args, **kwargs): """Show overlap :func:`~matplotlib.pyplot.bar`. :arg mode: a single mode/vector :type mode: :class:`.Mode`, :class:`.Vector` :arg modes: multiple modes :type modes: :class:`.ModeSet`, :class:`.ANM`, :class:`.GNM`, :class:`.PCA` """ import matplotlib.pyplot as plt if not isinstance(mode, (Mode, Vector)): raise TypeError('mode must be Mode or Vector, not {0}' .format(type(mode))) if not isinstance(modes, (NMA, ModeSet)): raise TypeError('modes must be NMA or ModeSet, not {0}' .format(type(modes))) overlap = abs(calcOverlap(mode, modes)) if isinstance(modes, NMA): arange = np.arange(0.5, len(modes)+0.5) else: arange = modes.getIndices() + 0.5 show = plt.bar(arange, overlap, *args, **kwargs) plt.title('Overlap with {0}'.format(str(mode))) plt.xlabel('{0} mode index'.format(modes)) plt.ylabel('Overlap') if SETTINGS['auto_show']: showFigure() return show
def draw_ranges_for_parameters(data, title='', save_path='./pictures/'): parameters = data.columns.values.tolist() # remove flight name parameter for idx, parameter in enumerate(parameters): if parameter == 'flight_name': del parameters[idx] flight_names = np.unique(data['flight_name']) print len(flight_names) for parameter in parameters: plt.figure() axis = plt.gca() # ax.set_xticks(numpy.arange(0,1,0.1)) axis.set_yticks(flight_names) axis.tick_params(labelright=True) axis.set_ylim([94., 130.]) plt.grid() plt.title(title) plt.xlabel(parameter) plt.ylabel('flight name') colors = iter(cm.rainbow(np.linspace(0, 1,len(flight_names)))) for flight in flight_names: temp = data[data.flight_name == flight][parameter] plt.plot([np.min(temp), np.max(temp)], [flight, flight], c=next(colors), linewidth=2.0) plt.savefig(save_path+title+'_'+parameter+'.jpg') plt.close()
def show_plot(X, y, n_neighbors=10, h=0.2): # Create color maps cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF','#FFAAAA', '#AAFFAA', '#AAAAFF','#FFAAAA', '#AAFFAA', '#AAAAFF','#AAAAFF']) cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF','#FF0000','#FF0000','#FF0000','#FF0000','#FF0000','#FF0000','#FF0000',]) for weights in ['uniform', 'distance']: # we create an instance of Neighbours Classifier and fit the data. clf = neighbors.KNeighborsClassifier(n_neighbors, weights=weights) clf.fit(X, y) clf.n_neighbors = n_neighbors # Plot the decision boundary. For that, we will assign a color to each # point in the mesh [x_min, x_max]x[y_min, y_max]. x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) # Put the result into a color plot Z = Z.reshape(xx.shape) plt.figure() plt.pcolormesh(xx, yy, Z, cmap=cmap_light) # Plot also the training points plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold) plt.xlim(xx.min(), xx.max()) plt.ylim(yy.min(), yy.max()) plt.title("3-Class classification (k = %i, weights = '%s')" % (n_neighbors, weights)) plt.show()
def main(): gw = gridworld() a = agent(gw) for epoch in range(20): a.initEpoch() while True: rwd, stat, act = a.takeAction() a.updateQ(rwd, stat, act) if gw.status() == 'Goal': break if mod(a.counter, 10)==0: print(gw.state()) print(gw.field()) print('Finished') print(a.counter) print(gw.state()) print(gw.field()) Q = transpose(a.Q(), (2,0,1)) for i in range(4): plt.subplot(2,2,i) plt.imshow(Q[i], interpolation='nearest') plt.title(a.actions()[i]) plt.colorbar() plt.show()
def plotResults(datasetName, sampleSizes, foldsSet, cvScalings, sampleMethods, fileNameSuffix): """ Plots the errors for a particular dataset on a bar graph. """ for k in range(len(sampleMethods)): outfileName = outputDir + datasetName + sampleMethods[k] + fileNameSuffix + ".npz" data = numpy.load(outfileName) errors = data["arr_0"] meanMeasures = numpy.mean(errors, 0) for i in range(sampleSizes.shape[0]): plt.figure(k*len(sampleMethods) + i) plt.title("n="+str(sampleSizes[i]) + " " + sampleMethods[k]) for j in range(errors.shape[3]): plt.plot(foldsSet, meanMeasures[i, :, j]) plt.xlabel("Folds") plt.ylabel('Error') labels = ["VFCV", "PenVF+"] labels.extend(["VFP s=" + str(x) for x in cvScalings]) plt.legend(tuple(labels)) plt.show()
def run_test(fld, seeds, plot2d=True, plot3d=True, add_title="", view_kwargs=None, show=False, scatter_mpl=False, mesh_mvi=True): interpolated_fld = viscid.interp_trilin(fld, seeds) seed_name = seeds.__class__.__name__ if add_title: seed_name += " " + add_title try: if not plot2d: raise ImportError from viscid.plot import vpyplot as vlt from matplotlib import pyplot as plt plt.clf() # plt.plot(seeds.get_points()[2, :], fld) mpl_plot_kwargs = dict() if interpolated_fld.is_spherical(): mpl_plot_kwargs['hemisphere'] = 'north' vlt.plot(interpolated_fld, **mpl_plot_kwargs) plt.title(seed_name) plt.savefig(next_plot_fname(__file__, series='2d')) if show: plt.show() if scatter_mpl: plt.clf() vlt.plot2d_line(seeds.get_points(), fld, symdir='z', marker='o') plt.savefig(next_plot_fname(__file__, series='2d')) if show: plt.show() except ImportError: pass try: if not plot3d: raise ImportError from viscid.plot import vlab _ = get_mvi_fig(offscreen=not show) try: if mesh_mvi: mesh = vlab.mesh_from_seeds(seeds, scalars=interpolated_fld) mesh.actor.property.backface_culling = True except RuntimeError: pass pts = seeds.get_points() p = vlab.points3d(pts[0], pts[1], pts[2], interpolated_fld.flat_data, scale_mode='none', scale_factor=0.02) vlab.axes(p) vlab.title(seed_name) if view_kwargs: vlab.view(**view_kwargs) vlab.savefig(next_plot_fname(__file__, series='3d')) if show: vlab.show(stop=True) except ImportError: pass
def default_run(self): """ Plots the results, saves the figure, and finally displays it from simulating codewords with Sum-prod and Max-prod algorithms across variance levels. This combines the results in one plot. :return: """ if not os.path.exists("./graphs"): os.makedirs("./graphs") self.save_time = str(int(time.time())) self.simulate(Decoder.SUM_PROD) self.compute_error() plt.plot([math.log10(x) for x in self.variance_levels], [math.log10(y) for y in self.bit_error_probability], "ro-", label="Sum-Prod") self.simulate(Decoder.MAX_PROD) self.compute_error() plt.plot([math.log10(x) for x in self.variance_levels], [math.log10(y) for y in self.bit_error_probability], "g^--", label="Max-Prod") plt.legend(loc=2) plt.title("Hamming Decoder Factor Graph Simulation Results\n" + r"$\log_{10}(\sigma^2)$ vs. $\log_{10}(P_e)$" + " for Max-Prod & Sum-Prod Algorithms\n" + "Sample Size n = %(codewords)s Codewords \n Variance Levels = %(levels)s" % {"codewords": str(self.iterations), "levels": str(self.variance_levels)}) plt.xlabel("$\log_{10}(\sigma^2)$") plt.ylabel(r"$\log_{10}(P_e)$") plt.savefig("graphs/%(time)s-max-prod-sum-prod-%(num_codewords)s-codewords-variance-bit_error_probability.png" % {"time": self.save_time, "num_codewords": str(self.iterations)}, bbox_inches="tight") plt.show()
def build_hist(self, coverage, show=False, save=False, save_fn="max_hist_plot"): """ Build a histogram to determine what the maxes look & visualize match_count Might be used to determine a resonable threshold @param coverage: the average coverage for an single nt @param show: Show visualization with match maxes @param save_fn: Save to disk with this file name or else it will be the default @return: the histogram array """ #import matplotlib #matplotlib.use("Agg") import matplotlib.pyplot as plt maxes = self.match_count.max(1) # get maxes along 1st dim h = plt.hist(maxes, bins=self.match_count.shape[0]) # figure out where the majority plt.ylabel("Frequency") plt.xlabel("Count per index") plt.title("Frequency count histogram") if show: plt.show() if save: plt.savefig(save_fn, dpi=160, frameon=False) return h[0]
def plot_scenario(strategies, names, scenario_id=1): probabilities = get_scenario(scenario_id) plt.figure(figsize=(6, 4.5)) ax = plt.subplot(111) ax.spines["top"].set_visible(False) ax.spines["bottom"].set_visible(False) ax.spines["right"].set_visible(False) ax.spines["left"].set_visible(False) ax.get_xaxis().tick_bottom() ax.get_yaxis().tick_left() plt.yticks(fontsize=14) plt.xticks(fontsize=14) plt.xlim((0, 1300)) # Remove the tick marks; they are unnecessary with the tick lines we just plotted. plt.tick_params(axis="both", which="both", bottom="on", top="off", labelbottom="on", left="off", right="off", labelleft="on") for rank, (strategy, name) in enumerate(zip(strategies, names)): plot_strategy(probabilities, strategy, name, rank) plt.title("Bandits: " + str(probabilities), fontweight='bold') plt.xlabel('Number of Trials', fontsize=14) plt.ylabel('Cumulative Regret', fontsize=14) plt.legend(names) plt.show()
def plotErrorBars(dict_to_plot, x_lim, y_lim, xlabel, y_label, title, out_file, margin=[0.05, 0.05], loc=2): plt.title(title) plt.xlabel(xlabel) plt.ylabel(y_label) if y_lim is None: y_lim = [1 * float("Inf"), -1 * float("Inf")] max_val_seen_y = y_lim[1] - margin[1] min_val_seen_y = y_lim[0] + margin[1] print min_val_seen_y, max_val_seen_y max_val_seen_x = x_lim[1] - margin[0] min_val_seen_x = x_lim[0] + margin[0] handles = [] for k in dict_to_plot: means, stds, x_vals = dict_to_plot[k] min_val_seen_y = min(min(np.array(means) - np.array(stds)), min_val_seen_y) max_val_seen_y = max(max(np.array(means) + np.array(stds)), max_val_seen_y) min_val_seen_x = min(min(x_vals), min_val_seen_x) max_val_seen_x = max(max(x_vals), max_val_seen_x) handle = plt.errorbar(x_vals, means, yerr=stds) handles.append(handle) print max_val_seen_y plt.xlim([min_val_seen_x - margin[0], max_val_seen_x + margin[0]]) plt.ylim([min_val_seen_y - margin[1], max_val_seen_y + margin[1]]) plt.legend(handles, dict_to_plot.keys(), loc=loc) plt.savefig(out_file)
def plot_precision_recall_n(y_true, y_scores, model_name): ''' Takes the model, plots precision and recall curves ''' precision_curve, recall_curve, pr_thresholds = precision_recall_curve(y_true, y_scores) precision_curve = precision_curve[:-1] recall_curve = recall_curve[:-1] pct_above_per_thresh = [] number_scored = len(y_scores) for value in pr_thresholds: num_above_thresh = len(y_scores[y_scores >= value]) pct_above_thresh = num_above_thresh / float(number_scored) pct_above_per_thresh.append(pct_above_thresh) pct_above_per_thresh = np.array(pct_above_per_thresh) plt.clf() fig, ax1 = plt.subplots() ax1.plot(pct_above_per_thresh, precision_curve, 'b') ax1.set_xlabel('percent of population') ax1.set_ylabel('precision', color='b') ax2 = ax1.twinx() ax2.plot(pct_above_per_thresh, recall_curve, 'r') ax2.set_ylabel('recall', color='r') name = model_name plt.title(name) plt.savefig("Eval/{}.png".format(name))
def plotTestData(tree): plt.figure() plt.axis([0,1,0,1]) plt.xlabel("X axis") plt.ylabel("Y axis") plt.title("Green: Class1, Red: Class2, Blue: Class3, Yellow: Class4") for value in class1: plt.plot(value[0],value[1],'go') plt.hold(True) for value in class2: plt.plot(value[0],value[1],'ro') plt.hold(True) for value in class3: plt.plot(value[0],value[1],'bo') plt.hold(True) for value in class4: plt.plot(value[0],value[1],'yo') plotRegion(tree) for value in classPlot1: plt.plot(value[0],value[1],'g.',ms=3.0) plt.hold(True) for value in classPlot2: plt.plot(value[0],value[1],'r.', ms=3.0) plt.hold(True) for value in classPlot3: plt.plot(value[0],value[1],'b.', ms=3.0) plt.hold(True) for value in classPlot4: plt.plot(value[0],value[1],'y.', ms=3.0) plt.grid(True) plt.show()
def plotJ(J_history,num_iters): x = np.arange(1,num_iters+1) plt.plot(x,J_history) plt.xlabel(u"迭代次数",fontproperties=font) # 注意指定字体,要不然出现乱码问题 plt.ylabel(u"代价值",fontproperties=font) plt.title(u"代价随迭代次数的变化",fontproperties=font) plt.show()
def plot_mpl_fig(): rootdir = '/Users/catherinefielder/Documents/Research_Halos/HaloDetail' cs = [] pops = [] for subdir, dirs, files in os.walk(rootdir): head,tail = os.path.split(subdir) haloname = tail for file in files: if file.endswith('_columnsadded'): values = ascii.read(os.path.join(subdir, file), format = 'commented_header') #Get full path and access file host_c = values[1]['host_c'] cs = np.append(cs, host_c) pop = len(values['mvir(10)']) pops = np.append(pops, pop) print pop plt.loglog(host_c, pop, alpha=0.8,label = haloname) print "%s done. On to the next." %haloname #plt.xscale('log') #plt.yscale('log') plt.xlabel('Host Concentration') plt.ylabel('Nsat') plt.title('Abundance vs. Host Concentration', ha='center') #plt.legend(loc='best') spearman = scipy.stats.spearmanr(cs, pops) print spearman
def as_pyplot_figure(self, label=1, **kwargs): """Returns the explanation as a pyplot figure. Will throw an error if you don't have matplotlib installed Args: label: desired label. If you ask for a label for which an explanation wasn't computed, will throw an exception. kwargs: keyword arguments, passed to domain_mapper Returns: pyplot figure (barchart). """ import matplotlib.pyplot as plt exp = self.as_list(label, **kwargs) fig = plt.figure() vals = [x[1] for x in exp] names = [x[0] for x in exp] vals.reverse() names.reverse() colors = ['green' if x > 0 else 'red' for x in vals] pos = np.arange(len(exp)) + .5 plt.barh(pos, vals, align='center', color=colors) plt.yticks(pos, names) plt.title('Local explanation for class %s' % self.class_names[label]) return fig
def plotISVar(): plt.figure() plt.title('Variance minimization problem (call).\nVertical lines mark the minima.') for K in [0.6, 0.8, 1.0, 1.2]: theta = np.linspace(-0.6, 2) var = [BS.exactCallVar(K*s0, theta) for theta in theta] minth = theta[np.argmin(var)] line, = plt.plot(theta, var, label=str(K)) plt.axvline(minth, color=line.get_color()) plt.xlabel(r'$\theta$') plt.ylabel('call variance') plt.legend(title=r'$K/s_0$', loc='upper left') plt.autoscale(tight=True) plt.figure() plt.title('Variance minimization problem (put).\nVertical lines mark the minima.') for K in [0.8, 1.0, 1.2, 1.4]: theta = np.linspace(-2, 0.5) var = [BS.exactPutVar(K*s0, theta) for theta in theta] minth = theta[np.argmin(var)] line, = plt.plot(theta, var, label=str(K)) plt.axvline(minth, color=line.get_color()) plt.xlabel(r'$\theta$') plt.ylabel('put variance') plt.legend(title=r'$K/s_0$', loc='upper left') plt.autoscale(tight=True)
def visualize(segmentation, expression, visualize=None, store=None, title=None, legend=False): notes = [] onsets = [] values = [] param = ['Dynamics', 'Articulation', 'Tempo'] converter = NoteList() converter.bpm = 100 if not visualize: visualize = selectSubset(param) for segment, expr in zip(segmentation, expression): for note in segment: onsets.append(converter.ticks_to_milliseconds(note.on)/1000.0) values.append([expr[i] for i in visualize]) import matplotlib.pyplot as plt fig = plt.figure(figsize=(12, 4)) for i in visualize: plt.plot(onsets, [v[i] for v in values], label=param[i]) plt.ylabel('Deviation') plt.xlabel('Score time (seconds)') if legend: plt.legend(bbox_to_anchor=(0., 1), loc=2, borderaxespad=0.) if title: plt.title(title) #dplot = fig.add_subplot(111) #sodplot = fig.add_subplot(111) #dplot.plot([i for i in range(len(deltas[0]))], deltas[0]) #sodplot.plot([i for i in range(len(sodeltas[0]))], sodeltas[0]) if store: fig.savefig('plots/{0}.png'.format(store)) else: plt.show()
def predicted_probabilities(y_true, y_pred, n_groups=30): """Plots the distribution of predicted probabilities. Parameters ---------- y_true : array_like Observed labels, either 0 or 1. y_pred : array_like Predicted probabilities, floats on [0, 1]. n_groups : int, optional The number of groups to create. The default value is 30. Notes ----- .. plot:: pyplots/predicted_probabilities.py """ plt.hist(y_pred, n_groups) plt.xlim([0, 1]) plt.xlabel('Predicted Probability') plt.ylabel('Count') title = 'Distribution of Predicted Probabilities (n = {})' plt.title(title.format(len(y_pred))) plt.tight_layout()
def roc_plot(y_true, y_pred): """Plots a receiver operating characteristic. Parameters ---------- y_true : array_like Observed labels, either 0 or 1. y_pred : array_like Predicted probabilities, floats on [0, 1]. Notes ----- .. plot:: pyplots/roc_plot.py References ---------- .. [1] Pedregosa, F. et al. "Scikit-learn: Machine Learning in Python." *Journal of Machine Learning Research* 12 (2011): 2825–2830. .. [2] scikit-learn developers. "Receiver operating characteristic (ROC)." Last modified August 2013. http://scikit-learn.org/stable/auto_examples/plot_roc.html. """ fpr, tpr, __ = roc_curve(y_true, y_pred) roc_auc = auc(fpr, tpr) plt.plot(fpr, tpr, label='ROC curve (area = {:0.2f})'.format(roc_auc)) plt.plot([0, 1], [0, 1], 'k--') plt.xlim([0, 1]) plt.ylim([0, 1]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('Receiver Operating Characteristic') plt.legend(loc='lower right')
def _plot(self,names,title,style,when=0,showLegend=True): if isinstance(names,str): names = [names] assert isinstance(names,list) legend = [] for name in names: assert isinstance(name,str) legend.append(name) # if it's a differential state if name in self.xNames: index = self.xNames.index(name) ys = np.squeeze(self._log['x'])[:,index] ts = np.arange(len(ys))*self.Ts plt.plot(ts,ys,style) if name in self.outputNames: index = self.outputNames.index(name) ys = np.squeeze(self._log['outputs'][name]) ts = np.arange(len(ys))*self.Ts plt.plot(ts,ys,style) if title is not None: assert isinstance(title,str), "title must be a string" plt.title(title) plt.xlabel('time [s]') if showLegend is True: plt.legend(legend) plt.grid()
def ecdf_by_observed_label(y_true, y_pred): """Plots the empirical cumulative density functions by observed label. Parameters ---------- y_true : array_like Observed labels, either 0 or 1. y_pred : array_like Predicted probabilities, floats on [0, 1]. Notes ----- .. plot:: pyplots/ecdf_by_observed_label.py """ x = np.linspace(0, 1) ecdf = ECDF(y_pred[y_true == 0]) y_0 = ecdf(x) ecdf = ECDF(y_pred[y_true == 1]) y_1 = ecdf(x) plt.step(x, y_0, label='Observed label 0') plt.step(x, y_1, label='Observed label 1') plt.xlabel('Predicted Probability') plt.ylabel('Proportion') plt.title('Empirical Cumulative Density Functions by Observed Label') plt.legend(loc='lower right')
def display(spectrum): template = np.ones(len(spectrum)) #Get the plot ready and label the axes pyp.plot(spectrum) max_range = int(math.ceil(np.amax(spectrum) / standard_deviation)) for i in range(0, max_range): pyp.plot(template * (mean + i * standard_deviation)) pyp.xlabel('Units?') pyp.ylabel('Amps Squared') pyp.title('Mean Normalized Power Spectrum') if 'V' in Options: pyp.show() if 'v' in Options: tokens = sys.argv[-1].split('.') filename = tokens[0] + ".png" input = '' if os.path.isfile(filename): input = input("Error: Plot file already exists! Overwrite? (y/n)\n") while input != 'y' and input != 'n': input = input("Please enter either \'y\' or \'n\'.\n") if input == 'y': pyp.savefig(filename) else: print("Plot not written.") else: pyp.savefig(filename)
def make_line( x, y, f_name, title=None, legend=None, x_label=None, y_label=None, x_ticks=None, y_ticks=None, ): fig = plt.figure() if title is not None: plt.title(title, fontsize=16) if x_label is not None: plt.ylabel(x_label) if y_label is not None: plt.xlabel(y_label) if x_ticks is not None: plt.xticks(x, x_ticks) if y_ticks is not None: plt.yticks(y_ticks) if isinstance(y[0], list): for data in y: plt.plot(x, data) else: plt.plot(x, y) if legend is not None: plt.legend(legend) plt.savefig(f_name) plt.close(fig)
def zplane(self, title="", fontsize=18): """ Display filter in the complex plane Parameters ---------- """ rb = self.z ra = self.p t = np.arange(0, 2 * np.pi + 0.1, 0.1) plt.plot(np.cos(t), np.sin(t), "k") plt.plot(np.real(ra), np.imag(ra), "x", color="r") plt.plot(np.real(rb), np.imag(rb), "o", color="b") M1 = -10000 M2 = -10000 if len(ra) > 0: M1 = np.max([np.abs(np.real(ra)), np.abs(np.imag(ra))]) if len(rb) > 0: M2 = np.max([np.abs(np.real(rb)), np.abs(np.imag(rb))]) M = 1.6 * max(1.2, M1, M2) plt.axis([-M, M, -0.7 * M, 0.7 * M]) plt.title(title, fontsize=fontsize) plt.show()
def train_model_classification(X, X_test, y, params, folds, model_type='lgb', eval_metric='auc', columns=None, plot_feature_importance=False, model=None, verbose=10000, early_stopping_rounds=200, n_estimators=50000): """ A function to train a variety of regression models. Returns dictionary with oof predictions, test predictions, scores and, if necessary, feature importances. :params: X - training data, can be pd.DataFrame or np.ndarray (after normalizing) :params: X_test - test data, can be pd.DataFrame or np.ndarray (after normalizing) :params: y - target :params: folds - folds to split data :params: model_type - type of model to use :params: eval_metric - metric to use :params: columns - columns to use. If None - use all columns :params: plot_feature_importance - whether to plot feature importance of LGB :params: model - sklearn model, works only for "sklearn" model type """ columns = X.columns if columns == None else columns X_test = X_test[columns] # to set up scoring parameters metrics_dict = {'auc': {'lgb_metric_name': eval_auc, 'catboost_metric_name': 'AUC', 'sklearn_scoring_function': metrics.roc_auc_score}, } result_dict = {} # out-of-fold predictions on train data oof = np.zeros((len(X), len(set(y.values)))) # averaged predictions on train data prediction = np.zeros((len(X_test), oof.shape[1])) # list of scores on folds scores = [] feature_importance = pd.DataFrame() # split and train on folds for fold_n, (train_index, valid_index) in enumerate(folds.split(X)): print(f'Fold {fold_n + 1} started at {time.ctime()}') if type(X) == np.ndarray: X_train, X_valid = X[columns][train_index], X[columns][valid_index] y_train, y_valid = y[train_index], y[valid_index] else: X_train, X_valid = X[columns].iloc[train_index], X[columns].iloc[valid_index] y_train, y_valid = y.iloc[train_index], y.iloc[valid_index] if model_type == 'lgb': model = lgb.LGBMClassifier(**params, n_estimators=n_estimators, n_jobs=-1) model.fit(X_train, y_train, eval_set=[(X_train, y_train), (X_valid, y_valid)], eval_metric=metrics_dict[eval_metric]['lgb_metric_name'], verbose=verbose, early_stopping_rounds=early_stopping_rounds) y_pred_valid = model.predict_proba(X_valid) y_pred = model.predict_proba(X_test, num_iteration=model.best_iteration_) if model_type == 'xgb': train_data = xgb.DMatrix(data=X_train, label=y_train, feature_names=X.columns) valid_data = xgb.DMatrix(data=X_valid, label=y_valid, feature_names=X.columns) watchlist = [(train_data, 'train'), (valid_data, 'valid_data')] model = xgb.train(dtrain=train_data, num_boost_round=n_estimators, evals=watchlist, early_stopping_rounds=early_stopping_rounds, verbose_eval=verbose, params=params) y_pred_valid = model.predict(xgb.DMatrix(X_valid, feature_names=X.columns), ntree_limit=model.best_ntree_limit) y_pred = model.predict(xgb.DMatrix(X_test, feature_names=X.columns), ntree_limit=model.best_ntree_limit) if model_type == 'sklearn': model = model model.fit(X_train, y_train) y_pred_valid = model.predict(X_valid).reshape(-1, ) score = metrics_dict[eval_metric]['sklearn_scoring_function'](y_valid, y_pred_valid) print(f'Fold {fold_n}. {eval_metric}: {score:.4f}.') print('') y_pred = model.predict_proba(X_test) if model_type == 'cat': model = CatBoostClassifier(iterations=n_estimators, eval_metric=metrics_dict[eval_metric]['catboost_metric_name'], **params, loss_function=metrics_dict[eval_metric]['catboost_metric_name']) model.fit(X_train, y_train, eval_set=(X_valid, y_valid), cat_features=[], use_best_model=True, verbose=False) y_pred_valid = model.predict(X_valid) y_pred = model.predict(X_test) oof[valid_index] = y_pred_valid scores.append(metrics_dict[eval_metric]['sklearn_scoring_function'](y_valid, y_pred_valid[:, 1])) prediction += y_pred if model_type == 'lgb' and plot_feature_importance: # feature importance fold_importance = pd.DataFrame() fold_importance["feature"] = columns fold_importance["importance"] = model.feature_importances_ fold_importance["fold"] = fold_n + 1 feature_importance = pd.concat([feature_importance, fold_importance], axis=0) prediction /= folds.n_splits print('CV mean score: {0:.4f}, std: {1:.4f}.'.format(np.mean(scores), np.std(scores))) result_dict['oof'] = oof result_dict['prediction'] = prediction result_dict['scores'] = scores if model_type == 'lgb': if plot_feature_importance: feature_importance["importance"] /= folds.n_splits cols = feature_importance[["feature", "importance"]].groupby("feature").mean().sort_values( by="importance", ascending=False)[:50].index best_features = feature_importance.loc[feature_importance.feature.isin(cols)] plt.figure(figsize=(16, 12)); sns.barplot(x="importance", y="feature", data=best_features.sort_values(by="importance", ascending=False)); plt.title('LGB Features (avg over folds)'); result_dict['feature_importance'] = feature_importance return result_dict
for _ in t: u = controlled_plant.input() controlled_plant.step(dt) U.append(u) X.append(plant.x) Z_hat.append(controlled_plant.z_hat) Y.append(plant.output(u)) ETA_hat.append(controlled_plant.output()) # Plot time response plt.figure() plt.subplot(2, 1, 1) plt.plot(t, np.dot(100, Y)) plt.plot(t, np.dot(100, ETA_hat + y_eq)) plt.title('Pendulum Horizontal Displacement') plt.xlabel('Time (s)') plt.ylabel('Displacement (cm)') plt.legend(['Actual Displacement', 'Estimated Displacement']) plt.subplot(2, 1, 2) plt.plot(t, U) plt.title('Torque') plt.xlabel('Time (s)') plt.ylabel('Torque (N)') plt.tight_layout() # Plot trajectory on phase portrait plt.figure(fig_phase.number) plt.plot([x[0] for x in X], [x[1] for x in X])
'asal': (i['asal']), 'nama': i['nama'], 'status': 'mahasiswa', 'usia': int(i['usia']), } datamahasiswa.append(d) return datamahasiswa with open('datamahasiswa.csv','w',newline='') as fileku: kolom = ['asal','nama','status','usia'] tulis = csv.DictWriter(fileku,fieldnames = kolom) tulis.writeheader() tulis.writerows(ambildatamahasiswa()) #membuka file dengan pandas datadosen = pd.read_csv('datadosen.csv') datamahasiswa = pd.read_csv('datamahasiswa.csv') print(datadosen) print(datamahasiswa) #plotting berdasarkan usia plt.bar(datadosen['nama'],datadosen['usia'],color = 'blue') plt.bar(datamahasiswa['nama'],datamahasiswa['usia'],color = 'orange') plt.grid(True) plt.legend(['Dosen','Mahasiswa']) plt.title('Usia Warga Kampus') plt.show()
# Define the policy pol = [0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 3] # Set range for plot of J and J^pi nmax = 10000 pace = 10 ngrid = np.arange(100, nmax, pace) # Estimate J and J^pi j_mcs = exo2.j_mc_estimates(ngrid, pol, mu0_mc, env, Tmax, gamma) j_pi = np.dot(mu0_mc, v_q4) # Plot the result plt.figure() plt.plot(ngrid, j_mcs - j_pi) plt.title("Convergence of MC estimation of Value Function") plt.ylabel("$J - J^{\pi}$") plt.xlabel("MC iterations") # ########################## Question 1.5 ######################################################################### # Params nactions = 4 nstates = 11 # Number of episodes nits = 70 # Exploration parameter eps = 0.1
import matplotlib.pyplot as plt import numpy as np import nltk from nltk.corpus import inaugural from collections import Counter sents_Washington = nltk.tokenize.sent_tokenize( inaugural.raw('1789-Washington.txt')) sents_Kennedy = nltk.tokenize.sent_tokenize(inaugural.raw('1961-Kennedy.txt')) sents_Obama = nltk.tokenize.sent_tokenize(inaugural.raw('2009-Obama.txt')) cnt_Washington = Counter(len(sent.split()) for sent in sents_Washington) cnt_Kennedy = Counter(len(sent.split()) for sent in sents_Kennedy) cnt_Obama = Counter(len(sent.split()) for sent in sents_Obama) print(sorted(cnt_Washington.items(), key=lambda x: [x[1], x[0]], reverse=True)) print(sorted(cnt_Kennedy.items(), key=lambda x: [x[1], x[0]], reverse=True)) print(sorted(cnt_Obama.items(), key=lambda x: [x[1], x[0]], reverse=True)) nstring_Washington = np.array([len(sent.split()) for sent in sents_Washington]) nstring_Kennedy = np.array([len(sent.split()) for sent in sents_Kennedy]) nstring_Obama = np.array([len(sent.split()) for sent in sents_Obama]) plt.hist([nstring_Washington, nstring_Kennedy, nstring_Obama], color=['blue', 'red', 'green'], label=['1789年ワシントン', '1961年ケネディ', '2007年オバマ']) plt.title('1789年ワシントン/1961年ケネディ/2007年オバマ就任演説の文ごとの単語数分布') plt.xlabel('文の単語数') plt.ylabel('出現頻度') plt.legend() plt.show()
def plot_accuracy(df_list): # title font linestyle_list = ['-', ':', '-.', '--'] marker_list = ['.', 'o', 'v', 's'] color_list = ['r', 'b', 'g', 'y'] training_label_list = [ 'Training Acc ', 'Training Acc Early Stopping', 'Training Loss LeakyReLU', 'Training Loss PReLU' ] validation_label_list = [ 'Validation Acc ', 'Validation Acc Early Stopping', 'Validation Loss LeakyReLU', 'Validation Loss PReLU' ] training_loss_label_list = [ 'Training Loss ', 'Training Loss Early Stopping', 'Training Loss LeakyReLU', 'Training Loss PReLU' ] validation_loss_label_list = [ 'Validation Loss ', 'Validation Loss Early Stopping', 'Validation Loss LeakyReLU', 'Validation Loss PReLU' ] title_font = { 'family': 'Times New Roman', 'weight': 'bold', 'size': 18, } # lable_font lable_font = { 'family': 'Times New Roman', 'weight': 'normal', 'size': 12, } # epoch = list(range(1, len(list(df_list[0]['training_accuracy'])) + 1, 5)) for i in range(len(df_list)): df = df_list[i] acc = list(df['training_accuracy'])[0::5] val_acc = list(df['validation_accuracy'])[0::5] epoch = list(range(1, len(list(df['training_accuracy'])) + 1, 5)) print(len(epoch), len(acc)) plt.plot(epoch, acc, color=color_list[i], label=training_label_list[i], linestyle=linestyle_list[0], linewidth=1, markersize=1) plt.plot(epoch, val_acc, color=color_list[i], label=validation_label_list[i], linestyle=linestyle_list[1], linewidth=1, markersize=1) plt.title('Training and validation accuracy', fontdict=title_font) plt.legend(prop=lable_font) plt.xlabel("Epoch", fontdict=lable_font) plt.ylabel("Accuracy", fontdict=lable_font) plt.figure() for i in range(len(df_list)): df = df_list[i] loss = list(df['training_loss'])[0::5] val_loss = list(df['validation_loss'])[0::5] epoch = list(range(1, len(list(df['training_accuracy'])) + 1, 5)) plt.plot(epoch, loss, color=color_list[i], label=training_loss_label_list[i], linestyle=linestyle_list[0], linewidth=1, markersize=1) plt.plot(epoch, val_loss, color=color_list[i], label=validation_loss_label_list[i], linestyle=linestyle_list[1], linewidth=1, markersize=1) plt.title('Training and validation loss', fontdict=title_font) plt.legend(prop=lable_font) plt.xlabel("Epoch", fontdict=lable_font) plt.ylabel("Loss", fontdict=lable_font) plt.show()
#meshgrid is used to specify the pixel/coordinate. start & stop is the starting & ending pixel value of the grid with increment of resolution size(step). #min-1 & max+1 is done to avoid the data points touching the start & end line of the grid. #X_set[:,0] is age column and X_set[:,1] is Salary column #X1 is age, X2 is Salary column plt.contour(X1,X2, classifier.predict(np.array([X1.ravel(),X2.ravel()]).T).reshape(X1.shape), alpha = 0.75, cmap = ListedColormap(('red', 'green'))) plt.xlim(X1.min(), X1.max()) #x & y coordinates range plt.ylim(X2.min(), X2.max()) for i,j in enumerate(np.unique(Y_set)): plt.scatter(X_set[Y_set == j, 0], X_set[Y_set == j, 1], c = ListedColormap(('red', 'green'))(i), label = j) plt.title('Kernal SVM Classifier (Training Set)') plt.xlabel('Age') plt.ylabel('Estimated Salary') plt.legend() plt.show() #Visualising the Test set result from matplotlib.colors import ListedColormap X_set, Y_set = X_test, Y_test #local assignment of 'train' into 'set' helps in reusability X1, X2 = np.meshgrid(np.arange(start = X_set[:,0].min() - 1, stop = X_set[:,0].max() + 1, step =0.01), np.arange(start = X_set[:,0].min() - 1, stop = X_set[:,0].max() + 1, step =0.01)) #meshgrid is used to specify the pixel/coordinate. start & stop is the starting & ending pixel value of the grid with increment of resolution size(step). #min-1 & max+1 is done to avoid the data points touching the start & end line of the grid. #X_set[:,0] is age column and X_set[:,1] is Salary column #X1 is age, X2 is Salary column
# Figure out what arguments to add to the loadtxt function call # so that numbers are loaded into the local function 'data'. # Hint: look for arguments like 'skiprows' and 'delimiter', skip row 0-32 # Check by running: # $ python plot.py raw-data/Sp15_245L_sect-001_group-1_glass.raw # at the command line. plt.plot(datax, datay, color='k', linestyle='-', label=filename + 'Stress vs. Strain') plt.legend(loc='best') plt.xlabel('Strain (MPa)') plt.ylabel('Stress (N)') plt.title('Engineering strain curve') plt.savefig('Slope.png') ## Part 1 # Figure out what columns and rows of data we need to plot # Stress (y-axis) vs Strain (x-axis) # plot raw-data/Sp15_245L_sect-001_group-1_glass.raw # Make sure to include axis labels and units! # plt.plot(xdata,ydata, arguments-to-make-plot-pretty) ## Part 2 # Check to see if your code in part 1 will plot all of the files in raw-data/ # Edit the files (use git liberally here!) to make them more usable ## Part 3
# plt.title(r'horizontal mass flux out of convective region, W$_{{c}}$ = {:4.2f} m/s, block-averaging over ({:2.0f} km)$^2$'.format(Wcrit, db*(np.diff(x)[0])/1e3)) # plt.plot(divs/divbar, z[:-1]/1e3, '-x', color=colors[k], label='{:d} km, averaged over day {:2.1f} to {:2.1f}'.format(domsize, times[0], times[-1])) # plt.xlabel('mass flux relative to vertically-averaged magnitude of mass flux') # plt.ylabel('z (km)') # plt.legend(loc='best') # ax.set_ylim(0, 20) plt.savefig(fout + 'massflux_blkavg_day250_{:d}day_db{:d}Wc{:2.3f}.pdf'.format( nave, db, Wcrit)) plt.figure(6) ax = plt.gcf().gca() if db == 1: plt.title( r'horizontal MSE flux out of convective region, $w_{{c}}$ = {:4.2f} m/s' .format(Wcrit)) else: plt.title( r'horizontal MSE flux out of convective region, $w_{{c}}$ = {:4.2f} m/s, block-averaging over ({:2.0f} km)$^2$' .format(Wcrit, db * (np.diff(x)[0]) / 1e3)) plt.plot(divh / divhbar, z[:-1] / 1e3, '-x', color=colors[k], label='{:d} km, averaged over day {:2.0f} to {:2.0f}'.format( domsize, times[0], times[-1])) plt.xlabel( r'$\frac{(\nabla \cdot \rho{\bf u}h)_{conv}}{\overline{|\nabla \cdot \rho{\bf u}h|}_{conv}}$' )
#!/usr/bin/env python3 """ Plots x -> y1 and x -> y2 as line graphs in the same figure. """ import numpy as np import matplotlib.pyplot as plt x = np.arange(0, 21000, 1000) r = np.log(0.5) t1 = 5730 t2 = 1600 y1 = np.exp((r / t1) * x) y2 = np.exp((r / t2) * x) plt.plot(x, y1, 'r--', x, y2, 'g') plt.xlabel('Time (years)') plt.ylabel('Fraction Remaining') plt.title('Exponential Decay of Radioactive Elements') plt.axis([0, 20000, 0, 1]) plt.legend(['C-14', 'Ra-226']) plt.show()
inplace=True) dataset['citric acid'].fillna(dataset['citric acid'].mean(), inplace=True) dataset['residual sugar'].fillna(dataset['residual sugar'].mean(), inplace=True) dataset['chlorides'].fillna(dataset['chlorides'].mean(), inplace=True) dataset['pH'].fillna(dataset['pH'].mean(), inplace=True) dataset['sulphates'].fillna(dataset['sulphates'].mean(), inplace=True) # In[ ]: # Data Visualization # In[ ]: plt.scatter(x="quality", y="alcohol", data=dataset) plt.title("Wine Quality Prediction") plt.xlabel("Quality") plt.ylabel("Alcohol") plt.plot() # In[ ]: dataset['type'].unique() # In[ ]: quality_mapping = { 3: "Low", 4: "Low", 5: "Medium", 6: "Medium",
# visualising test from matplotlib.colors import ListedColormap X_set, y_set = X_train, y_train X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01), np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01)) plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), alpha = 0.75, cmap = ListedColormap(('r', 'g'))) plt.xlim(X1.min(), X1.max()) plt.ylim(X2.min(), X2.max()) for i, j in enumerate(np.unique(y_set)): plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('r', 'g'))(i), label = j) plt.title('Naive Bayes (Training Set)') plt.xlabel('Age') plt.ylabel('Estimated Salary') plt.legend() plt.show() # visualising test from matplotlib.colors import ListedColormap X_set, y_set = X_test, y_test X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01), np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01)) plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), alpha = 0.75, cmap = ListedColormap(('r', 'g'))) plt.xlim(X1.min(), X1.max()) plt.ylim(X2.min(), X2.max())
print("Interplanar distance: ", str('{:.4f}'.format(dist_plane)), "angstroms") temperature = pu.bragg_temperature(spacing=dist_plane, reflection=reflection, spacing_ref=reference_spacing, temperature_ref=reference_temperature, use_q=False, material="Pt") ######################### # calculate voxel sizes # ######################### dz_realspace = setup_post.wavelength * 1e9 / ( nb_frames * d_rocking_angle * np.pi / 180) # in nm dy_realspace = setup_post.wavelength * 1e9 * sdd / (numy * detector.pixelsize_y ) # in nm dx_realspace = setup_post.wavelength * 1e9 * sdd / (numx * detector.pixelsize_x ) # in nm print('Real space voxel size (z, y, x): ', str('{:.2f}'.format(dz_realspace)), 'nm', str('{:.2f}'.format(dy_realspace)), 'nm', str('{:.2f}'.format(dx_realspace)), 'nm') ################################# # plot image at Bragg condition # ################################# plt.figure() plt.imshow(np.log10(abs(data[int(round(z0)), :, :])), vmin=0, vmax=5) plt.title('Central slice at frame ' + str(int(np.rint(z0)))) plt.colorbar() plt.ioff() plt.show()
SNR_sq = 0.5 * Mode_Volume_Factor/(4.*np.pi**2) * sp.integrate.trapz(int1, y_bin_arr, axis=0) SNR_arr[np.int(bin_number_y),np.int(bin_number_ell)] = np.sqrt(SNR_sq) # print 'kparmin=', kpar_bin_min, 'kperpmin=', kperp_bin_min, np.sqrt(SNR_sq) return kperp_arr,kpar_arr,SNR_arr kperp_arr=SNR_binned(1.27,HI_den_SNR)[0] kpar_arr=SNR_binned(1.27,HI_den_SNR)[1] SNR_arr=SNR_binned(1.27,HI_den_SNR)[2] pylab.pcolormesh(kperp_arr,kpar_arr,SNR_arr) ; cbar=plt.colorbar() cbar.ax.tick_params(labelsize=12) plt.tick_params(axis='both', which='major', labelsize=12) pylab.xlim([np.min(kperp_arr),np.max(kperp_arr)]) ; pylab.ylim([np.min(kpar_arr),np.max(kpar_arr)]) plt.xlabel(r'$k_\perp$',fontsize=12); plt.ylabel(r'$k_\parallel$',fontsize=12); plt.title('Pixel SN for 21-21', x=1.13, y=1.05) pylab.show() ''' def SNR_integrand_mat(ell,z_i,y,S): kperp=ell/chi(z_i) kpar=y/r(z_i) Sarea=15000.*(np.pi/180.)**2 #converting from square degrees to square radians fsky=Sarea/4./np.pi delta_nu_dimless=400./1420. const=2.*fsky/(2.*np.pi)**2*Sarea*delta_nu_dimless integrand_arr=np.array([]) for i in ell: for j in y: cl_21=S(i,z_i,j) integrand=i*(cl_21)**2/(cl_21+Hirax_noise_z_1_interp(i))**2 integrand_arr=np.append(integrand_arr,integrand)
def drawDigit(position, image, title): plt.subplot(*position) plt.imshow(image.reshape(-1, 28), cmap='gray_r') plt.axis('off') plt.title(title)
def graphic_frame3_1(self,main): Label(main.Frame3_1_3_2, text="Image legend",bg="white").grid(row=0,column=0) Label(main.Frame3_1_3_2, text="from").grid(row=1,column=0) self.legend_from_entry = Entry(main.Frame3_1_3_2,width=6) self.legend_from_entry.grid(row=2,column=0) Label(main.Frame3_1_3_2, text="to").grid(row=3,column=0) self.legend_to_entry = Entry(main.Frame3_1_3_2,width=6) self.legend_to_entry.grid(row=4,column=0) self.cmap_var=StringVar() Radiobutton(main.Frame3_1_3_2, text="hot", variable=self.cmap_var, value="hot").grid(row=5,column=0,sticky=W) Radiobutton(main.Frame3_1_3_2, text="cool", variable=self.cmap_var, value="GnBu").grid(row=6,column=0,sticky=W) Radiobutton(main.Frame3_1_3_2, text="gray", variable=self.cmap_var, value="gray").grid(row=7,column=0,sticky=W) Radiobutton(main.Frame3_1_3_2, text="nipy", variable=self.cmap_var, value="nipy_spectral").grid(row=8,column=0,sticky=W) self.cmap_var.set("hot") scrollbar = Scrollbar(main.Frame3_1_1) scrollbar.pack(side = RIGHT, fill=Y) mylist= Listbox(main.Frame3_1_1, yscrollcommand = scrollbar.set) mylist.pack(side = LEFT, fill = BOTH,expand=YES) for i in range(len(self.phi)): mylist.insert(END, str(i+1)+u'. \u03C6='+str(self.phi[i])+" ; "+u'\u03C7='+str(self.chi[i])+u"; \u03C9="+str(float(self.omega[i]))+ ' (.'+str(self.nfile[i]+1)+'.'+str(self.nimage_i[i]+1)+'.)') self.rotate=0 self.flip=0 mylist.bind("<ButtonRelease-1>", lambda event: show_original_image(event,self,main)) scrollbar.config(command = mylist.yview) #----------------affiche the first image valide--------------------------------------- data=read_data_2D_1(0,self,main) data_dim=len(data.shape) if data_dim==2: self.intensity_2D=data elif data_dim==3: self.intensity_2D=data[0] else: self.intensity_2D=[] self.legend_from_entry.insert(0,0) self.legend_to_entry.insert(0,max(map(max, self.intensity_2D))) fig=plt.figure(1,facecolor="0.94") plt.imshow(self.intensity_2D,cmap="hot",origin='lower') plt.title(u'\u03C6='+str(float(self.phi[0]))+" ; "+u'\u03C7='+str(float(self.chi[0]))+u"; \u03C9="+str(float(self.omega[0]))) nrow=self.nrow[0] ncol=self.ncol[0] plt.yticks((1,nrow*0.25,nrow*0.5,nrow*0.75,nrow), ('%0.1f' % self.row_tick[0][0], '%0.1f' % self.row_tick[0][int(nrow*0.25)], '%0.1f' % self.row_tick[0][int(nrow*0.5)], '%0.1f' % self.row_tick[0][int(nrow*0.75)], '%0.1f' % self.row_tick[0][int(nrow-1)])) plt.xticks((1,ncol*0.25,ncol*0.5,ncol*0.75,ncol), ('%0.1f' % self.col_tick[0][0], '%0.1f' % self.col_tick[0][int(ncol*0.25)], '%0.1f' % self.col_tick[0][int(ncol*0.5)], '%0.1f' % self.col_tick[0][int(ncol*0.75)], '%0.1f' % self.col_tick[0][int(ncol-1)])) plt.xlabel("pixel") plt.ylabel("pixel") plt.colorbar( cax=plt.axes([0.9, 0.45, 0.02, 0.4]) ) plt.close() canvas = FigureCanvasTkAgg(fig, main.Frame3_1_2) canvas.get_tk_widget().pack(fill=BOTH, expand=YES) Button(main.Frame3_1_3_1,compound=CENTER, text="Original",bg="white",command=lambda:rotate_0(self,main)).pack() Button(main.Frame3_1_3_1,compound=CENTER, text="Rotate +90°",bg="white",command=lambda:rotate_270(self,main)).pack() Button(main.Frame3_1_3_1,compound=CENTER, text="Rotate -90°",bg="white",command=lambda:rotate_90(self,main)).pack() Button(main.Frame3_1_3_1,compound=CENTER, text="Rotate +180°",bg="white",command=lambda:rotate_180(self,main)).pack() Button(main.Frame3_1_3_1,compound=CENTER, text="Flip horizontal",bg="white",command=lambda:flip_horizontal(self,main)).pack() Button(main.Frame3_1_3_1,compound=CENTER, text="Flip vertical",bg="white",command=lambda:flip_vertical(self,main)).pack() scrollbar = Scrollbar(main.Frame3_1_4) scrollbar.pack(side = RIGHT, fill=BOTH) mylist = Listbox(main.Frame3_1_4, yscrollcommand = scrollbar.set) for i in range(len(self.file_info[0])): mylist.insert(END,str(self.file_info[0][i])) mylist.pack(side = LEFT, fill=BOTH,expand=YES) scrollbar.config( command = mylist.yview )
plt.plot(t1, f(t1), '-b',label='X(t)=e^-t') plt.plot(t2, g(t2), '-r',label='X(t)=e^t') plt.plot(t2, h(t2), 'g-',label='X(t)=e^0=1') plt.legend(loc='best') plt.xlim((-5, 5)) plt.ylim((-1,10)) plt.xlabel('t/s') plt.ylabel('X(t)') ax = plt.gca() ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') ax.spines['bottom'].set_position(('data', 0)) ax.spines['left'].set_position(('data', 0)) plt.title("e^-t,e^t,e^0=1") plt.subplot(222) ax = plt.gca() plt.plot(t2, x(t2), 'r-') plt.xlim((-5, 5)) plt.ylim((-2, 2)) plt.xlabel('t/s') plt.ylabel('X(t)') ax.spines['right'].set_color('none') ax.spines['top'].set_color('none')
#!/usr/bin/env python # -*- coding:utf-8 -*- # author:艾强云 # Email:[email protected] # datetime:2019/4/19 10:54 # software: PyCharm import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 10, 1000) y = np.sin(x) z = np.cos(x**2) plt.figure(figsize=(8, 4)) plt.plot(x, y, label="$sin(x)$", color="red", linewidth=2) plt.plot(x, z, "b--", label="$cos(x^2)$", color="green", linewidth=1.5) plt.xlabel("Time(s)") plt.ylabel("Volt") plt.title("PyPlot First Example") plt.ylim(-1.2, 1.2) plt.legend() plt.show()
# 0から6まで0.1刻みで作成 x = np.arange(0, 6, 0.1) y = np.sin(x) plt.plot(x, y) plt.show() # %% # ラベルなどを設定してグラフ表示 x = np.arange(0, 6, 0.1) y1 = np.sin(x) y2 = np.cos(x) plt.plot(x, y1, label="sin") plt.plot(x, y2, linestyle ="--", label="cos") plt.xlabel("x") plt.ylabel("y") plt.title('sin & cos') plt.legend() plt.show() # %% # 画像の表示 matplotlib.image.imread()を使う import matplotlib.pyplot as plt from matplotlib.image import imread # プロジェクトのルートから実行する場合の相対パス, 別の場所から実行する場合は適宜書き換える img = imread('data/images/british-shorthair.jpeg') plt.imshow(img) plt.show()
plt.scatter(cur_data[:, 0], cur_data[:, 1], marker='o', facecolors='none', edgecolors='black', s=40, label=iris.target_names[i]) test_data = X_test[y_test == i] plt.scatter(test_data[:, 0], test_data[:, 1], marker='s', facecolors='black', edgecolors='black', s=40, label=iris.target_names[i]) y_train_pred = classifier.predict(X_train) accuracy_training = np.mean(y_train_pred.ravel() == y_train.ravel()) * 100 print('Accuracy on training data =', accuracy_training) y_test_pred = classifier.predict(X_test) accuracy_testing = np.mean(y_test_pred.ravel() == y_test.ravel()) * 100 print('Accuracy on testing data =', accuracy_testing) plt.title('GMM classifier') plt.xticks(()) plt.yticks(()) plt.show()
def plot_path(self, paths, path_name): plt.figure(figsize=(8, 4), dpi=120) plt.plot(paths.T, linewidth=1) plt.title('Simulated Paths of Security Price (%s)' % path_name) plt.xlabel('steps') plt.ylabel('Price')
#!/usr/bin/python import matplotlib.pyplot as plt import numpy as np zscore = np.array([0.037113, 0.104134, 0.166243, 0.216293, 0.287822, 0.321248, 0.387036, 0.417454, 0.465838, 0.504883, 0.527515, 0.546570, 0.771566, 0.905882, 1.038765, 1.345971, 1.605462, 2.159268]) pearson = np.array([0.002516, 0.002516, 0.002516, 0.002516, 0.002516, 0.002516, 0.002516, 0.002516, 0.002516, 0.002516, 0.002516, 0.002516, 0.002516, 0.002516, 0.002516, 0.002516, 0.002516, 0.002516]) npmi = np.array([0.030254, 0.030254, 0.030254, 0.030254, 0.030254, 0.030254, 0.030254, 0.030254, 0.030254, 0.030254, 0.030254, 0.030254, 0.030254, 0.030254, 0.030254, 0.030254, 0.030254, 0.030254]) jaccard = np.array([0.005025, 0.005025, 0.005025, 0.005025, 0.005025, 0.005025, 0.005025, 0.005025, 0.005025, 0.005025, 0.005025, 0.005025, 0.005025, 0.005025, 0.005025, 0.005025, 0.005025, 0.005025]) bases = np.array([0.010000, 0.010000, 0.010000, 0.010000, 0.010000, 0.010000, 0.010000, 0.010000, 0.010000, 0.010000, 0.010000, 0.010000, 0.010000, 0.010000, 0.010000, 0.010000, 0.010000, 0.010000]) regions = np.array([1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000]) pairwise = np.array([1.332883, 1.332883, 1.332883, 1.332883, 1.332883, 1.332883, 1.332883, 1.332883, 1.332883, 1.332883, 1.332883, 1.332883, 1.332883, 1.332883, 1.332883, 1.332883, 1.332883, 1.332883]) suptitle = '1% overlap | different regions number | 20 000 000 bp' scale = np.array([1, 10, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 500, 750, 1000, 1750, 2500, 5000]) allStats = [zscore, pearson, npmi, jaccard, bases, regions, pairwise] titles = ['Z-Score', 'Pearson', 'NPMI', 'Jaccard', 'Bases Overlap', 'Regions Overlap', 'Pairwise Enrichment'] plt.figure(1) plt.suptitle(suptitle, fontsize=14, fontweight='bold') for i in range(len(allStats)): plt.subplot(3,3,i+1) plt.title(titles[i]) plt.plot(scale, allStats[i], marker='o') plt.xlim([-100, scale[-1]]) manager = plt.get_current_fig_manager() manager.resize(*manager.window.maxsize()) plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1) plt.show()
# cross validation can provide more general metric of this model and it can enables us to reduce variance of the model. # In[ ]: from sklearn.model_selection import cross_val_score scores = cross_val_score(estimator=pipe_svc, X=X_train, y=y_train, cv=10, n_jobs=1) print('Cross validation scores: %s' % scores) import matplotlib.pyplot as plt plt.title('Cross validation scores') plt.scatter(np.arange(len(scores)), scores) plt.axhline(y=np.mean(scores), color='g') # Mean value of cross validation scores plt.show() # # Learning Curve # # Learning curve enables us decide a model is over fitting to given training data and training under appropriate bias and variance balance. Now we try to plot learning curve of this model. # In[ ]: from sklearn.model_selection import learning_curve train_sizes, train_scores, test_scores = learning_curve( estimator=pipe_svc,
n_samples = 10 X = np.ndarray((n_samples, 2)) X[:, 0] = np.linspace(0, 2 * np.pi, n_samples) X[:, 1] = 1 - 3 * X[:, 0] + random_state.randn(n_samples) mvn = MVN(random_state=0) mvn.from_samples(X) X_test = np.linspace(0, 2 * np.pi, 100) mean, covariance = mvn.predict(np.array([0]), X_test[:, np.newaxis]) plt.figure(figsize=(10, 5)) plt.subplot(1, 3, 1) plt.title("Linear: $p(Y | X) = \mathcal{N}(\mu_{Y|X}, \Sigma_{Y|X})$") plt.scatter(X[:, 0], X[:, 1]) y = mean.ravel() s = covariance.ravel() plt.fill_between(X_test, y - s, y + s, alpha=0.2) plt.plot(X_test, y, lw=2) n_samples = 100 X = np.ndarray((n_samples, 2)) X[:, 0] = np.linspace(0, 2 * np.pi, n_samples) X[:, 1] = np.sin(X[:, 0]) + random_state.randn(n_samples) * 0.1 gmm = GMM(n_components=3, random_state=0) gmm.from_samples(X) Y_gmm = gmm.predict(np.array([0]), X_test[:, np.newaxis])
def plot_spikes(spikes: Dict[str, torch.Tensor], time: Optional[Tuple[int, int]] = None, n_neurons: Optional[Dict[str, Tuple[int, int]]] = None, ims: Optional[List[AxesImage]] = None, axes: Optional[Union[Axes, List[Axes]]] = None, figsize: Tuple[float, float] = (8.0, 4.5)) -> Tuple[List[AxesImage], List[Axes]]: # language=rst """ Plot spikes for any group(s) of neurons. :param spikes: Mapping from layer names to spiking data. :param time: Plot spiking activity of neurons in the given time range. Default is entire simulation time. :param n_neurons: Plot spiking activity of neurons in the given range of neurons. Default is all neurons. :param ims: Used for re-drawing the plots. :param axes: Used for re-drawing the plots. :param figsize: Horizontal, vertical figure size in inches. :return: ``ims, axes``: Used for re-drawing the plots. """ n_subplots = len(spikes.keys()) if n_neurons is None: n_neurons = {} spikes = {k: v.view(-1, v.size(-1)) for (k, v) in spikes.items()} if time is None: # Set it for entire duration for key in spikes.keys(): time = (0, spikes[key].shape[1]) break # Use all neurons if no argument provided. for key, val in spikes.items(): if key not in n_neurons.keys(): n_neurons[key] = (0, val.shape[0]) if ims is None: fig, axes = plt.subplots(n_subplots, 1, figsize=figsize) ims = [] if n_subplots == 1: for datum in spikes.items(): ims.append(axes.imshow(spikes[datum[0]][n_neurons[datum[0]][0]:n_neurons[datum[0]][1], time[0]:time[1]], cmap='binary')) args = (datum[0], n_neurons[datum[0]][0], n_neurons[datum[0]][1], time[0], time[1]) plt.title('%s spikes for neurons (%d - %d) from t = %d to %d ' % args) plt.xlabel('Simulation time'); plt.ylabel('Neuron index') axes.set_aspect('auto') else: for i, datum in enumerate(spikes.items()): ims.append(axes[i].imshow(datum[1].detach().clone().cpu().numpy() [n_neurons[datum[0]][0]:n_neurons[datum[0]][1], time[0]:time[1]], cmap='binary')) args = (datum[0], n_neurons[datum[0]][0], n_neurons[datum[0]][1], time[0], time[1]) axes[i].set_title('%s spikes for neurons (%d - %d) from t = %d to %d ' % args) for ax in axes: ax.set_aspect('auto') plt.setp(axes, xticks=[], yticks=[], xlabel='Simulation time', ylabel='Neuron index') plt.tight_layout() else: if n_subplots == 1: for datum in spikes.items(): ims[0].set_data(datum[1].detach().clone().cpu().numpy() [n_neurons[datum[0]][0]:n_neurons[datum[0]][1], time[0]:time[1]]) ims[0].autoscale() args = (datum[0], n_neurons[datum[0]][0], n_neurons[datum[0]][1], time[0], time[1]) axes.set_title('%s spikes for neurons (%d - %d) from t = %d to %d ' % args) else: for i, datum in enumerate(spikes.items()): ims[i].set_data(datum[1].detach().clone().cpu().numpy() [n_neurons[datum[0]][0]:n_neurons[datum[0]][1], time[0]:time[1]]) ims[i].autoscale() args = (datum[0], n_neurons[datum[0]][0], n_neurons[datum[0]][1], time[0], time[1]) axes[i].set_title('%s spikes for neurons (%d - %d) from t = %d to %d ' % args) plt.draw() return ims, axes
def master(D, expCounter, sideBilinear, weightClass, eta_Latent, eta_RowBias, eta_LatentScaler, eta_Bilinear, epochs, alpha, epsilon, testSet): v_training_set = [] # set of indexes of the D where exists and edge testOnes = [] for index in range(0, len(testSet[0, :])): if testSet[2, index] == 1.0: testOnes.append(index) for index in range(0, len(D[0, :])): if D[2, index] == 1.0: v_training_set.append(index) accuracy = [] precision = [] recall = [] AUC = [] F1 = [] HR = [] accuracyAdv = [] precisionAdv = [] recallAdv = [] AUCAdv = [] F1Adv = [] HRAdv = [] # newepochs is the number of epochs for the sgd_adv learning if epochs % 2 == 0: Newepochs = int(epochs / 2) else: Newepochs = int((epochs + 1) / 2) # plot parameters aucSamples = int(math.ceil((epochs + Newepochs) / aucStep)) aucSamplesAdv = int(math.ceil(Newepochs / aucStep)) plot0 = np.random.rand(aucSamples - 1, 1) plotAdv = np.random.rand(aucSamplesAdv - 1, 1) plot0final = np.random.rand(aucSamples - 1) plotAdvfinal = np.random.rand(aucSamplesAdv - 1) plot0HR = np.random.rand(aucSamples - 1, 1) plotAdvHR = np.random.rand(aucSamplesAdv - 1, 1) plot0finalHR = np.random.rand(aucSamples - 1) plotAdvfinalHR = np.random.rand(aucSamplesAdv - 1) trainSet = np.copy(D[:, :]) testSet = np.copy(TestSet[:, :]) U, UBias, ULatentScaler, WBilinear, plot0, plot0HR, plotPoint = sgd( trainSet, sideBilinear, weightClass, eta_Latent, eta_RowBias, eta_LatentScaler, eta_Bilinear, epochs, plot0, plot0HR, testSet) UAdv, UBiasAdv, ULatentScalerAdv, WBilinearAdv, plotAdv, plotAdvHR = sgd_adv( trainSet, sideBilinear, U, UBias, ULatentScaler, WBilinear, eta_Latent, eta_LatentScaler, eta_Bilinear, eta_RowBias, Newepochs, plotAdv, plotAdvHR, testSet, alpha, epsilon) UCon, UBiasCon, ULatentScalerCon, WBilinearCon, plot0, plot0HR = sgd_continue( trainSet, sideBilinear, U, UBias, ULatentScaler, WBilinear, eta_Latent, eta_LatentScaler, eta_Bilinear, eta_RowBias, Newepochs, plot0, plot0HR, testSet, plotPoint) predictionAdv = predict(UAdv, UBiasAdv, ULatentScalerAdv, WBilinearAdv, sideBilinear) # generate prediction matrix predictionCon = predict(UCon, UBiasCon, ULatentScalerCon, WBilinearCon, sideBilinear) # generate prediction matrix accAdv, recAdv, precAdv, aucAdv, f1Adv = test(predictionAdv, testSet) accCon, recCon, precCon, aucCon, f1Con = test(predictionCon, testSet) testOnes = [] for index in range(0, len(testSet[0, :])): if testSet[2, index] == 1.0: testOnes.append(index) hrAdv = hitratio(UAdv, ULatentScalerAdv, UBiasAdv, WBilinearAdv, testSet, testOnes) hrCon = hitratio(UCon, ULatentScalerCon, UBiasCon, WBilinearCon, testSet, testOnes) accuracyAdv.append(accAdv) recallAdv.append(recAdv) precisionAdv.append(precAdv) AUCAdv.append(aucAdv) F1Adv.append(f1Adv) HRAdv.append(hrAdv) accuracy.append(accCon) recall.append(recCon) precision.append(precCon) AUC.append(aucCon) F1.append(f1Con) HR.append(hrCon) for i in range(0, len(plot0[:, 0])): plot0final[i] = np.mean(plot0[i, :]) plot0finalHR[i] = np.mean(plot0HR[i, :]) for i in range(0, len(plotAdv[:, 0])): plotAdvfinal[i] = np.mean(plotAdv[i, :]) plotAdvfinalHR[i] = np.mean(plotAdvHR[i, :]) # Plot the AUC plotString = str(expCounter) + '_AUC_plot.jpg' #'./exportCurves/' + x0 = np.arange(0, len(plot0[:, 0])) x0 = aucStep * (x0 + 1) y0 = plot0final xA = np.arange(0, len(plotAdv[:, 0])) xA = (aucStep * (xA + 1)) + epochs yA = plotAdvfinal fig = plt.figure(figsize=(10, 5)) plt.plot(x0, y0, label="MF") plt.plot(xA, yA, label="AMF") # Labeling the X-axis plt.xlabel('Iterations') # Labeling the Y-axis plt.ylabel('AUC') plt.title('AUC trend comparison') plt.legend() plt.ylim(bottom=0, top=1) #Saving the plot as an image fig.savefig(plotString, bbox_inches='tight', dpi=150) # Plot the HR plotStringHR = str(expCounter) + '_HR_plot.jpg' #'./exportCurves/' + x0HR = np.arange(0, len(plot0HR[:, 0])) x0HR = aucStep * (x0HR + 1) y0HR = plot0finalHR xAHR = np.arange(0, len(plotAdvHR[:, 0])) xAHR = (aucStep * (xAHR + 1)) + epochs yAHR = plotAdvfinalHR fig = plt.figure(figsize=(10, 5)) plt.plot(x0HR, y0HR, label="MF") plt.plot(xAHR, yAHR, label="AMF") # Labeling the X-axis plt.xlabel('Iterations') # Labeling the Y-axis plt.ylabel('HR@30') plt.title('Hit Ratio trend comparison') plt.legend() plt.ylim(bottom=0, top=1) #Saving the plot as an image fig.savefig(plotStringHR, bbox_inches='tight', dpi=150) print("MODEL: ", expCounter) print("eta_Latent: ", eta_Latent) print("eta_RowBias: ", eta_RowBias) print("eta_LatentScaler: ", eta_LatentScaler) print("eta_Bilinear: ", eta_Bilinear) print("Epochs per Kfold:", (epochs - 1)) print("-----------------------------------") print(" METRICS for MODEL: ", expCounter) print("--- Matrix Factorization ----") print("Accuracy:", accCon) print("Precision:", precCon) print("Recall:", recCon) print("F1:", f1Con) print("AUC:", aucCon) print("HR@30:", hrCon) print("---- Adversarial Matrix Factorization ----") print("Accuracy:", accAdv) print("Precision:", precAdv) print("Recall:", recAdv) print("F1:", f1Adv) print("AUC:", aucAdv) print("HR@30:", hrAdv) expString = './exportFiles/' + str(expCounter) + '_file.pkl' exportObject = export(eta_Latent, eta_RowBias, eta_LatentScaler, eta_Bilinear, (epochs - 1), alpha, epsilon, UCon, ULatentScalerCon, UBiasCon, WBilinearCon, UAdv, ULatentScalerAdv, UBiasAdv, WBilinearAdv, x0HR, y0HR, xAHR, yAHR) f = open(expString, 'wb') pickle.dump(exportObject, f, -1) f.close() return
from keras import optimizers from keras.callbacks import CSVLogger import os from sklearn.model_selection import StratifiedKFold from keras.optimizers import SGD import sys import time df_ge = pd.read_csv('IAF2.csv', sep=',') print(df_ge.tail()) plt.figure() plt.plot(df_ge["Open"]) plt.plot(df_ge["High"]) plt.plot(df_ge["Low"]) plt.plot(df_ge["Close"]) plt.title('Iran Aluminium stock price history') plt.ylabel('Price (USD)') plt.xlabel('Days') plt.legend(['Open','High','Low','Close'], loc='upper left') plt.show() plt.figure() plt.plot(df_ge["Volume"]) plt.title('Iran Aluminium stock volume history') plt.ylabel('Volume') plt.xlabel('Days') plt.show() print("checking if any null values are present\n", df_ge.isna().sum()) #stime = time.time() train_cols = ["Open","High","Low","Close","Volume"] df_train, df_test = train_test_split(df_ge, train_size=0.8, test_size=0.2, shuffle=False)
# predict (image_lr, image_hr, _, pmaps) = dataset[0] x = _normalize(image_lr[np.newaxis]) x = torch.from_numpy(x).float().to(args.device) with torch.no_grad(): y_c, prs, out = net(x) y_c = _denormalize(y_c.cpu())[0].astype('uint8') prs = _denormalize(prs.cpu())[0].astype('uint8') out = _denormalize(out.cpu())[0].astype('uint8') # plot images fig = plt.figure(figsize=(10,3)) fig.add_subplot(1, 4, 1) plt.title('Target') plt.imshow(image_hr, vmin=0, vmax=255) fig.add_subplot(1, 4, 2) plt.title('Input (Bicubic)') _img = image_lr plt.imshow(_img, vmin=0, vmax=255) psnr = compare_psnr(image_hr, _img) ssim = compare_ssim(image_hr, _img, multichannel=True) plt.xlabel('PSNR=%.2f\nSSIM=%.4f' % (psnr, ssim)) fig.add_subplot(1, 4, 3) plt.title('FSRNet(y_c)') _img = y_c plt.imshow(_img, vmin=0, vmax=255) psnr = compare_psnr(image_hr, _img)
def simple_contour_plot (data, lons, lats, datacrs= ccrs.PlateCarree(), mapcrs= ccrs.PlateCarree(), title = None, data_units = '', sequential = False, diverging = False, colormap='', cbar_orientation = 'horizontal'): '''A plug and chug quick contour plot for spatial data. Parameters ---------- data: string lons: float lats: float datacrs: string, optional What projection data comes in. Note: If data comes in lons & lats, means that it is PlateCarree(). https://scitools.org.uk/cartopy/docs/latest/crs/projections.html#cartopy-projections mapcrs: string, optional What projection you want output to be in. title: string, optional data_units: string, optional Used to label the colorbar values. colormap: string, optional https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html cbar_orientation: string, optional Color bar orientation, either horizonatal or vertical. Returns ------- Contour plot of data within specified lat and lon. ''' # clevs = _nice_intervals(data, 10) fig = plt.figure(figsize=(7, 5)) ax = fig.add_subplot(1, 1, 1, projection=mapcrs) if sequential == True: colorbar = 'cool' else: colobar = 'YlOrRd' if diverging == True: colorbar = 'RdBu' else: colobar = 'YlOrRd' p = ax.contourf(lons, lats, data, transform=datacrs, cmap = colormap, extend='both' # , levels=clevs ) ax.coastlines() ax.gridlines() plt.title(title) cbar = plt.colorbar(p, orientation=cbar_orientation, shrink=0.85, pad=0.05, label=data_units) plt.show()
def plot_voltages(voltages: Dict[str, torch.Tensor], ims: Optional[List[AxesImage]] = None, axes: Optional[List[Axes]] = None, time: Tuple[int, int] = None, n_neurons: Optional[Dict[str, Tuple[int, int]]] = None, cmap: Optional[str] = 'jet', plot_type: str = 'color', threshold: Dict[str,float] = None, figsize: Tuple[float, float] = (8.0, 4.5),) -> Tuple[List[AxesImage], List[Axes]]: # language=rst """ Plot voltages for any group(s) of neurons. :param voltages: Contains voltage data by neuron layers. :param ims: Used for re-drawing the plots. :param axes: Used for re-drawing the plots. :param time: Plot voltages of neurons in given time range. Default is entire simulation time. :param n_neurons: Plot voltages of neurons in given range of neurons. Default is all neurons. :param cmap: Matplotlib colormap to use. :param figsize: Horizontal, vertical figure size in inches. :param plot_type: The way how to draw graph. 'color' for pcolormesh, 'line' for curved lines. :param threshold: Threshold of each layer. :return: ``ims, axes``: Used for re-drawing the plots. """ n_subplots = len(voltages.keys()) if time is None: for key in voltages.keys(): time = (0, voltages[key].shape[1]) break if n_neurons is None: n_neurons = {} for key, val in voltages.items(): if key not in n_neurons.keys(): n_neurons[key] = (0, val.shape[0]) if not ims: fig, axes = plt.subplots(n_subplots, 1, figsize=figsize) ims = [] if n_subplots == 1: # Plotting only one image for v in voltages.items(): if plot_type == 'line': ims.append(axes.plot(v[1].detach().clone().cpu().numpy() [n_neurons[v[0]][0]:n_neurons[v[0]][1], time[0]:time[1]].cpu().numpy().T)) if threshold is not None: ims.append(axes.axhline(y=threshold[v[0]], c='r', linestyle='--')) else: ims.append(axes.pcolormesh(v[1][n_neurons[v[0]][0]:n_neurons[v[0]][1], time[0]:time[1]], cmap=cmap)) args = (v[0], n_neurons[v[0]][0], n_neurons[v[0]][1], time[0], time[1]) plt.title('%s voltages for neurons (%d - %d) from t = %d to %d ' % args) plt.xlabel('Time (ms)'); plt.ylabel('Neuron index') axes.set_aspect('auto') else: # Plot each layer at a time for i, v in enumerate(voltages.items()): if plot_type == 'line': ims.append(axes[i].plot(v[1].detach().clone().cpu().numpy() [n_neurons[v[0]][0]:n_neurons[v[0]][1], time[0]:time[1]].cpu().numpy().T)) if threshold is not None: ims.append(axes[i].axhline(y=threshold[v[0]], c='r', linestyle='--')) else: ims.append(axes[i].matshow(v[1].detach().clone().cpu().numpy() [n_neurons[v[0]][0]:n_neurons[v[0]][1], time[0]:time[1]], cmap=cmap)) args = (v[0], n_neurons[v[0]][0], n_neurons[v[0]][1], time[0], time[1]) axes[i].set_title('%s voltages for neurons (%d - %d) from t = %d to %d ' % args) for ax in axes: ax.set_aspect('auto') plt.setp(axes, xlabel='Simulation time', ylabel='Neuron index') plt.tight_layout() else: # Plotting figure given if n_subplots == 1: # Plotting only one image for v in voltages.items(): axes.clear() if plot_type == 'line': axes.plot(v[1].detach().clone().cpu().numpy() [n_neurons[v[0]][0]:n_neurons[v[0]][1], time[0]:time[1]].cpu().numpy().T) if threshold is not None: axes.axhline(y=threshold[v[0]], c='r', linestyle='--') else: axes.matshow(v[1].detach().clone().cpu().numpy() [n_neurons[v[0]][0]:n_neurons[v[0]][1], time[0]:time[1]], cmap=cmap) args = (v[0], n_neurons[v[0]][0], n_neurons[v[0]][1], time[0], time[1]) axes.set_title('%s voltages for neurons (%d - %d) from t = %d to %d ' % args) axes.set_aspect('auto') else: # Plot each layer at a time for i, v in enumerate(voltages.items()): axes[i].clear() if plot_type == 'line': axes[i].plot(v[1].detach().clone().cpu().numpy() [n_neurons[v[0]][0]:n_neurons[v[0]][1], time[0]:time[1]].cpu().numpy().T) if threshold is not None: axes[i].axhline(y=threshold[v[0]], c='r', linestyle='--') else: axes[i].matshow(v[1].detach().clone().cpu().numpy() [n_neurons[v[0]][0]:n_neurons[v[0]][1], time[0]:time[1]], cmap=cmap) args = (v[0], n_neurons[v[0]][0], n_neurons[v[0]][1], time[0], time[1]) axes[i].set_title('%s voltages for neurons (%d - %d) from t = %d to %d ' % args) for ax in axes: ax.set_aspect('auto') if plot_type == 'color': plt.setp(axes, xlabel='Simulation time', ylabel='Neuron index') elif plot_type == 'line': plt.setp(axes, xlabel='Simulation time', ylabel='Voltage') plt.tight_layout() return ims, axes