def _plot_histogram(self, data, number_of_devices=1, preamp_timeout=1253): if number_of_devices == 0: return data = np.array(data) plt.figure(3) plt.ioff() plt.get_current_fig_manager().window.wm_geometry("800x550+700+25") plt.clf() if number_of_devices == 1: plt.hist(data[0,:], bins=preamp_timeout, range=(1, preamp_timeout-1), color='b') elif number_of_devices == 2: plt.hist(data[0,:], bins=preamp_timeout, range=(1, preamp_timeout-1), color='r', label='JPM A') plt.hist(data[1,:], bins=preamp_timeout, range=(1, preamp_timeout-1), color='b', label='JPM B') plt.legend() elif number_of_devices > 2: raise Exception('Histogram plotting for more than two ' + 'devices is not implemented.') plt.xlabel('Timing Information [Preamp Time Counts]') plt.ylabel('Counts') plt.xlim(0, preamp_timeout) plt.draw() plt.pause(0.05)
def showPairDeformationDist(model, coords, ind1, ind2, *args, **kwargs): """Show distribution of deformations in distance contributed by each mode for selected pair of residues *ind1* *ind2* using :func:`~matplotlib.pyplot.plot`. """ import matplotlib import matplotlib.pyplot as plt if not isinstance(model, NMA): raise TypeError('model must be a NMA instance, ' 'not {0}'.format(type(model))) elif not model.is3d(): raise TypeError('model must be a 3-dimensional NMA instance') elif len(model) == 0: raise ValueError('model must have normal modes calculated') elif model.getStiffness() is None: raise ValueError('model must have stiffness matrix calculated') d_pair = calcPairDeformationDist(model, coords, ind1, ind2) with plt.style.context('fivethirtyeight'): matplotlib.rcParams['font.size'] = '16' fig = plt.figure(num=None, figsize=(12,8), dpi=100, facecolor='w') #plt.title(str(model)) plt.plot(d_pair[0], d_pair[1], 'k-', linewidth=1.5, *args, **kwargs) plt.xlabel('mode (k)', fontsize = '18') plt.ylabel('d$^k$' '($\AA$)', fontsize = '18') if SETTINGS['auto_show']: showFigure() return plt.show
def showMechStiff(model, coords, *args, **kwargs): """Show mechanical stiffness 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 arange = np.arange(model.numAtoms()) model.buildMechStiff(coords) if not 'origin' in kwargs: kwargs['origin'] = 'lower' if 'jet_r' in kwargs: import matplotlib.cm as plt kwargs['jet_r'] = 'cmap=cm.jet_r' MechStiff = model.getStiffness() matplotlib.rcParams['font.size'] = '14' fig = plt.figure(num=None, figsize=(10,8), dpi=100, facecolor='w') show = plt.imshow(MechStiff, *args, **kwargs), plt.colorbar() plt.clim(math.floor(np.min(MechStiff[np.nonzero(MechStiff)])), \ round(np.amax(MechStiff),1)) #plt.title('Mechanical Stiffness Matrix')# for {0}'.format(str(model))) plt.xlabel('Indices', fontsize='16') plt.ylabel('Indices', fontsize='16') if SETTINGS['auto_show']: showFigure() return show
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 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 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 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 showScaledSqFlucts(modes, *args, **kwargs): """Show scaled square fluctuations using :func:`~matplotlib.pyplot.plot`. Modes or mode sets given as additional arguments will be scaled to have the same mean squared fluctuations as *modes*.""" import matplotlib.pyplot as plt sqf = calcSqFlucts(modes) mean = sqf.mean() args = list(args) modesarg = [] i = 0 while i < len(args): if isinstance(args[i], (VectorBase, ModeSet, NMA)): modesarg.append(args.pop(i)) else: i += 1 show = [plt.plot(sqf, *args, label=str(modes), **kwargs)] plt.xlabel('Indices') plt.ylabel('Square fluctuations') for modes in modesarg: sqf = calcSqFlucts(modes) scalar = mean / sqf.mean() show.append(plt.plot(sqf * scalar, *args, label='{0} (x{1:.2f})'.format(str(modes), scalar), **kwargs)) if SETTINGS['auto_show']: showFigure() return show
def showNormedSqFlucts(modes, *args, **kwargs): """Show normalized square fluctuations via :func:`~matplotlib.pyplot.plot`. """ import matplotlib.pyplot as plt sqf = calcSqFlucts(modes) args = list(args) modesarg = [] i = 0 while i < len(args): if isinstance(args[i], (VectorBase, ModeSet, NMA)): modesarg.append(args.pop(i)) else: i += 1 show = [plt.plot(sqf/(sqf**2).sum()**0.5, *args, label='{0}'.format(str(modes)), **kwargs)] plt.xlabel('Indices') plt.ylabel('Square fluctuations') for modes in modesarg: sqf = calcSqFlucts(modes) show.append(plt.plot(sqf/(sqf**2).sum()**0.5, *args, label='{0}'.format(str(modes)), **kwargs)) if SETTINGS['auto_show']: showFigure() return show
def showCumulFractVars(modes, *args, **kwargs): """Show fraction of variances of *modes* using :func:`~matplotlib.pyplot. plot`. Note that mode indices are incremented by 1. See also :func:`.showFractVars` function.""" import matplotlib.pyplot as plt if not isinstance(modes, (Mode, NMA, ModeSet)): raise TypeError('modes must be a Mode, NMA, or ModeSet instance, ' 'not {0}'.format(type(modes))) if isinstance(modes, Mode): indices = modes.getIndices() + 0.5 modes = [modes] elif isinstance(modes, ModeSet): indices = modes.getIndices() + 0.5 else: indices = np.arange(len(modes)) + 0.5 fracts = calcFractVariance(modes).cumsum() show = plt.plot(indices, fracts, *args, **kwargs) axis = list(plt.axis()) axis[0] = 0.5 axis[2] = 0 axis[3] = 1 plt.axis(axis) plt.xlabel('Mode index') plt.ylabel('Fraction of variance') if SETTINGS['auto_show']: showFigure() return show
def showOverlapTable(modes_x, modes_y, **kwargs): """Show overlap table using :func:`~matplotlib.pyplot.pcolor`. *modes_x* and *modes_y* are sets of normal modes, and correspond to x and y axes of the plot. Note that mode indices are incremented by 1. List of modes is assumed to contain a set of contiguous modes from the same model. Default arguments for :func:`~matplotlib.pyplot.pcolor`: * ``cmap=plt.cm.jet`` * ``norm=plt.normalize(0, 1)``""" import matplotlib.pyplot as plt overlap = abs(calcOverlap(modes_y, modes_x)) if overlap.ndim == 0: overlap = np.array([[overlap]]) elif overlap.ndim == 1: overlap = overlap.reshape((modes_y.numModes(), modes_x.numModes())) cmap = kwargs.pop('cmap', plt.cm.jet) norm = kwargs.pop('norm', plt.normalize(0, 1)) show = (plt.pcolor(overlap, cmap=cmap, norm=norm, **kwargs), plt.colorbar()) x_range = np.arange(1, modes_x.numModes() + 1) plt.xticks(x_range-0.5, x_range) plt.xlabel(str(modes_x)) y_range = np.arange(1, modes_y.numModes() + 1) plt.yticks(y_range-0.5, y_range) plt.ylabel(str(modes_y)) plt.axis([0, modes_x.numModes(), 0, modes_y.numModes()]) if SETTINGS['auto_show']: showFigure() return show
def plot(self, standardized=False, **kwargs): """ standardized: standardize each estimated coefficient and confidence interval endpoints by the standard error of the estimate. """ from matplotlib import pyplot as plt ax = kwargs.get('ax', None) or plt.figure().add_subplot(111) yaxis_locations = range(len(self.hazards_.columns)) summary = self.summary lower_bound = self.confidence_intervals_.loc['lower-bound'].copy() upper_bound = self.confidence_intervals_.loc['upper-bound'].copy() hazards = self.hazards_.values[0].copy() if standardized: se = summary['se(coef)'] lower_bound /= se upper_bound /= se hazards /= se order = np.argsort(hazards) ax.scatter(upper_bound.values[order], yaxis_locations, marker='|', c='k') ax.scatter(lower_bound.values[order], yaxis_locations, marker='|', c='k') ax.scatter(hazards[order], yaxis_locations, marker='o', c='k') ax.hlines(yaxis_locations, lower_bound.values[order], upper_bound.values[order], color='k', lw=1) tick_labels = [c + significance_code(p).strip() for (c, p) in summary['p'][order].iteritems()] plt.yticks(yaxis_locations, tick_labels) plt.xlabel("standardized coef" if standardized else "coef") return ax
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 do_plot(mode, content, wide): global style style.apply(mode, content, wide) data = np.load("data/prr_AsAu_%s%s.npz"%(content, wide)) AU, TAU = np.meshgrid(-data["Au_range_dB"], data["tau_range"]) Zu = data["PRR_U"] Zs = data["PRR_S"] assert TAU.shape == AU.shape == Zu.shape, "The inputs TAU, AU, PRR_U must have the same shape for plotting!" plt.clf() if mode in ("sync",): # Plot the inverse power ratio, sync signal is stronger for positive ratios CSf = plt.contourf(TAU, AU, Zs, levels=(0.0, 0.2, 0.4, 0.6, 0.8, 0.9, 1.0), colors=("1.0", "0.75", "0.5", "0.25", "0.15", "0.0"), origin="lower") CS2 = plt.contour(CSf, colors = ("r",)*5+("w",), linewidths=(0.75,)*5+(1.0,), origin="lower", hold="on") else: CSf = plt.contourf(TAU, AU, Zs, levels=(0.0, 0.2, 0.4, 0.6, 0.8, 0.9, 1.0), colors=("1.0", "0.75", "0.5", "0.25", "0.15", "0.0"), origin="lower") CS2f = plt.contour(CSf, levels=(0.0, 0.2, 0.4, 0.6, 0.8, 1.0), colors=4*("r",)+("w",), linewidths=(0.75,)*4+(1.0,), origin="lower", hold="on") #CS2f = plt.contour(TAU, -AU, Zu, levels=(0.9, 1.0), colors=("0.0",), linewidths=(1.0,), origin="lower", hold="on") if content in ("unif",): CSu = plt.contourf(TAU, AU, Zu, levels=(0.2, 1.0), hatches=("////",), colors=("0.75",), origin="lower") CS2 = plt.contour(CSu, levels=(0.2,), colors = ("r",), linewidths=(1.0,), origin="lower", hold="on") style.annotate(mode, content, wide) plt.axis([data["tau_range"][0], data["tau_range"][-1], -data["Au_range_dB"][-1], -data["Au_range_dB"][0]]) plt.ylabel(r"Signal power ratio ($\mathrm{SIR}$)", labelpad=2) plt.xlabel(r"Time offset $\tau$ ($/T$)", labelpad=2) plt.savefig("pdf/prrc2_%s_%s%s_z.pdf"%(mode, content, wide))
def scree_plot(pca_obj, fname=None): ''' Scree plot for variance & cumulative variance by component from PCA. Arguments: - pca_obj: a fitted sklearn PCA instance - fname: path to write plot to file Output: - scree plot ''' components = pca_obj.n_components_ variance = pca.explained_variance_ratio_ plt.figure() plt.plot(np.arange(1, components + 1), np.cumsum(variance), label='Cumulative Variance') plt.plot(np.arange(1, components + 1), variance, label='Variance') plt.xlim([0.8, components]); plt.ylim([0.0, 1.01]) plt.xlabel('No. Components', labelpad=11); plt.ylabel('Variance Explained', labelpad=11) plt.legend(loc='best') plt.tight_layout() if fname is not None: plt.savefig(fname) plt.close() else: plt.show() return
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 plotAlphas(datasetNames, sampleSizes, foldsSet, cvScalings, sampleMethods, fileNameSuffix): """ Plot the variation in the error with alpha for penalisation. """ for i, datasetName in enumerate(datasetNames): #plt.figure(i) 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) foldInd = 4 for i in range(sampleSizes.shape[0]): plt.plot(cvScalings, meanMeasures[i, foldInd, 2:8], next(linecycler), label="m="+str(sampleSizes[i])) plt.xlabel("Alpha") plt.ylabel('Error') xmin, xmax = cvScalings[0], cvScalings[-1] plt.xlim((xmin,xmax)) plt.legend(loc="upper left") plt.show()
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 scatter_time_vs_s(time, norm, point_labels, title): plt.figure() size = 100 for i, l in enumerate(sorted(norm.keys())): if l is not "fbpca": plt.scatter(time[l], norm[l], label=l, marker='o', c='b', s=size) for label, x, y in zip(point_labels, list(time[l]), list(norm[l])): plt.annotate(label, xy=(x, y), xytext=(0, -80), textcoords='offset points', ha='right', arrowprops=dict(arrowstyle="->", connectionstyle="arc3"), va='bottom', size=11, rotation=90) else: plt.scatter(time[l], norm[l], label=l, marker='^', c='red', s=size) for label, x, y in zip(point_labels, list(time[l]), list(norm[l])): plt.annotate(label, xy=(x, y), xytext=(0, 30), textcoords='offset points', ha='right', arrowprops=dict(arrowstyle="->", connectionstyle="arc3"), va='bottom', size=11, rotation=90) plt.legend(loc="best") plt.suptitle(title) plt.ylabel("norm discrepancy") plt.xlabel("running time [s]")
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 plotIterationResult(train_err_list): x = range(1,len(train_err_list) + 1) fig = plt.figure() plt.plot(x,train_err_list) plt.xlabel('iterations') plt.ylabel('training error') plt.show()
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 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 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 scatter(x, y, equal=False, xlabel=None, ylabel=None, xinvert=False, yinvert=False): """ Plot a scatter with simple formatting options """ plt.scatter(x, y, 200, color=[0.3, 0.3, 0.3], edgecolors="white", linewidth=1, zorder=2) sns.despine() if xlabel: plt.xlabel(xlabel) if ylabel: plt.ylabel(ylabel) if equal: plt.axes().set_aspect("equal") plt.plot([0, max([x.max(), y.max()])], [0, max([x.max(), y.max()])], color=[0.6, 0.6, 0.6], zorder=1) bmin = min([x.min(), y.min()]) bmax = max([x.max(), y.max()]) rng = abs(bmax - bmin) plt.xlim([bmin - rng * 0.05, bmax + rng * 0.05]) plt.ylim([bmin - rng * 0.05, bmax + rng * 0.05]) else: xrng = abs(x.max() - x.min()) yrng = abs(y.max() - y.min()) plt.xlim([x.min() - xrng * 0.05, x.max() + xrng * 0.05]) plt.ylim([y.min() - yrng * 0.05, y.max() + yrng * 0.05]) if xinvert: plt.gca().invert_xaxis() if yinvert: plt.gca().invert_yaxis()
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 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 tuning(x, y, err=None, smooth=None, ylabel=None, pal=None): """ Plot a tuning curve """ if smooth is not None: xs, ys = smoothfit(x, y, smooth) plt.plot(xs, ys, linewidth=4, color="black", zorder=1) else: ys = asarray([0]) if pal is None: pal = sns.color_palette("husl", n_colors=len(x) + 6) pal = pal[2 : 2 + len(x)][::-1] plt.scatter(x, y, s=300, linewidth=0, color=pal, zorder=2) if err is not None: plt.errorbar(x, y, yerr=err, linestyle="None", ecolor="black", zorder=1) plt.xlabel("Wall distance (mm)") plt.ylabel(ylabel) plt.xlim([-2.5, 32.5]) errTmp = err errTmp[isnan(err)] = 0 rng = max([nanmax(ys), nanmax(y + errTmp)]) plt.ylim([0 - rng * 0.1, rng + rng * 0.1]) plt.yticks(linspace(0, rng, 3)) plt.xticks(range(0, 40, 10)) sns.despine() return rng
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)
beta = 0.1 w = np.linspace(-5, 10, 200) matsu_spectrum = spectrum_matsubara(w, coup_strength, bath_broad, bath_freq, beta) total_spectrum = spectrum(w, coup_strength, bath_broad, bath_freq, beta) nonmatsu_spectrum = spectrum_non_matsubara(w, coup_strength, bath_broad, bath_freq, beta) nonmatsu_spectrum_neg = spectrum_non_matsubara(-w, coup_strength, bath_broad, bath_freq, beta) # Effective temperature log = np.log(nonmatsu_spectrum/nonmatsu_spectrum_neg) effective_beta = log/(w) plt.plot(w, total_spectrum, label = r"S(total)", color = "blue") plt.plot(w, nonmatsu_spectrum, "--", label = r"$S_0(\omega)$", color = "orange") plt.xlabel(r"$\omega$") plt.ylabel(r"Spectrum") plt.legend() plt.show() plt.figure(figsize=(5, 4)) plt.plot(w[w>0], effective_beta[w>0]) plt.ylim(-2, 2) plt.xlabel(r"$\omega$") plt.ylabel(r"$\beta_{eff}[\omega]$") plt.show() # Low temperature case beta = np.inf w = np.linspace(-5, 10, 200)
def exploratory_rank_analysis(adata, groupby, x='inflation', y='mean', groups='all', n=100, special_markers=None, coloring='scores', annotate=False): """Plot scatterplots for various gene_characteristics. This is a visualization tools that helps to find significant markers and get a better understanding of the underlying data. Parameters ---------- adata : :class:`~scanpy.api.AnnData` Annotated data matrix. groupby : `str` The key of the sample grouping to consider. x : 'str' x-axis labelling for plots y : 'str' y-axis labelling for plots groups : `str`, `list`, optional (default: `'all'`) Subset of groups, e.g. `['g1', 'g2', 'g3']`, to which comparison shall be restricted. If not passed, a ranking will be generated for all groups. n : `int`, optional (default: 100) Number of datapoints in the scatterplot. If less are available, use all that are available special_markers: 'dict', optional (default: None) If provided, this should be a dict containing a list of gene names for each group in groupby. Special marked genes are highlighted in the visualization coloring : {'scores', 'absolute'}, optional (default: 'scores') Rank either according to Scores, or to absolute test-statistic value. In either case, results are scaled so as to guarantee sufficient contrast. annotate: bool, optional (default: False) If set to TRUE, annotate each scatterpoint with its name. Advisable only for small number of data points. """ # TODO: Check closely what of the below actions can be skipped and whats necessary n_groups = 0 for i, j in enumerate(adata.uns['rank_genes_groups_gene_names'][0]): n_groups = n_groups + 1 # Get group masks # TODO: Generalize. At the moment, only groups='all' works groups_order, groups_masks = utils.select_groups(adata, groups, groupby) # Create figure: n_rows = int(n_groups / 4) + 1 n_cols = 4 # For each group, get right genes (can be handled by intern function?) plt.figure(figsize=(24, 16)) for imask, mask in enumerate(groups_masks): score_list = list() name_list = list() special_markers_indices = list() # Note: No duplicates in each group for j, k in enumerate(adata.uns['rank_genes_groups_gene_scores']): # Make sure only first n datapoints are used if j >= n: break score_list.append(k[imask]) name_list.append( adata.uns['rank_genes_groups_gene_names'][j][imask]) # Inefficient and not generalizable but works: Check added index if in list of specially_marked_genes # TODO: Speed up if becomes a time issue if special_markers is None: pass elif adata.uns['rank_genes_groups_gene_names'][j][ imask] in special_markers[imask]: special_markers_indices.append(len(name_list) - 1) else: pass ### Get all the key figures # make things faster by calculating only what is required for plot mask_rest = ~mask # Get rate of expression rate_group = _zero_inflation_estimate(adata[:, name_list], mask) rate_rest = _zero_inflation_estimate(adata[:, name_list], mask_rest) if (x in { 'full_mean_group', 'tail_mean_group', 'full_mean_difference', 'tail_mean_difference' } or y in { 'full_mean_group', 'tail_mean_group', 'full_mean_difference', 'tail_mean_difference' }): means_group = _tail_mean_estimate(adata[:, name_list], mask) if (x in { 'full_mean_rest', 'tail_mean_rest', 'full_mean_difference', 'tail_mean_difference' } or y in { 'full_mean_rest', 'tail_mean_rest', 'full_mean_difference', 'tail_mean_difference' }): means_rest = _tail_mean_estimate(adata[:, name_list], mask_rest) if (x is 'tail_var_group' or y is 'tail_var_group'): # Get tail variance of expression var_group = _tail_var_estimate(adata[:, name_list], mask) if (x is 'tail_var_rest' or y is 'tail_var_rest'): var_rest = _tail_var_estimate(adata[:, name_list], mask_rest) if (x is 'CDR' or y is 'CDR'): # Get CDR: Need to give full adata object, since we need to count everything CDR = _Avg_CDR(adata, mask, name_list, model='rough', n_genes=None) if (x is 'full_var_group' or y is 'full_var_group'): # Slice first appropriately: adata_relevant = adata[:, name_list] exp, full_var_group = simple._get_mean_var(adata_relevant.X[mask]) if (x is 'full_var_rest' or y is 'full_var_rest'): # Slice first appropriately: adata_relevant = adata[:, name_list] exp_rest, full_var_rest = simple._get_mean_var( adata_relevant.X[mask_rest]) ### Prepare for coloring # get colored scatterplot # For coloring, get max score value, normalize (0,1) # Depending on whether normalization should be scale-invariant or only rank-invariant, do the following if coloring is 'scores': score_list = score_list / max(score_list) colors = cm.jet(score_list) elif coloring is 'absolute': color_list = rankdata(score_list) max_values = max(color_list) colors = cm.jet(color_list / max_values) # Identify true markers distinctly by using different size. else: logg.error('coloring should be either <socres> or <absolute>') s = 20 * np.ones(len(score_list)) # This works for numpy access (not for normal lists though) s[special_markers_indices] = 100 # In future, build method to mark top genes specially ### Actually do the plotting: Looping is inefficient and lengthy, but clear style # Potential values for x, y: 'mean' ('full' or 'tail'), 'tail_variance', 'inflation', 'CDR', # tail_variance_rest, Score (Just the ranking as given by test-statistic), 'full_var', 'full_var_rest' if x is 'expression_rate_difference': x_plot = rate_group - rate_rest elif x is 'expression_rate_group': x_plot = rate_group elif x is 'expression_rate_rest': x_plot = rate_rest elif x is 'Score': x_plot = score_list elif x is 'full_mean_difference': x_plot = means_group * rate_group - means_rest * rate_rest elif x is 'full_mean_group': x_plot = means_group * rate_group elif x is 'full_mean_rest': x_plot = means_rest * rate_rest elif x is 'tail_mean_difference': x_plot = means_group - means_rest elif x is 'tail_mean_group': x_plot = means_group elif x is 'tail_mean_rest': x_plot = means_rest elif x is 'tail_var_group': x_plot = var_group elif x is 'tail_var_rest': x_plot = var_rest elif x is 'full_var_group': x_plot = full_var_group elif x is 'full_var_rest': x_plot = full_var_rest elif x is 'CDR': x_plot = CDR else: logg.error( 'No accepted input. Check function documentation to get an overview over all inputs' ) if y is 'expression_rate_difference': y_plot = rate_group - rate_rest elif y is 'expression_rate_group': y_plot = rate_group elif y is 'expression_rate_rest': y_plot = rate_rest elif y is 'Score': y_plot = score_list elif y is 'full_mean_difference': y_plot = means_group * rate_group - means_rest * rate_rest elif y is 'full_mean_group': y_plot = means_group * rate_group elif y is 'full_mean_rest': y_plot = means_rest * rate_rest elif y is 'tail_mean_difference': y_plot = means_group - means_rest elif y is 'tail_mean_group': y_plot = means_group elif y is 'tail_mean_rest': y_plot = means_rest elif y is 'tail_var_group': y_plot = var_group elif y is 'tail_var_rest': y_plot = var_rest elif y is 'full_var_group': y_plot = full_var_group elif y is 'full_var_rest': y_plot = full_var_rest elif y is 'CDR': y_plot = CDR else: logg.error( 'No accepted input. Check function documentation to get an overview over all inputs' ) plt.subplot(n_rows, n_cols, imask + 1) plt.xlabel(x) plt.ylabel(y) # To make different scalings easier to compare, we set fixed limits for the case that x,y are e # expression rates if (x in { 'expression_rate_difference', 'expression_rate_group', 'expression_rate_rest' } and y in { 'expression_rate_difference', 'expression_rate_group', 'expression_rate_rest' }): plt.xlim(0, 1) plt.ylim(0, 1) plt.scatter(x_plot, y_plot, color=colors, s=s) if annotate is True: for i, txt in enumerate(name_list): plt.annotate(txt, (x_plot[i], y_plot[i])) plt.show()
def scatter(adata, groupby, groupid, x, y, n=100, special_markers=None, coloring='scores', size=12, annotate=True): """For one group, output a detailed chart analyzing highly ranked genes detailly. This is a visualization tools that helps to find significant markers and get a better understanding of the underlying data. Parameters ---------- adata : :class:`~scanpy.api.AnnData` Annotated data matrix. groupby : `str` The key of the sample grouping to consider. groupid: int The group for which detailed analysis should be displayed. x : 'str' x-axis labelling for plots y : 'str' y-axis labelling for plots n : `int`, optional (default: 100) Number of datapoints in the scatterplot. If less are available, use all that are available special_markers: 'dict', optional (default: None) If provided, this should be a dict containing a list of gene names for each group in groupby. Special marked genes are highlighted in the visualization coloring : {'scores', 'absolute'}, optional (default: 'scores') Rank either according to Scores, or to absolute test-statistic value. In either case, results are scaled so as to guarantee sufficient contrast. size: int, optional (default: 12) Determines scatter plot size. Large scatter-plots make it easier to identify specific genes using annotate=True annotate : bool, optional (default: False) If True, annotate each datapoint in (each?) scatterplot. Helps identify specific genes. Only recommended for small n. """ groups = 'all' groups_order, groups_masks = utils.select_groups(adata, groups, groupby) imask = groupid mask = groups_masks[imask] score_list = list() name_list = list() special_markers_indices = list() # Note: No duplicates in each group for j, k in enumerate(adata.uns['rank_genes_groups_gene_scores']): # Make sure only first n datapoints are used if j >= n: break score_list.append(k[imask]) name_list.append(adata.uns['rank_genes_groups_gene_names'][j][imask]) # Inefficient and not generalizable but works: Check added index if in list of specially_marked_genes # TODO: Speed up if becomes a time issue if special_markers is None: pass elif adata.uns['rank_genes_groups_gene_names'][j][ imask] in special_markers[imask]: special_markers_indices.append(len(name_list) - 1) else: pass ### Get all the key figures # make things faster by calculating only what is required for plot mask_rest = ~mask # Get rate of expression rate_group = _zero_inflation_estimate(adata[:, name_list], mask) rate_rest = _zero_inflation_estimate(adata[:, name_list], mask_rest) if (x in { 'full_mean_group', 'tail_mean_group', 'full_mean_difference', 'tail_mean_difference' } or y in { 'full_mean_group', 'tail_mean_group', 'full_mean_difference', 'tail_mean_difference' }): means_group = _tail_mean_estimate(adata[:, name_list], mask) if (x in { 'full_mean_rest', 'tail_mean_rest', 'full_mean_difference', 'tail_mean_difference' } or y in { 'full_mean_rest', 'tail_mean_rest', 'full_mean_difference', 'tail_mean_difference' }): means_rest = _tail_mean_estimate(adata[:, name_list], mask_rest) if (x is 'tail_var_group' or y is 'tail_var_group'): # Get tail variance of expression var_group = _tail_var_estimate(adata[:, name_list], mask) if (x is 'tail_var_rest' or y is 'tail_var_rest'): var_rest = _tail_var_estimate(adata[:, name_list], mask_rest) if (x is 'CDR' or y is 'CDR'): # Get CDR: Need to give full adata object, since we need to count everything CDR = _Avg_CDR(adata, mask, name_list, model='rough', n_genes=None) if (x is 'full_var_group' or y is 'full_var_group'): # Slice first appropriately: adata_relevant = adata[:, name_list] exp, full_var_group = simple._get_mean_var(adata_relevant.X[mask]) if (x is 'full_var_rest' or y is 'full_var_rest'): # Slice first appropriately: adata_relevant = adata[:, name_list] exp_rest, full_var_rest = simple._get_mean_var( adata_relevant.X[mask_rest]) ### Prepare for coloring # get colored scatterplot # For coloring, get max score value, normalize (0,1) # Depending on whether normalization should be scale-invariant or only rank-invariant, do the following if coloring is 'scores': score_list = score_list / max(score_list) colors = cm.jet(score_list) elif coloring is 'absolute': color_list = rankdata(score_list) max_values = max(color_list) colors = cm.jet(color_list / max_values) # Identify true markers distinctly by using different size. else: logg.error('coloring should be either <socres> or <absolute>') s = 20 * np.ones(len(score_list)) # This works for numpy access (not for normal lists though) s[special_markers_indices] = 100 # In future, build method to mark top genes specially ### Actually do the plotting: Looping is inefficient and lengthy, but clear style # Potential values for x, y: 'mean' ('full' or 'tail'), 'tail_variance', 'inflation', 'CDR', # tail_variance_rest, Score (Just the ranking as given by test-statistic), 'full_var', 'full_var_rest' if x is 'expression_rate_difference': x_plot = rate_group - rate_rest elif x is 'expression_rate_group': x_plot = rate_group elif x is 'expression_rate_rest': x_plot = rate_rest elif x is 'Score': x_plot = score_list elif x is 'full_mean_difference': x_plot = means_group * rate_group - means_rest * rate_rest elif x is 'full_mean_group': x_plot = means_group * rate_group elif x is 'full_mean_rest': x_plot = means_rest * rate_rest elif x is 'tail_mean_difference': x_plot = means_group - means_rest elif x is 'tail_mean_group': x_plot = means_group elif x is 'tail_mean_rest': x_plot = means_rest elif x is 'tail_var_group': x_plot = var_group elif x is 'tail_var_rest': x_plot = var_rest elif x is 'full_var_group': x_plot = full_var_group elif x is 'full_var_rest': x_plot = full_var_rest elif x is 'CDR': x_plot = CDR else: logg.error( 'No accepted input. Check function documentation to get an overview over all inputs' ) if y is 'expression_rate_difference': y_plot = rate_group - rate_rest elif y is 'expression_rate_group': y_plot = rate_group elif y is 'expression_rate_rest': y_plot = rate_rest elif y is 'Score': y_plot = score_list elif y is 'full_mean_difference': y_plot = means_group * rate_group - means_rest * rate_rest elif y is 'full_mean_group': y_plot = means_group * rate_group elif y is 'full_mean_rest': y_plot = means_rest * rate_rest elif y is 'tail_mean_difference': y_plot = means_group - means_rest elif y is 'tail_mean_group': y_plot = means_group elif y is 'tail_mean_rest': y_plot = means_rest elif y is 'tail_var_group': y_plot = var_group elif y is 'tail_var_rest': y_plot = var_rest elif y is 'full_var_group': y_plot = full_var_group elif y is 'full_var_rest': y_plot = full_var_rest elif y is 'CDR': y_plot = CDR else: logg.error( 'No accepted input. Check function documentation to get an overview over all inputs' ) # To make different scalings easier to compare, we set fixed limits for the case that x,y are e # expression rates if (x in { 'expression_rate_difference', 'expression_rate_group', 'expression_rate_rest' } and y in { 'expression_rate_difference', 'expression_rate_group', 'expression_rate_rest' }): plt.xlim(0, 1) plt.ylim(0, 1) fig, ax = plt.subplots(figsize=(size, size)) ax.scatter(x_plot, y_plot, color=colors, s=s) plt.xlabel(x) plt.ylabel(y) if annotate is True: for i, txt in enumerate(name_list): plt.annotate(txt, (x_plot[i], y_plot[i])) plt.show()
### only run this code if cleaned_data is returning data if len(cleaned_data) > 0: ages, net_worths, errors = zip(*cleaned_data) ages = numpy.reshape( numpy.array(ages), (len(ages), 1)) net_worths = numpy.reshape( numpy.array(net_worths), (len(net_worths), 1)) ### refit your cleaned data! try: reg.fit(ages, net_worths) plt.plot(ages, reg.predict(ages), color="blue") print "The Slope Of The New Regression Line :", reg.coef_ print "The New Regression Score is ", reg.score(ages_test, net_worths_test) except NameError: print "you don't seem to have regression imported/created," print " or else your regression object isn't named reg" print " either way, only draw the scatter plot of the cleaned data" plt.scatter(ages, net_worths) plt.xlabel("ages") plt.ylabel("net worths") plt.show() else: print "outlierCleaner() is returning an empty list, no refitting to be done"
def matplotall(): count = np.array(countnum) plt.figure(figsize=(20,10)) #subplot(縦何個,横何個, どこか) plt.subplot(2,3,1) mean = np.array(meancosnum) min = np.array(mincosnum) max = np.array(maxcosnum) plt.plot(count,max,label="max") plt.plot(count,mean,label="mean") plt.plot(count,min,label="min") plt.title("cossim") plt.xlabel("times") plt.ylabel("cossim") #plt.legend(bbox_to_anchor=(0.8, 0.2), loc='upper left', borderaxespad=0) #plt.subplots_adjust(right=0.7) plt.subplot(2,3,2) mean = np.array(meandqnum) min = np.array(mindqnum) max = np.array(maxdqnum) plt.plot(count,max,label="max") plt.plot(count,mean,label="mean") plt.plot(count,min,label="min") plt.title("two_cell_sum") plt.xlabel("times") plt.ylabel("dq") #plt.legend(bbox_to_anchor=(0.2, 0.2), loc='upper left', borderaxespad=0) #plt.subplots_adjust(right=0.7) plt.subplot(2,3,3) mean = np.array(meanaspnum) min = np.array(minaspnum) max = np.array(maxaspnum) plt.plot(count,max,label="max") plt.plot(count,mean,label="mean") plt.plot(count,min,label="min") plt.title("asp") plt.xlabel("times") plt.ylabel("asp") plt.subplot(2,3,4) mean = np.array(meancelldqnum) min = np.array(mincelldqnum) max = np.array(maxcelldqnum) plt.plot(count,max,label="max") plt.plot(count,mean,label="mean") plt.plot(count,min,label="min") plt.title("one_cell_max") plt.xlabel("times") plt.ylabel("celldq") #plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0) #plt.subplots_adjust(right=0.7) plt.subplot(2,3,5) mean = np.array(meanvisualnum) min = np.array(minvisualnum) max = np.array(maxvisualnum) plt.plot(count,max,label="max") plt.plot(count,mean,label="mean") plt.plot(count,min,label="min") plt.title("one_cell_max_rate_yaxis") plt.xlabel("times") plt.ylabel("visual") plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0) plt.show()
for i in range(max_iter): cost = compute_cost(X, y, theta) grad = gradient_descent(X, y, theta, learning_rate, m) theta = theta - grad his[i, :] = cost if i % 100 == 99: print ("iterate number: " + str(i + 1) + " -- cost: " + str(cost)) plt.plot(his, label='cost') plt.ylabel('cost') plt.xlabel('step') plt.title("logistic regression'") plt.legend(loc='upper center', shadow=True) plt.show() plt.scatter(X[:, 1], X[:, 2], c=y, s=50, cmap=plt.cm.Spectral) xa = get_arange(X[:, 1]) counter = 3 r3 = theta[counter] * multiply_feature(xa, np.array([0, 0])) + theta[0] + theta[1] * xa counter += 1 r2 = theta[counter] * xa +theta[2]
#plt.xticks(np.arange(len(label)), label) #plt.ylabel("Encounter registration rate", fontsize=16) #plt.legend(loc='upper right',fontsize=12) #plt.savefig('figure7.eps', format='eps', bbox_inches='tight') label = ("Species I","Species II","Species III") x =list(range(len(label))) list1 = [1,1,1] list2 = [0.5,0.5,1] list3 = [0.5,0.5,1] list4 = [1,1,1] total_width, n = 0.8,4 width = total_width / n plt.bar(x, list1, width=width, label="Our protocol", color="lightsalmon", edgecolor=['black','black','black','black'], align="center") for i in range(len(x)): x[i] = x[i] + width plt.bar(x, list2, width=width, label="pro=0.05", color="lightgreen", edgecolor=['black','black','black','black'], align="center") for i in range(len(x)): x[i] = x[i] + width plt.bar(x, list3, width=width, label="pro=0.1", color="lightskyblue", edgecolor=['black','black','black','black'], align="center") for i in range(len(x)): x[i] = x[i] + width plt.bar(x, list4, width=width, label="pro=0.2", color="orange", edgecolor=['black','black','black','black'], align="center") plt.xticks(np.arange(len(label))+0.25, label,fontsize=13) plt.yticks(fontsize=13) plt.xlabel('',fontsize=18) plt.ylabel("Encounter registration rate", fontsize=18) plt.legend(loc='right',fontsize=12, bbox_to_anchor=(1.4,0.2)) plt.savefig('figure7.eps', format='eps', bbox_inches='tight')
import numpy as np import matplotlib.pyplot as plt path_here = 'weights/weights_17/' path_self_xs = path_here + 'vsselfx_list.npy' path_self_ys = path_here + 'vsselfresults_y.npy' path_self3_xs = path_here + 'vsself3x_list.npy' path_self3_ys = path_here + 'vsself3results_y.npy' plt.xlabel('対戦回数', fontname="IPAexGothic") plt.ylabel('平均勝率(対戦相手はランダムな手を指す)', fontname="IPAexGothic") episodes_x = np.load(path_self_xs) results_y = np.load(path_self_ys)[0] results_y = results_y/2 + 0.5 episodes3_x = np.load(path_self3_xs) + 50000 results3_y = np.load(path_self3_ys)[0] results3_y = results3_y/2 + 0.5 episodes_x = np.concatenate((episodes_x, episodes3_x), axis=None) results_y = np.concatenate((results_y, results3_y), axis=None) plt.plot(episodes_x, results_y, linestyle='-', c='b', label="self-play") plt.legend() plt.show()
# Input file names print("Enter file names:") filename = [] while True: name_input = input('> ') if name_input == '': break else: filename.append(name_input) plt.figure(figsize=figs) for ic,filename1 in enumerate(filename): data1 = np.loadtxt(filename1,delimiter=',',skiprows=1) data1 = np.transpose(data1) Z1 = data1[0] Yc = data1[1] sc = plt.scatter(Z1,Yc,c=data1[3],cmap='rainbow',vmin=300, vmax=2200) v = [300,500,1000,1500,2000,2300] cbar = plt.colorbar(sc,ticks=v) cbar.set_label(r'$T$ $\mathrm{(K)}$') # plt.xlim(0,0.2) # plt.ylim(0,0.3) plt.xlabel(r'$Z$ (-)') plt.ylabel(r'$Y_c$ (-)') plt.tight_layout() plt.show()
def main(): parser = ArgumentParser(description='VOC Evaluation') parser.add_argument( 'result_dir', help='result dir including inference_*/result_test.json') parser.add_argument('config', help='config file path') parser.add_argument('--iou-thr', type=float, default=0.35, help='IoU threshold for evaluation') parser.add_argument('--conf_thresh', type=float, default=0.5, help='confidence threshold for evaluation') parser.add_argument('--nproc', type=int, default=4, help='Processes to be used for computing mAP') args = parser.parse_args() cfg = mmcv.Config.fromfile(args.config) data_test = cfg.data.test test_dataset = mmcv.runner.obj_from_dict(data_test, datasets) result_list = sorted( glob.glob(os.path.join(args.result_dir, '*/result_test.json')), key=lambda x: int(os.path.basename(os.path.dirname(x)).split('-')[1])) if hasattr(test_dataset, 'year') and test_dataset.year == 2007: dataset_name = 'voc07' else: dataset_name = test_dataset.CLASSES label_names = get_classes(dataset_name) step_list = [] class_f1_map = {key: [] for key in label_names} class_yewu2_map = {key: [] for key in label_names} label_ap_map = {key: [] for key in label_names} label_f1_map = {key: [] for key in label_names} label_yewu2_map = {key: [] for key in label_names} label_score_map = {key: [] for key in label_names} for r_i, result in enumerate(result_list): # 1. eval one result step = int(os.path.dirname(result).split('/')[-1].split('-')[-1]) # if step < 10000 or step > 60000: # continue print(result) try: eval_results = voc_eval(result, test_dataset, args.iou_thr, args.nproc, args.conf_thresh) except Exception as e: # print(e) continue step_list.append(step) if isinstance(eval_results[0]['ap'], np.ndarray): num_scales = len(eval_results[0]['ap']) else: num_scales = 1 num_classes = len(eval_results) max_f1 = np.zeros((num_scales, num_classes), dtype=np.float32) max_yewu2 = np.zeros((num_scales, num_classes), dtype=np.float32) for class_index, cls_result in enumerate(eval_results): if cls_result['recall'].size > 0: max_f1[:, class_index] = np.array(cls_result['f1'], ndmin=2)[:, -1] max_yewu2[:, class_index] = np.array(cls_result['yewu2'], ndmin=2)[:, -1] if len(max_f1) == 1: for j in range(num_classes): class_f1_map[label_names[j]].append(max_f1[0, j]) class_yewu2_map[label_names[j]].append(max_yewu2[0, j]) else: print('WARNING: current only accept num_scales == 1') # temp, using to add score list for class_index, cls_result in enumerate(eval_results): if cls_result['recall'].size > 0: score = np.array(cls_result['scores'], ndmin=2) ap = np.array(cls_result['ap'], ndmin=2) f1 = np.array(cls_result['f1'], ndmin=2) yewu2 = np.array(cls_result['yewu2'], ndmin=2) if len(f1) == 1: label_ap_map[label_names[class_index]].append(ap[0]) label_f1_map[label_names[class_index]].append(f1[0]) label_yewu2_map[label_names[class_index]].append(yewu2[0]) label_score_map[label_names[class_index]].append(score[0]) # for debug # if r_i > 10: # break # draw 3d figure # draw_figure_3d(step_list, label_f1_map, label_score_map, ylabel='F1') # print print_top_n(step_list, label_f1_map, label_score_map, ylabel='F1') print_top_n(step_list, label_yewu2_map, label_score_map, ylabel='Yewu2') # save result to csv class_f1_map['step'] = step_list dataframe = pd.DataFrame(class_f1_map) dataframe.to_csv('F1_result.csv', index=None) step_list = class_f1_map.pop('step') class_yewu2_map['step'] = step_list dataframe = pd.DataFrame(class_yewu2_map) dataframe.to_csv('Yewu2_result.csv', index=None) step_list = class_yewu2_map.pop('step') y_label = 'F1' save_path = './F1_result.png' plt.figure(figsize=(10, 5)) plt.title('{} result analyse'.format(y_label)) plt.xlabel('step') plt.ylabel(y_label) lines = [] for label, f1_list in class_f1_map.items(): x_list = [int(n) for n in step_list] x_y = [[i, j] for i, j in zip(x_list, f1_list)] sorted_x_y = sorted(x_y, key=lambda x: x[0]) new_x, new_y = [], [] for x_y in sorted_x_y: new_x.append(x_y[0]) new_y.append(x_y[1]) line = plt.plot(new_x, new_y) lines.append(line) plt.legend(lines, labels=label_names, loc='best') plt.savefig(save_path)
# Fitting Linear Regression to the dataset linear_regressor = LinearRegression() linear_regressor.fit(X, y) # Fitting Polynomial Linear Regression equation (y = b0 + b1*x1 + b2*x1^2 + ... + bn*x1^n) polynomial_regressor = PolynomialFeatures(degree=4) X_poly = polynomial_regressor.fit_transform(X) linear_regressor_2 = LinearRegression() linear_regressor_2.fit(X_poly, y) # Visualising the Linear Regression results plt.scatter(X, y, color='red') plt.plot(X, linear_regressor.predict(X), color='blue') plt.title('Salary According To Position') plt.xlabel('Position Level') plt.ylabel('Salary') plt.show() # Visualising the Polynomial Regression results X_grid = np.arange(min(X), max(X), 0.1) X_grid = X_grid.reshape(len(X_grid), 1) plt.scatter(X, y, color='red') plt.plot(X_grid, linear_regressor_2.predict( polynomial_regressor.fit_transform(X_grid)), color='blue') plt.title('Salary According To Position') plt.xlabel('Position Level') plt.ylabel('Salary') plt.show()
history = classifier.fit_generator(training_set, steps_per_epoch=204, epochs=10, callbacks=callbacks, validation_data=test_set, validation_steps=52) #%% Plotting accuracy results import matplotlib.pyplot as plt print(history.history.keys()) plt.plot(history.history['accuracy']) plt.plot(history.history['val_accuracy']) plt.ylabel('accuracy') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.title('input layer = 32, hidden layer = 48') #%% Import necessary Libraries import os import cv2 import numpy as np from keras.preprocessing import image #%% Resize Function def Resize(img): # Resize Image to 480x360 resize = cv2.resize(img, (480, 360))
def prody_anm(pdb, **kwargs): """Perform ANM calculations for *pdb*. """ for key in DEFAULTS: if not key in kwargs: kwargs[key] = DEFAULTS[key] from os.path import isdir, join outdir = kwargs.get('outdir') if not isdir(outdir): raise IOError('{0} is not a valid path'.format(repr(outdir))) import numpy as np import prody LOGGER = prody.LOGGER selstr = kwargs.get('select') prefix = kwargs.get('prefix') cutoff = kwargs.get('cutoff') gamma = kwargs.get('gamma') nmodes = kwargs.get('nmodes') selstr = kwargs.get('select') model = kwargs.get('model') pdb = prody.parsePDB(pdb, model=model) if prefix == '_anm': prefix = pdb.getTitle() + '_anm' select = pdb.select(selstr) if select is None: LOGGER.warn('Selection {0} did not match any atoms.'.format( repr(selstr))) return LOGGER.info('{0} atoms will be used for ANM calculations.'.format( len(select))) anm = prody.ANM(pdb.getTitle()) anm.buildHessian(select, cutoff, gamma) anm.calcModes(nmodes) LOGGER.info('Writing numerical output.') if kwargs.get('outnpz'): prody.saveModel(anm, join(outdir, prefix)) prody.writeNMD(join(outdir, prefix + '.nmd'), anm, select) extend = kwargs.get('extend') if extend: if extend == 'all': extended = prody.extendModel(anm, select, pdb) else: extended = prody.extendModel(anm, select, select | pdb.bb) prody.writeNMD(join(outdir, prefix + '_extended_' + extend + '.nmd'), *extended) outall = kwargs.get('outall') delim = kwargs.get('numdelim') ext = kwargs.get('numext') format = kwargs.get('numformat') if outall or kwargs.get('outeig'): prody.writeArray(join(outdir, prefix + '_evectors' + ext), anm.getArray(), delimiter=delim, format=format) prody.writeArray(join(outdir, prefix + '_evalues' + ext), anm.getEigvals(), delimiter=delim, format=format) if outall or kwargs.get('outbeta'): from prody.utilities import openFile fout = openFile(prefix + '_beta' + ext, 'w', folder=outdir) fout.write( '{0[0]:1s} {0[1]:4s} {0[2]:4s} {0[3]:5s} {0[4]:5s}\n'.format( ['C', 'RES', '####', 'Exp.', 'The.'])) for data in zip(select.getChids(), select.getResnames(), select.getResnums(), select.getBetas(), prody.calcTempFactors(anm, select)): fout.write( '{0[0]:1s} {0[1]:4s} {0[2]:4d} {0[3]:5.2f} {0[4]:5.2f}\n'. format(data)) fout.close() if outall or kwargs.get('outcov'): prody.writeArray(join(outdir, prefix + '_covariance' + ext), anm.getCovariance(), delimiter=delim, format=format) if outall or kwargs.get('outcc') or kwargs.get('outhm'): cc = prody.calcCrossCorr(anm) if outall or kwargs.get('outcc'): prody.writeArray(join(outdir, prefix + '_cross-correlations' + ext), cc, delimiter=delim, format=format) if outall or kwargs.get('outhm'): prody.writeHeatmap(join(outdir, prefix + '_cross-correlations.hm'), cc, resnum=select.getResnums(), xlabel='Residue', ylabel='Residue', title=anm.getTitle() + ' cross-correlations') if outall or kwargs.get('hessian'): prody.writeArray(join(outdir, prefix + '_hessian' + ext), anm.getHessian(), delimiter=delim, format=format) if outall or kwargs.get('kirchhoff'): prody.writeArray(join(outdir, prefix + '_kirchhoff' + ext), anm.getKirchhoff(), delimiter=delim, format=format) if outall or kwargs.get('outsf'): prody.writeArray(join(outdir, prefix + '_sqflucts' + ext), prody.calcSqFlucts(anm), delimiter=delim, format=format) figall = kwargs.get('figall') cc = kwargs.get('figcc') sf = kwargs.get('figsf') bf = kwargs.get('figbeta') cm = kwargs.get('figcmap') if figall or cc or sf or bf or cm: try: import matplotlib.pyplot as plt except ImportError: LOGGER.warning('Matplotlib could not be imported. ' 'Figures are not saved.') else: prody.SETTINGS['auto_show'] = False LOGGER.info('Saving graphical output.') format = kwargs.get('figformat') width = kwargs.get('figwidth') height = kwargs.get('figheight') dpi = kwargs.get('figdpi') format = format.lower() if figall or cc: plt.figure(figsize=(width, height)) prody.showCrossCorr(anm) plt.savefig(join(outdir, prefix + '_cc.' + format), dpi=dpi, format=format) plt.close('all') if figall or cm: plt.figure(figsize=(width, height)) prody.showContactMap(anm) plt.savefig(join(outdir, prefix + '_cm.' + format), dpi=dpi, format=format) plt.close('all') if figall or sf: plt.figure(figsize=(width, height)) prody.showSqFlucts(anm) plt.savefig(join(outdir, prefix + '_sf.' + format), dpi=dpi, format=format) plt.close('all') if figall or bf: plt.figure(figsize=(width, height)) bexp = select.getBetas() bcal = prody.calcTempFactors(anm, select) plt.plot(bexp, label='Experimental') plt.plot(bcal, label=('Theoretical (R={0:.2f})'.format( np.corrcoef(bcal, bexp)[0, 1]))) plt.legend(prop={'size': 10}) plt.xlabel('Node index') plt.ylabel('Experimental B-factors') plt.title(pdb.getTitle() + ' B-factors') plt.savefig(join(outdir, prefix + '_bf.' + format), dpi=dpi, format=format) plt.close('all')
for i, n in enumerate( range(GRID_incr, GRID_NUM_OF_STOPS + GRID_incr, GRID_incr)): time_start = time.time() grid_algorithm(n) time_taken = time.time() - time_start grid_n_vs_time[i][0] = n grid_n_vs_time[i][1] = (time_taken + grid_n_vs_time[i][1]) / x data_grid = pd.DataFrame(grid_n_vs_time[0:-2], columns=["n", "Time"]) print(grid_n_vs_time) fig1 = plt.figure(figsize=(10, 10)) plt.scatter(data_grid['n'], data_grid['Time'], marker='o') plt.title("Time Response of the Grid Algorithm:") plt.xlabel("Number of Stops (n):", fontsize=17) plt.ylabel("Time (sec):", fontsize=17) plt.show() # # Near Algorithm: # # NEAR_NUM_OF_STOPS = 4000 # NEAR_NUM_OF_DATA_POINTS = 25 # NEAR_incr = NEAR_NUM_OF_STOPS // NEAR_NUM_OF_DATA_POINTS # # near_n_vs_time = [] # # for n in range(NEAR_incr, NEAR_NUM_OF_STOPS + NEAR_incr, NEAR_incr): # time_start = time.time() # greedy_algorithm(n) # time_taken = time.time() - time_start
data = dataArray[-1] data.CPU += float(line1[5]) data.MEM += float(line1[7]) if len(line2) == 14: # print(line2) # print(line2[11]) # print(line2[13]) dataArray.append(resource(float(line2[11]),float(line2[13]))) # print("DATA ARRAY: "+str(dataArray)) import matplotlib.pyplot as plt if __name__ == "__main__": parseFile() # array = [] # for i in dataArray: # if i.MEM > 2.6: # print(i) plt.plot([i for i in range(0,299)],[data.CPU for data in dataArray],'r') plt.ylabel('CPU %') plt.xlabel('Time') plt.show() plt.plot([i for i in range(0,299)],[data.MEM for data in dataArray],'g') plt.ylabel('MEM %') plt.xlabel('Time') plt.show() # plt.plot([i for i in range(0,299)],[data.MEM for data in dataArray],'g',[i for i in range(0,299)],[data.CPU for data in dataArray],'r') # plt.ylabel('MEM and CPU %') # plt.xlabel('Time') # plt.show()
def measureTime(preSorted = False, numTrials = 30): # Print whether we are using sorted inputs. if preSorted: print('Timing algorithms using only sorted data.') else: print('Timing algorithms using random data.') print('Averaging over %d Trials' % numTrials) print() # First, we seed the random number generator to ensure consistency. random.seed(1) # We now define the range of n values to consider. if preSorted: # Need to look at larger n to get a good sense of runtime. # Look at n from 20 to 980. # Note that 1000 causes issues with recursion depth... N = list(range(1,50)) N = [20*x for x in N] else: # Look at n from 10 to 500. N = list(range(1,51)) N = [10*x for x in N] # Store the different algs to consider. algs = [SelectionSort, InsertionSort, \ BubbleSort, MergeSort, \ QuickSort, list.sort] # Preallocate space to store the runtimes. tSelectionSort = N.copy() tInsertionSort = N.copy() tBubbleSort = N.copy() tMergeSort = N.copy() tQuickSort = N.copy() tPython = N.copy() # Create some flags for whether each sorting alg works. isCorrect = [True, True, True, True, True, True] # Loop over the different sizes. for nInd in range(0,len(N)): # Get the current value of n to consider. n = N[nInd] # Reset the running sum of the runtimes. timing = [0,0,0,0,0,0] # Loop over the 30 tests. for test in range(1,numTrials+1): # Create the random list of size n to sort. listToSort = list(range(0,n)) listToSort = [random.random() for x in listToSort] if preSorted: # Pre-sort the list. listToSort.sort() # Loop over the algs. for aI in range(0,len(algs)): # Grab the name of the alg. alg = algs[aI] # Copy the original list for sorting. copiedList = listToSort.copy() # Time the sort. t = time.time() if aI != 4 : alg(copiedList) else: alg(copiedList,0,len(copiedList)) t = time.time() - t # Ensure that your function sorted the list. if not isSorted(listToSort,copiedList): isCorrect[aI] = False # Add the time to our running sum. timing[aI] += t # Now that we have completed the numTrials tests, average the times. timing = [x/numTrials for x in timing] # Store the times for this value of n. tSelectionSort[nInd] = timing[0] tInsertionSort[nInd] = timing[1] tBubbleSort[nInd] = timing[2] tMergeSort[nInd] = timing[3] tQuickSort[nInd] = timing[4] tPython[nInd] = timing[5] # If there was an error in one of the plotting algs, report it. for aI in range(0,len(algs)-1): if not isCorrect[aI]: print('%s not implemented properly!!!' % algs[aI].__name__) # Now plot the timing data. for aI in range(0,len(algs)): # Get the alg. alg = algs[aI].__name__ if aI != 5 else 'Python' # Plot. plt.figure() plt.plot(N,locals()['t%s' % alg]) plt.title('%s runtime versus n' % alg) plt.xlabel('Input Size n') plt.ylabel('Runtime (s)') if preSorted: plt.savefig('%s_sorted.png' % alg, bbox_inches='tight') else: plt.savefig('%s.png' % alg, bbox_inches='tight') # Plot them all together. plt.figure() fig, ax = plt.subplots() ax.plot(N,tSelectionSort, label='Selection') ax.plot(N,tInsertionSort, label='Insertion') ax.plot(N,tBubbleSort, label='Bubble') ax.plot(N,tMergeSort, label='Merge') ax.plot(N,tQuickSort, label='Quick') ax.plot(N,tPython, label='Python') legend = ax.legend(loc='upper left') plt.title('All sorting runtimes versus n') plt.xlabel('Input Size n') plt.ylabel('Runtime (s)') if preSorted: plt.savefig('sorting_sorted.png', bbox_inches='tight') else: plt.savefig('sorting.png', bbox_inches='tight') # Now look at the log of the sort times. logN = [(numpy.log(x) if x>0 else -6) for x in N] logSS = [(numpy.log(x) if x>0 else -6) for x in tSelectionSort] logIS = [(numpy.log(x) if x>0 else -6) for x in tInsertionSort] logBS = [(numpy.log(x) if x>0 else -6) for x in tBubbleSort] logMS = [(numpy.log(x) if x>0 else -6) for x in tMergeSort] logQS = [(numpy.log(x) if x>0 else -6) for x in tQuickSort] # Linear regression. mSS, _, _, _, _ = stats.linregress(logN,logSS) mIS, _, _, _, _ = stats.linregress(logN,logIS) mBS, _, _, _, _ = stats.linregress(logN,logBS) # Plot log-log figure. plt.figure() fig, ax = plt.subplots() ax.plot(logN,logSS, label='Selection') ax.plot(logN,logIS, label='Insertion') ax.plot(logN,logBS, label='Bubble') legend = ax.legend(loc='upper left') plt.title('Log-Log plot of runtimes versus n') plt.xlabel('log(n)') plt.ylabel('log(runtime)') if preSorted: plt.savefig('log_sorted.png', bbox_inches='tight') else: plt.savefig('log.png', bbox_inches='tight') # Print the regression info. print() print('Selection Sort log-log Slope (all n): %f' % mSS) print('Insertion Sort log-log Slope (all n): %f' % mIS) print('Bubble Sort log-log Slope (all n): %f' % mBS) print() # Now strip off all n<200... logN = logN[19:] logSS = logSS[19:] logIS = logIS[19:] logBS = logBS[19:] logMS = logMS[19:] logQS = logQS[19:] # Linear regression. mSS, _, _, _, _ = stats.linregress(logN,logSS) mIS, _, _, _, _ = stats.linregress(logN,logIS) mBS, _, _, _, _ = stats.linregress(logN,logBS) mMS, _, _, _, _ = stats.linregress(logN,logMS) mQS, _, _, _, _ = stats.linregress(logN,logQS) # Print the regression info. print('Selection Sort log-log Slope (n>%d): %f' \ % (400 if preSorted else 200, mSS)) print('Insertion Sort log-log Slope (n>%d): %f' \ % (400 if preSorted else 200, mIS)) print('Bubble Sort log-log Slope (n>%d): %f' \ % (400 if preSorted else 200, mBS)) print('Merge Sort log-log Slope (n>%d): %f' \ % (400 if preSorted else 200, mMS)) print('Quick Sort log-log Slope (n>%d): %f' \ % (400 if preSorted else 200, mQS)) # Close all figures. plt.close('all')
def plot_rsqprofile(fig_data): """Create an R² profile plot using kmeans_reduce_ensemble output. The R² plot allows evaluation of the proportion of total uncertainty in the original ensemble that is provided by the reduced selected. Examples -------- >>> from xclim.ensembles import kmeans_reduce_ensemble, plot_rsqprofile >>> is_matplotlib_installed() >>> crit = xr.open_dataset(path_to_ensemble_file).data >>> ids, cluster, fig_data = kmeans_reduce_ensemble(data=crit, method={'rsq_cutoff':0.9}, random_state=42, make_graph=True) >>> plot_rsqprofile(fig_data) """ rsq = fig_data["rsq"] n_sim = fig_data["realizations"] n_clusters = fig_data["n_clusters"] # make a plot of rsq profile plt.figure(figsize=(10, 6)) plt.plot(range(1, n_sim + 1), rsq, "k-o", label="R²", linewidth=0.8, markersize=4) # plt.plot(np.arange(1.5, n_sim + 0.5), np.diff(rsq), 'r', label='ΔR²') axes = plt.gca() axes.set_xlim([0, fig_data["realizations"]]) axes.set_ylim([0, 1]) plt.xlabel("Number of groups") plt.ylabel("R²") plt.legend(loc="lower right") plt.title("R² of groups vs. full ensemble") if "rsq_cutoff" in fig_data["method"].keys(): col = "k--" label = f"R² selection > {fig_data['method']['rsq_cutoff']} (n = {n_clusters})" if "max_clusters" in fig_data.keys(): if rsq[n_clusters - 1] < fig_data["method"]["rsq_cutoff"]: col = "r--" label = ( f"R² selection = {rsq[n_clusters - 1].round(2)} (n = {n_clusters}) :" f" Max cluster set to {fig_data['max_clusters']}" ) else: label = ( f"R² selection > {fig_data['method']['rsq_cutoff']} (n = {n_clusters}) :" f" Max cluster set to {fig_data['max_clusters']}" ) plt.plot( (0, n_clusters, n_clusters), (rsq[n_clusters - 1], rsq[n_clusters - 1], 0), col, label=label, linewidth=0.75, ) plt.legend(loc="lower right") elif "rsq_optimize" in fig_data["method"].keys(): onetoone = -1 * (1.0 / (n_sim - 1)) + np.arange(1, n_sim + 1) * ( 1.0 / (n_sim - 1) ) plt.plot( range(1, n_sim + 1), onetoone, color=[0.25, 0.25, 0.75], label="Theoretical constant increase in R²", linewidth=0.5, ) plt.plot( range(1, n_sim + 1), rsq - onetoone, color=[0.75, 0.25, 0.25], label="Real benefits (R² - theoretical)", linewidth=0.5, ) col = "k--" label = f"Optimized R² cost / benefit (n = {n_clusters})" if "max_clusters" in fig_data.keys(): opt = rsq - onetoone imax = np.where(opt == opt.max())[0] if rsq[n_clusters - 1] < rsq[imax]: col = "r--" label = ( f"R² selection = {rsq[n_clusters - 1].round(2)} (n = {n_clusters}) :" f" Max cluster set to {fig_data['max_clusters']}" ) plt.plot( (0, n_clusters, n_clusters), (rsq[n_clusters - 1], rsq[n_clusters - 1], 0), col, linewidth=0.75, label=label, ) plt.legend(loc="center right") else: plt.plot( (0, n_clusters, n_clusters), (rsq[n_clusters - 1], rsq[n_clusters - 1], 0), "k--", label=f"n = {n_clusters} (R² selection = {rsq[n_clusters - 1].round(2)})", linewidth=0.75, ) plt.legend(loc="lower right")
satTest = sat[60:len(sat),:] #% step-2: train a linear regression model using the Gradient Descent (GD) method # ** theta and xValues have 3 columns since have 2 features: y = (theta * x^0) + (theta * x^1) + (theta * x^2) theta = np.zeros(3) xValues = np.ones((60, 3)) xValues[:, 1:3] = satTrain[:, 0:2] yValues = satTrain[:, 2] # call the GD algorithm, placeholders in the function gradientDescent() [theta, arrCost] = gradientDescent(xValues, yValues, theta, ALPHA, MAX_ITER) #visualize the convergence curve plt.plot(range(0,len(arrCost)),arrCost); plt.xlabel('iteration') plt.ylabel('cost') plt.title('alpha = {} theta = {}'.format(ALPHA, theta)) plt.show() #% step-3: testing testXValues = np.ones((len(satTest), 3)) testXValues[:, 1:3] = satTest[:, 0:2] tVal = testXValues.dot(theta) #% step-4: evaluation # calculate average error and standard deviation tError = np.sqrt([x**2 for x in np.subtract(tVal, satTest[:, 2])]) print('results: {} ({})'.format(np.mean(tError), np.std(tError)))
model_name_y = "QTABLE_Y_TESTON{}".format(chosen_test_set) np.savetxt(model_name_x, x_qtable) np.savetxt(model_name_y, y_qtable) # PLOT TRAINING RESULTS x_end_mean = [] y_end_mean = [] X = np.linspace(0, total_epochs, total_epochs) for i in range(0, total_epochs): x_end_mean.append(np.mean(x_mean_errors[:, i])) y_end_mean.append(np.mean(y_mean_errors[:, i])) plt.plot(x_end_mean, color='k', label='Error in X') plt.plot(y_end_mean, color='g', label='Error in Y') plt.legend() plt.ylabel('mean error per epoch') plt.xlabel('Number of epochs') plt.title("Q Learning method with test on file {} .".format(chosen_test_set)) plt.savefig("PLOT_QTABLE_TESTON{}".format(chosen_test_set)) test_init_mean = np.zeros(2) t_sum = np.zeros(2) for set in test_sets: for i in range(0, 1): t_sum += set.init_mean test_init_mean = t_sum / len(test_sets) test_x_mean_errors = np.zeros(len(test_sets)) test_y_mean_errors = np.zeros(len(test_sets)) #TEST MODEL for set in range(len(test_sets) - 1):
# train the network print("[INFO] training network...") H = model.fit(trainX, trainY, validation_data=(testX, testY), batch_size=32, epochs=100, verbose=1) # evaluate the network print("[INFO] evaluating network...") predictions = model.predict(testX, batch_size=32) print( classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=["cat", "dog", "panda"])) # plot the training lass and accuracy plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, 100), H.history["loss"], label="train_loss") plt.plot(np.arange(0, 100), H.history["val_loss"], label="val_loss") plt.plot(np.arange(0, 100), H.history["acc"], label="train_acc") plt.plot(np.arange(0, 100), H.history["val_acc"], label="val_acc") plt.title("Training Loss and Accuracy") plt.xlabel("Epoch #") plt.ylabel("Loss/Accuracy") plt.legend() #plt.show() plt.savefig("output/shallownet_animals.png")
for x in range(acc*daysInMonth, acc*daysInMonth+daysInMonth): tmp1.append(tempsDaysAvg[x]) tmp2.append(tempsDaysHigh[x]) tmp3.append(tempsDaysLow[x]) acc += 1 tempsMonthAvg.append(sum(tmp1)/len(tmp1)) tempsMonthHigh.append(sum(tmp2)/len(tmp2)) tempsMonthLow.append(sum(tmp3)/len(tmp3)) # Smoothing the data months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] monthNP = np.array(months) xnew = np.linspace(monthNP.min(), monthNP.max(), 1000) avgNP = np.array(tempsMonthAvg) splAvg = make_interp_spline(monthNP, avgNP, k=3) highNP = np.array(tempsMonthHigh) splHigh = make_interp_spline(monthNP, highNP, k=3) lowNP = np.array(tempsMonthLow) splLow = make_interp_spline(monthNP, lowNP, k=3) # Initializing the plt stuffs plt.title("Average Temperature Every Month (smoothed)") plt.xlabel("Month") plt.ylabel("Temperature") plt.plot(xnew, splAvg(xnew), label="Average") plt.plot(xnew, splHigh(xnew), label=header[1]) plt.plot(xnew, splLow(xnew), label=header[0]) plt.legend() # Showing the actual plot plt.show()
#W[i,:] = w #print("iteration",k, " ",np.dot(W[i,:],w)) W[i,:] = w S = np.dot(W,X) A = np.linalg.inv(W) return W,S,A Xcenter = center(X) Xwhite, E, D= whiten(Xcenter) W,S,A = fastICA(Xwhite) #whitened X plt.scatter(Xwhite[0,:],Xwhite[1,:]) #plt.plot(Xwhite[0,:], Xwhite[1,:], marker='.', color='blue') plt.xlabel(r"$\widetilde{x}_1$") plt.ylabel(r"$\widetilde{x}_2$") ax = plt.axes() ax.arrow(0, 0, A[0,1],A[1,1], head_width=0.06, head_length=0.1, fc='k', ec='k', linewidth=2.0) ax.arrow(0, 0, A[0,0],A[1,0], head_width=0.06, head_length=0.1, fc='k', ec='k', linewidth=2.0) plt.show() #centered data + eigen vectors +eigen values plt.scatter(Xcenter[0,:],Xcenter[1,:], alpha ='0.5') D = np.sqrt(np.diag(D)) ax = plt.axes() ax.arrow(0, 0, E[0,0]*D[0],E[1,0]*D[0], head_width=0.06, head_length=0.1, fc='r', ec='r', linewidth=2.0) ax.annotate('ev1 = 5.21', xy=(E[0,0]*D[0],E[1,0]*D[0] ), xytext=(E[0,0]*D[0]+0.4, E[1,0]*D[0]+0.7), fontweight='bold',color='r') ax.arrow(0, 0, E[0,1]*D[1],E[1,1]*D[1], head_width=0.06, head_length=0.1, fc='r', ec='r', linewidth=2.0) ax.annotate('ev2 = 0.05', xy=(E[0,1]*D[1],E[1,1]*D[1]), xytext=(E[0,1]*D[1]+0.15, E[1,1]*D[1]+0.15), fontweight='bold', color ='r')
from matplotlib import pyplot as plt # print(plt.style.available) # plt.style.use('fivethirtyeight') # plt.style.use('ggplot') plt.xkcd() width = 0.25 x_axis = [1, 2, 3, 4, 5, 6] y_axis = [23, 45, 67, 120, 340, 560] sec_yaxis = [34, 56, 78, 190, 200, 320] plt.title("this is a dummy title") plt.xlabel("this is x axis label") plt.ylabel("this is a y axis label") plt.plot(x_axis, y_axis, label="Python testing", marker="o", linewidth=1) plt.plot(x_axis, sec_yaxis, label="javascript testing", marker="o", linewidth=1) plt.legend() plt.tight_layout() plt.grid(True) #plt.savefig("myimage.png") plt.show()
if lm + t2*del_lm >= 0: lm = lm + t2*del_lm print('Frame: {} Steps: {}'.format(int(i*(n/inc)/n), step), end='\n') # print('LM: {}'.format(lm)) data_clean[int(i):int(i+n)] += get_sparse(x) i += inc write('out_{}.wav'.format(filename.split('.wav')[0]), fs, 0.7*normalize(data_clean)) name = filename.split('.wav')[0] plt.figure(name) tt = np.arange(data_clean.size)/fs plt.title('Input signal SNR: {}dB'.format(name.split('_')[1])) plt.plot(tt, normalize(data), label='input') plt.plot(tt, normalize(data_clean), alpha=0.7, label='output') plt.ylabel('Normalised Signal value') plt.legend(loc='upper right') plt.xlabel('time') plt.grid(True) plt.show()
def plot_curve(self): title = 'the accuracy curve of train/validate' dpi = 80 width, height = 1200, 800 legend_fontsize = 10 scale_distance = 48.8 figsize = width / float(dpi), height / float(dpi) fig = plt.figure(figsize=figsize) x_axis = np.array([i for i in range(self.epochs)]) # epochs y_axis = np.zeros(self.epochs) plt.xlim(0, self.epochs) plt.ylim(0, 100) interval_y = 5 interval_x = 5 plt.xticks(np.arange(0, self.epochs + interval_x, interval_x)) plt.yticks(np.arange(0, 100 + interval_y, interval_y)) plt.grid() plt.title(title, fontsize=20) plt.xlabel('the training epoch', fontsize=16) plt.ylabel('accuracy', fontsize=16) y_axis = self.train_accuracy_history plt.plot(x_axis, y_axis, color='g', linestyle='-', label='train_accuracy', lw=2) plt.legend(loc=4, fontsize=legend_fontsize) y_axis = self.val_accuracy_history plt.plot(x_axis, y_axis, color='y', linestyle='-', label='valid_accuracy', lw=2) plt.legend(loc=4, fontsize=legend_fontsize) fig_name = self.save_history_path + "/vgg16_accuracy_history" fig.savefig(fig_name, dpi=dpi, bbox_inches='tight') print('---- save accuracy history figure {} into {}'.format( title, self.save_history_path)) plt.close(fig) title = 'the loss history curve of train/validate' fig = plt.figure(figsize=figsize) x_axis = np.array([i for i in range(self.epochs)]) # epochs y_axis = np.zeros(self.epochs) plt.xlim(0, self.epochs) plt.ylim(self.min_loss, self.max_loss) interval_y = (self.max_loss - self.min_loss) / 20 interval_x = 5 plt.xticks(np.arange(0, self.epochs + interval_x, interval_x)) plt.yticks( np.arange(max(0, self.min_loss - 5 * interval_y), self.max_loss + 2 * interval_y, interval_y)) plt.grid() plt.title(title, fontsize=20) plt.xlabel('the training epoch', fontsize=16) plt.ylabel('loss', fontsize=16) y_axis = self.train_loss_history plt.plot(x_axis, y_axis, color='g', linestyle=':', label='train_loss', lw=2) plt.legend(loc=4, fontsize=legend_fontsize) y_axis = self.val_loss_history plt.plot(x_axis, y_axis, color='y', linestyle=':', label='val_loss', lw=2) plt.legend(loc=4, fontsize=legend_fontsize) fig_name = self.save_history_path + "/vgg16_loss_history" fig.savefig(fig_name, dpi=dpi, bbox_inches='tight') print('---- save loss_history figure {} into {}'.format( title, self.save_history_path)) plt.close(fig)
from sklearn.preprocessing import PolynomialFeatures poly_regressor = PolynomialFeatures(degree=4) #exponencial matrix based in X # creates a matrix with X in the midle and X squared on the right, and a column of ones in the left X_poly = poly_regressor.fit_transform(X) #second linear regressor to fit the X_poly and y linear_regressor_2 = LinearRegression() linear_regressor_2.fit(X_poly, y) ##### Visualizes the linear regression results # plot the salaries plt.scatter(X, y, color="red") #predict using linear regressor (blue fit line) plt.plot(X, linear_regressor.predict(X), color="blue") plt.title("Truth or Bluff (Linear Regression)") plt.xlabel("Position Level") plt.ylabel("Salary") plt.show() ##### Visualizing the Polynnomial Regressor fit curve # plot the salaries ### Add a degree to polynomial #change degree param to 3 and later 4 when creating the poly_regressor object #the predictions that wiill match the pointing with more degrees. ######increases resolution of plot X_grid = np.arange(min(X), max(X), 0.1) X_grid = X_grid.reshape((len(X_grid), 1)) plt.scatter(X, y, color="red")
m = 25 for day in days: results = range(10) data = [] for result in results: a = random.choice(range(10)) data.append([m - a, m, m + a]) ax = plt.subplot(1, 7, days.index(day) + 1) medianprops = dict(linestyle=None, linewidth=0) plt.boxplot(data, notch=0, sym='', vert=0, whis=0, medianprops=medianprops, patch_artist=True, boxprops=dict(facecolor='blue', color='white')) plt.xlabel(day) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) ax.spines['bottom'].set_visible(False) ax.spines['left'].set_visible(False) ax.yaxis.set_ticks([0]) ax.xaxis.set_ticks([0]) # ax.yaxis.set_ticks([10,20,30,40]) ax.xaxis.set_ticklabels('') plt.setp(ax.get_yticklabels(), visible=False) plt.subplots_adjust(wspace=1) plt.show()
def run_model(session, pred, X, y, is_training, loss_val, Xdata, ydata, epochs=1, batch_size=64, print_every=100, train_step=None, plot_losses=False): # Compute accuracy using tf correct_prediction = tf.equal(tf.argmax(pred, axis=1), y) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # Shuffle indices N = Xdata.shape[0] train_indices = np.arange(N) np.random.shuffle(train_indices) is_training_mode = train_step is not None # Set up variables for computation and optimization variables = [mean_loss, correct_prediction, accuracy] if is_training_mode: variables[-1] = train_step iter_counter = 0 for e in range(epochs): # Keep track of losses and accuracy num_correct = 0 losses = [] for i in range(int(math.ceil(N / batch_size))): # Generate indices for the batch start_idx = (i * batch_size) % N idx = train_indices[start_idx:start_idx + batch_size] feed_dict = { X: Xdata[idx, :], y: ydata[idx], is_training: is_training_mode } actual_batch_size = ydata[idx].shape[0] # Compute loss and number of correct predictions loss, corr, _ = session.run(variables, feed_dict=feed_dict) losses.append(loss * actual_batch_size) num_correct += float(np.sum(corr)) if is_training_mode and (iter_counter % print_every) == 0: print "Iteration {0}: with minibatch training loss = {1:.3g} and accuracy of {2:.2g}".format( iter_counter, loss, float(np.sum(corr)) / actual_batch_size) iter_counter += 1 accuracy = num_correct / N avg_loss = np.sum(losses) / N print "Epoch {0}, overall loss = {1:.3g} and accuracy = {2:.3g}".format( e + 1, avg_loss, accuracy) if plot_losses: plt.plot(losses) plt.grid(True) plt.title('Epoch {} Loss'.format(e + 1)) plt.xlabel('minibatch number') plt.ylabel('minibatch loss') plt.show() return avg_loss, accuracy
# Train and evaluate model_ft, hist = train_model(model_ft, dataloaders_dict, criterion, optimizer_ft, num_epochs=num_epochs, is_inception=(model_name=="inception")) #WHAT IF I WANT TO TRAIN A MODEL COMPLETELY? #THEN WE TRAIN FROM SCRATCH # Initialize the non-pretrained version of the model used for this run scratch_model,_ = initialize_model(model_name, num_classes, feature_extract=False, use_pretrained=False) scratch_model = scratch_model.to(device) scratch_optimizer = optim.SGD(scratch_model.parameters(), lr=0.001, momentum=0.9) scratch_criterion = nn.CrossEntropyLoss() _,scratch_hist = train_model(scratch_model, dataloaders_dict, scratch_criterion, scratch_optimizer, num_epochs=num_epochs, is_inception=(model_name=="inception")) # Plot the training curves of validation accuracy vs. number # of training epochs_1 for the transfer learning method and # the model trained from scratch ohist = [] shist = [] ohist = [h.cpu().numpy() for h in hist] shist = [h.cpu().numpy() for h in scratch_hist] plt.title("Validation Accuracy vs. Number of Training Epochs") plt.xlabel("Training Epochs") plt.ylabel("Validation Accuracy") plt.plot(range(1,num_epochs+1),ohist,label="Pretrained") plt.plot(range(1,num_epochs+1),shist,label="Scratch") plt.ylim((0,1.)) plt.xticks(np.arange(1, num_epochs+1, 1.0)) plt.legend() plt.show()
FPR_list.append(FPR) Cp = paras_all[count-1] """ plt.plot(FPR_list, Recall_list) plt.plot([0,1],[0,1],'--') plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') roc_title = 'ROC Curve (trace_range:%g, Loss func:%s, Norm:%s )'%(Cp['trace_range'],Cp['loss'],Cp['normalize']) plt.title(roc_title) #plt.show() plt.savefig('./roc_pr/'+str(count)+roc_title+'.png',format='png',dpi=1000) print(count, auc(FPR_list,Recall_list)) plt.close() """ #plt.xlim([0,1]) #plt.ylim([0,1]) #plt.xticks(range(0,1)) print(Precision_list) Cur_p = Precision_list[:] Cur_r = Recall_list[:len(Cur_p)] #print(Cur_p) plt.plot(Cur_r,Cur_p) plt.ylabel('Precision') plt.xlabel('Recall') pr_title = 'PR Curve (trace_range:%g, Loss func:%s, Norm:%s )'%(Cp['trace_range'],Cp['loss'],Cp['normalize']) plt.title(pr_title) #plt.show() plt.savefig('./roc_pr/'+str(count)+pr_title+'.png',format='png',dpi=1000) plt.close() #exit()