def LinRegTest(XTrain, YTrain, close, filename): ''' Using RandomForest learner to predict how much the price will change in 5 days @filename: the file's true name is ML4T-filename @XTrain: the train data for feature @YTrain: the train data for actual price after 5 days @close: the actual close price of Test data set @k: the number of trees in the forest ''' XTest, YTest = TestGenerator(close) #plot thge feature plt.clf() fig = plt.figure() fig.suptitle('The value of features') plt.plot(range(100), XTest[0:100, 0], 'b', label = 'One day price change') plt.plot(range(100), XTest[0:100, 1], 'r', label = 'difference between two day price change') plt.legend(loc = 4) plt.ylabel('Price') filename4 = 'feature' + filename + '.pdf' fig.savefig(filename4, format = 'pdf') LRL = LinRegLearner() cof = LRL.addEvidence(XTrain, YTrain) YLearn = LRL.query(XTest, cof) return YLearn
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 _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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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()
H0_EM_16, H0_EM_50, H0_EM_84 = np.percentile(H0_EM,16), np.percentile(H0_EM,50), np.percentile(H0_EM,84) print('H0: %s: %.0f +%.0f -%.0f' % (name,H0_EM_50, H0_EM_84-H0_EM_50, H0_EM_50-H0_EM_16)) bins = np.arange(-1.5,1.5,0.1) fig = plt.figure(figsize=(9,6)) ax = plt.subplot(111) for ii, name in enumerate(data_struct.keys()): color_name = color_names[ii] kdedir = data_struct[name]["kdedir_distance"] plt.plot(bins, [kde_eval_single(kdedir,[d])[0] for d in bins], color = color_name, linestyle='-.',label=name, linewidth=3, zorder=10) plt.legend(loc=2) plt.xlabel('Relative Error in Distance') plt.ylabel('Probability') #plt.ylim([0,0.10]) plt.grid(True) plt.show() plotName = os.path.join(plotDir,'dist.pdf') plt.savefig(plotName) plt.close() n_live_points = 1000 evidence_tolerance = 0.5 max_iter = 0 title_fontsize = 26 label_fontsize = 30 parameters = ["H0","Om0","0de"] labels = [r'$H_0$',r'$\Omega_m$',r'$\Omega_\Lambda$']
def fit(self, Xs, validation_Xs=None): """ Given two views, create and train the autoencoder. Parameters ---------- Xs : list of array-likes or numpy.ndarray. Xs[0] is View1 and Xs[1] is View2 - Xs length: n_views, only 2 is currently supported for splitAE. - Xs[i] shape: (n_samples, n_features_i) validation_Xs: list of array-likes or numpy.ndarray optional validation data in the same shape of Xs. If printInfo is true, then validation error, calculated with this data, will be printed as the network trains. """ Xs = check_Xs(Xs, multiview=True, enforce_views=2) assert Xs[0].shape[0] >= self.batchSize, """batch size must be <= to number of samples""" assert self.batchSize > 0, """can't have negative batch size""" assert self.trainingEpochs >= 0, """can't train for negative amount of times""" device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") view1 = torch.FloatTensor(Xs[0]) view2 = torch.FloatTensor(Xs[1]) self.view1Encoder_ = _FullyConnectedNet(view1.shape[1], self.hiddenSize, self.numHiddenLayers, self.embedSize).to(device) self.view1Decoder_ = _FullyConnectedNet(self.embedSize, self.hiddenSize, self.numHiddenLayers, view1.shape[1]).to(device) self.view2Decoder_ = _FullyConnectedNet(self.embedSize, self.hiddenSize, self.numHiddenLayers, view2.shape[1]).to(device) self.view1_encoder_ = self.view1Encoder_ self.view1_decoder_ = self.view1Decoder_ self.view2_decoder_ = self.view2Decoder_ if self.printGraph: print("Parameter counts: \nview1Encoder: {:,}\nview1Decoder: {:,}" "\nview2Decoder: {:,}".format( self.view1Encoder_.paramCount(), self.view1Decoder_.paramCount(), self.view2Decoder_.paramCount())) parameters = [ self.view1Encoder_.parameters(), self.view1Decoder_.parameters(), self.view2Decoder_.parameters() ] optim = torch.optim.Adam(itertools.chain(*parameters), lr=self.learningRate) nSamples = view1.shape[0] epochTrainErrors = [] epochTestErrors = [] for epoch in tqdm.tqdm(range(self.trainingEpochs), disable=(not self.printInfo)): batchErrors = [] for batchNum in range(nSamples // self.batchSize): optim.zero_grad() view1Batch = view1[batchNum * self.batchSize:(batchNum + 1) * self.batchSize] view2Batch = view2[batchNum * self.batchSize:(batchNum + 1) * self.batchSize] embedding = self.view1Encoder_(view1Batch.to(device)) view1Reconstruction = self.view1Decoder_(embedding) view2Reconstruction = self.view2Decoder_(embedding) view1Error = torch.nn.MSELoss()(view1Reconstruction, view1Batch.to(device)) view2Error = torch.nn.MSELoss()(view2Reconstruction, view2Batch.to(device)) totalError = view1Error + view2Error totalError.backward() optim.step() batchErrors.append(totalError.item()) if self.printInfo: print("Average train error during epoch {} was {}".format( epoch, np.mean(batchErrors))) epochTrainErrors.append(np.mean(batchErrors)) if validation_Xs is not None: testError = self._testError(validation_Xs) if self.printInfo: print( "Average test error during epoch {} was {}\n".format( epoch, testError)) epochTestErrors.append(testError) if self.printGraph: plt.plot(epochTrainErrors, label="train error") if validation_Xs is not None: plt.plot(epochTestErrors, label="test error") plt.title("Errors during training") plt.xlabel("Epoch") plt.ylabel("Error") plt.legend() plt.show() return self
# PREPARAR LA DATA REGRESIÓN LINEAL SIMPLE # Seleccionamos solamente la columna 5 del dataset # (Número de habitaciones) # Vamos a encontrar la relación entre estas dos variables X = boston.data[:, np.newaxis, 5] # Se definen los datos correspondientes a las etiquetas y = boston.target # Se grafican los datos correspondientes plt.scatter(X, y) plt.xlabel('Número de habitaciones') plt.ylabel('Valor medio') plt.show() # In[28]: # IMPLEMENTACIÓN DE LA REGRESIÓN LINEAL SIMPLE from sklearn.model_selection import train_test_split # Separa los datos de "train" en entrenamiento y prueba # para probar los algoritmos X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) # Se define el algoritmo a utilizar
import matplotlib.pyplot as plt #condiciones de frontera h = 0.01 x_min = 0.0 x_max = 1.0 n_points = int((x_max - x_min)/h) x = np.zeros(n_points) y = np.zeros(n_points) def y_2prima(x, y): return -g/l*np.sin() x[0] = x_min y[0] = 0.0 x[1] = x_min + h y[1] = y[0] + h*f_derivada(x[0], y[0]) for i in range(1, n_points-1): x[i+1] = x[i] + h y[i+1] = y[i-1] + 2*h*f_derivada(x[i], y[i]) plt.plot(x, y, 'ko') plt.plot(x, np.tan(x)) plt.xlabel('x') plt.ylabel('y') plt.show()
# plt.show() x = np.arange(11) y = list(map(lambda num: int(m.pow(num, 2)), x)) #print(x) # [ 0 1 2 3 4 5 6 7 8 9 10] #print(y) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100] #------------------------------------------------------------------------------------------------------------ # Plotting a single figure directly #------------------------------------------------------------------------------------------------------------ plt.figure(1) plt.bar(x, y, color='red') plt.xlabel('X axis') plt.ylabel('Y axis') plt.title('Simple Plotting example') #------------------------------------------------------------------------------------------------------------ # SubPlot by specifying the subplot location #------------------------------------------------------------------------------------------------------------ plt.figure(2) # plt.subplot(2,2,1) subpplot will be of size 2 rows 2 columns . 1 represent the figure number i.e # 1st row 1st column plt.subplot(2, 2, 1) plt.bar(x, y, color='red') # subplot 2x2 , figure number 2 means 2nd row 1st column ax = plt.subplot(2, 2, 2) labels = ["X", "Y"]
print "can't make predictions to use in identifying outliers" ### 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") 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" print "New slope=",reg.coef_[0][0] print "New score=", reg.score(ages_test, net_worths_test)
# Get distance function. dist_func = get_dist_func(network, which=args.dist_func, prediction_model=args.prediction_model) # Get solution using simulated annealing. solution_edgelist, current_fitness, best_fitness, initial_fitness, \ accepted_edgelists, temp_vals, fitness_vals = anneal(network, max_it=args.max_it, temp=args.temp, temp_min=args.temp_min, alpha=args.alpha) # Save list of edge lists for animation. np.save('./results/edgelists/edgelist_tsp_sa.npy', list(map(np.vstack, accepted_edgelists))) nx.write_gpickle(network, './results/networks/network_tsp_sa.gpickle') # Plot temperature and fitness with respect to iteration. plt.plot(temp_vals) plt.xlabel('Iteration') plt.ylabel('Temperature') plt.savefig('./results/plots/temperature_tsp_sa.png') plt.clf() plt.plot(fitness_vals) plt.xlabel('Iteration') plt.ylabel('Fitness') plt.savefig('./results/plots/fitness_tsp_sa.png') # Print best solution fitness. print('Fitness of best found solution: {0:.3f}'.format(best_fitness)) # Print initial best fitness. print('Fitness of initial solution: {0:.3f}'.format(initial_fitness)) # Print increase in fitness. print('Fitness value improved by: {0:.3f}%'.format(100*initial_fitness/best_fitness))
i = i + 1 k = k + 1 while j < len(der): array[k] = der[j] j = j + 1 k = k + 1 return (array) array = [14, 46, 43, 27, 57, 41, 45, 21, 70] print(mergeSort(array)) Tiempo = [] x = [] for i in range(1, 10000000, 50000): array = [random.randint(0, x) for x in range(i)] start_time = time.time() array.sort() array.reverse() mergeSort(array) Tiempo.append(time.time() - start_time) print(time.time() - start_time) x.append(i) print(Tiempo) plt.plot(x, Tiempo, 'ro') plt.ylabel('Tiempo') plt.xlabel('Tamaño')
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()
N = 2**3 #defining the function to be transformed def f(t): return math.cos(2 * math.pi * t / N) #real and imaginary parts of the transform realPart = [] imPart = [] f_w = [] #matrix multiplication of DFT for i in range(N): tempR = 0 tempI = 0 for j in range(N): tempR += math.cos(2 * math.pi * i * j / N) * f(j) / N tempI -= math.sin(2 * math.pi * i * j / N) * f(j) / N realPart.append(tempR) imPart.append(tempI) T = list(range(N)) plt.plot(T, realPart, 'bo', markersize=4, alpha=0.5, label='Real Part') plt.plot(T, imPart, 'rx', markersize=4, alpha=0.5, label='imaginary Part') plt.legend() plt.xlabel('w') plt.ylabel('Fourier Transform Values') plt.show()
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()
def ROC_AUC_analysis(adata, groupby, group, n_genes=100, special_markers=None, coloring='scores', size=12, annotate=False): """Calculate correlation matrix. Calculate a correlation matrix for genes strored in sample annotation using rank_genes_groups.py Parameters ---------- adata : :class:`~scanpy.api.AnnData` Annotated data matrix. groupby : `str` The key of the sample grouping to consider. group : `str`, int, optional (default: None) Group name or index for which the correlation matrix for top_ranked genes should be calculated. If no parameter is passed, ROC/AUC is calculated for all groups n_genes : `int`, optional (default: 100) For how many genes to calculate ROC and AUC. If no parameter is passed, calculation is done for all stored top ranked genes. 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. """ fpr = adata.uns['ROCfpr' + groupby + str(group)] tpr = adata.uns['ROCtpr' + groupby + str(group)] # We dont need thresholds here # TODO: ALlow for comparison with rest, weighting... groups = 'all' groups_order, groups_masks = utils.select_groups(adata, groups, groupby) imask = group 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_genes: 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 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 fig, ax = plt.subplots(figsize=(size, size)) ax.scatter(rate_rest, rate_group, color=colors, s=s) plt.xlabel("False positive rate") plt.ylabel("True positive rate") if annotate is True: for i, txt in enumerate(name_list): plt.annotate(txt, (rate_rest[i], rate_group[i])) # TODO: Add AUC # Now annotate the lines for all genes: # TODO: Until now, everything requires same number of n (i.e. all in name list). Shouldn't be the case. Resolve. for i, j in enumerate(name_list): plt.plot(fpr[name_list[i]], tpr[name_list[i]], color=colors[i]) plt.show()
#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')
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()
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)
his = np.zeros((max_iter, 1)) 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
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
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)
indx += 1 #grafico de barras para % Maior Array ax = plt.subplot(rows, cols, indx) graf.gerarBarplot(ax=ax, x='probabilidade_erro', y='percentual_maior_array', hue='algoritmo', data=data, title='% Maior Array', palette=color_map) graf.inserirValoresNasBarras(ax, df_means['percentual_maior_array'].values) indx += 1 # grafico de barras para a média das % Desordenados ax = plt.subplot(rows, cols, indx) plt.ylabel('Média - percentual_k_unordered') data_graf = df_means['percentual_k_unordered'].unstack() data_graf.plot(kind='bar', ax=ax, color=color_map) graf.inserirValoresNasBarras(ax, df_means['percentual_k_unordered'].values) indx += 1 # grafico de barras para a média das % Maior Array ax = plt.subplot(rows, cols, indx) plt.ylabel('Média - percentual_maior_array') data_graf = df_means['percentual_maior_array'].unstack() data_graf.plot(kind='bar', ax=ax, color=color_map) graf.inserirValoresNasBarras(ax, df_means['percentual_maior_array'].values) indx += 1 # grafico de barras para a std_dev das % Desordenados ax = plt.subplot(rows, cols, indx)
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()
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()
# 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 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')
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()
tab_x = [] tab_y = [] tab_col = [] array = [] for i in range(0, tab.size): tab_x.append(tab[i][0]) tab_y.append(tab[i][1]) tab_col.append(tab[i][2]) array.append([tab[i][0], tab[i][1]]) begin = time.process_time() clusterer = hdbscan.HDBSCAN(min_cluster_size=10) cluster_labels = clusterer.fit_predict(array) silhouette_avg = silhouette_score(array, cluster_labels) graph.append(silhouette_avg) t = time.process_time() - begin temps.append(t) titre = nom_datasets[j] plt.title(titre) plt.scatter(tab_x, tab_y, c=cluster_labels) matplotlib.pyplot.show() y_axis = [temps, graph] y_label = ['Temps', 'Silhouette Avg'] for i in range(0, 2): plt.plot(nom_datasets, y_axis[i]) plt.ylabel(y_label[i]) plt.title(y_label[i]) plt.show()
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")
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')
#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')
# 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")