def yplot(data, limits=[None,None], fname='', xval=[0.5], label='', loc='upper left', ext='png'): """Make transverse plots of data Usage: yplot(data, limits=[None,None], fname='', xval=[0.5], label='', loc='upper left', ext='ext') xval is a list of axial distances in units of nx If no filename is specified a plot is displayed The special filename 'M' turns "hold" on (for multiplots) File format is ext (default is png) """ nx, ny = data.shape[0], data.shape[1] y = np.array(range(ny)) + 0.5 for x in xval: ix = int(x*nx) plt.plot(y, data[ix,:], label=label+" : "+"x="+str(x)) if (fname == 'M'): plt.hold=True else: plt.axis([0,data.shape[1],limits[0],limits[1]]) plt.legend(loc=loc) plt.hold=False if len(fname) == 0: plt.show() else: plt.savefig(fname+'-y.'+ext, format=ext) plt.close()
def plot_predict_is(self,h=5,**kwargs): """ Plots forecasts with the estimated model against data (Simulated prediction with data) Parameters ---------- h : int (default : 5) How many steps to forecast Returns ---------- - Plot of the forecast against data """ figsize = kwargs.get('figsize',(10,7)) plt.figure(figsize=figsize) date_index = self.index[-h:] predictions = self.predict_is(h) data = self.data[-h:] t_params = self.transform_z() plt.plot(date_index,np.abs(data-t_params[-1]),label='Data') plt.plot(date_index,predictions,label='Predictions',c='black') plt.title(self.data_name) plt.legend(loc=2) plt.show()
def 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 plot_robots_time_micmac(deploy_robots_mic, deploy_robots_mac, species_ind, node_ind): fig = plt.figure() #plt.axis('equal') num_nodes = deploy_robots_mic.shape[0] num_iter = deploy_robots_mic.shape[1] delta_t = 0.04 x = np.arange(0, num_iter) * delta_t # plot evolution of robot population over time labeled = False for n in node_ind: y = deploy_robots_mic[n,:,species_ind] y2 = deploy_robots_mac[n,:,species_ind] if (not labeled): labeled = True plt.plot(x,y,color='green', label='Microscopic') plt.plot(x,y2, color='red', label='Macroscopic') else: plt.plot(x,y,color='green') plt.plot(x,y2, color='red') # plot legend and labels plt.legend(loc='upper right', shadow=False, fontsize='large') plt.xlabel('Time [s]') plt.ylabel('Number of robots') #plt.show() return fig
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 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 plotWeightsAllTrials(data): """allWeights contains arrays - each of which is a trial Each trial should contain arrays each containing values of said weight for each iteration q is an approximate for the grid """ allWeights = data['weights'] allErrors = data['errors'] trial = 0 for trialVals in allWeights: assert array(trialVals).shape[1] == len(allErrors[0]), 'vals not transposd %s' % str(array(trialVals).shape) params_n = len(trialVals) q = int(sqrt(params_n))+1 trialError = allErrors[trial][-1] for i in range(params_n): s = subplot(q, q-1, i+1) s.axes.get_xaxis().set_visible(False) if i == params_n-1: s.axes.get_xaxis().set_visible(True) # s.xaxis.set_major_locator(MultipleLocator(20)) s.axes.get_yaxis().set_visible(False) if i == 0: s.axes.get_yaxis().set_visible(True) s.yaxis.set_major_locator(MultipleLocator(2)) subplots_adjust(hspace=0.1, wspace=0.1) plot(trialVals[i], label=str(trialError)) ylim(-4,4) trial+=1 plt.legend(bbox_to_anchor=(1.5, 1), prop={'size':8})
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 statistics_charts(self): if plt is None: return for chart in self.stats_charts: if chart["type"] == "plot": fig = plt.figure(figsize=(8, 2)) for xdata, ydata, label in chart["data"]: plt.plot(xdata, ydata, "-", label=label) plt.legend(loc="center left", bbox_to_anchor=(1, 0.5)) elif chart["type"] == "timeline": fig = plt.figure(figsize=(16, 2)) for i, (starts, stops, label) in enumerate(chart["data"]): plt.hlines([i] * len(starts), starts, stops, label=label) plt.ylim(-1, len(chart["data"])) elif chart["type"] == "bars": fig = plt.figure(figsize=(16, 4)) plt.bar(range(len(chart["data"])), chart["data"]) elif chart["type"] == "boxplot": fig = plt.figure(figsize=(16, 4)) plt.boxplot(chart["data"]) else: raise Exception("Unknown chart") png = serialize_fig(fig) yield chart["name"], html_embed_img(png)
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 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 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 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 test_get_obs(self): plt.figure() ant_sigs = antennas.antennas_signal(self.ants, self.ant_models, self.sources, self.rad.timebase) rad_sig_full = self.rad.sampled_signal(ant_sigs[0, :], 0) obs_full = self.rad.get_full_obs(ant_sigs, self.utc_date, self.config) ant_sigs_simp = antennas.antennas_simplified_signal(self.ants, self.ant_models, self.sources, self.rad.baseband_timebase, self.rad.int_freq) obs_simp = self.rad.get_simplified_obs(ant_sigs_simp, self.utc_date, self.config) freqs, spec_full_before_obs = spectrum.plotSpectrum(rad_sig_full, self.rad.ref_freq, label='full_before_obs_obj', c='blue') freqs, spec_full = spectrum.plotSpectrum(obs_full.get_antenna(1), self.rad.ref_freq, label='full', c='cyan') freqs, spec_simp = spectrum.plotSpectrum(obs_simp.get_antenna(1), self.rad.ref_freq, label='simp', c='red') plt.legend() self.assertTrue((spec_full_before_obs == spec_full).all(), True) plt.figure() plt.plot(freqs, (spec_simp-spec_full)/spec_full) plt.show() print len(obs_full.get_antenna(1)), obs_full.get_antenna(1).mean() print len(obs_simp.get_antenna(1)), obs_simp.get_antenna(1).mean()
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 _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 ecdf_by_observed_label(y_true, y_pred): """Plots the empirical cumulative density functions by observed label. Parameters ---------- y_true : array_like Observed labels, either 0 or 1. y_pred : array_like Predicted probabilities, floats on [0, 1]. Notes ----- .. plot:: pyplots/ecdf_by_observed_label.py """ x = np.linspace(0, 1) ecdf = ECDF(y_pred[y_true == 0]) y_0 = ecdf(x) ecdf = ECDF(y_pred[y_true == 1]) y_1 = ecdf(x) plt.step(x, y_0, label='Observed label 0') plt.step(x, y_1, label='Observed label 1') plt.xlabel('Predicted Probability') plt.ylabel('Proportion') plt.title('Empirical Cumulative Density Functions by Observed Label') plt.legend(loc='lower right')
def roc_plot(y_true, y_pred): """Plots a receiver operating characteristic. Parameters ---------- y_true : array_like Observed labels, either 0 or 1. y_pred : array_like Predicted probabilities, floats on [0, 1]. Notes ----- .. plot:: pyplots/roc_plot.py References ---------- .. [1] Pedregosa, F. et al. "Scikit-learn: Machine Learning in Python." *Journal of Machine Learning Research* 12 (2011): 2825–2830. .. [2] scikit-learn developers. "Receiver operating characteristic (ROC)." Last modified August 2013. http://scikit-learn.org/stable/auto_examples/plot_roc.html. """ fpr, tpr, __ = roc_curve(y_true, y_pred) roc_auc = auc(fpr, tpr) plt.plot(fpr, tpr, label='ROC curve (area = {:0.2f})'.format(roc_auc)) plt.plot([0, 1], [0, 1], 'k--') plt.xlim([0, 1]) plt.ylim([0, 1]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('Receiver Operating Characteristic') plt.legend(loc='lower right')
def make_line( x, y, f_name, title=None, legend=None, x_label=None, y_label=None, x_ticks=None, y_ticks=None, ): fig = plt.figure() if title is not None: plt.title(title, fontsize=16) if x_label is not None: plt.ylabel(x_label) if y_label is not None: plt.xlabel(y_label) if x_ticks is not None: plt.xticks(x, x_ticks) if y_ticks is not None: plt.yticks(y_ticks) if isinstance(y[0], list): for data in y: plt.plot(x, data) else: plt.plot(x, y) if legend is not None: plt.legend(legend) plt.savefig(f_name) plt.close(fig)
def _plot(self,names,title,style,when=0,showLegend=True): if isinstance(names,str): names = [names] assert isinstance(names,list) legend = [] for name in names: assert isinstance(name,str) legend.append(name) # if it's a differential state if name in self.xNames: index = self.xNames.index(name) ys = np.squeeze(self._log['x'])[:,index] ts = np.arange(len(ys))*self.Ts plt.plot(ts,ys,style) if name in self.outputNames: index = self.outputNames.index(name) ys = np.squeeze(self._log['outputs'][name]) ts = np.arange(len(ys))*self.Ts plt.plot(ts,ys,style) if title is not None: assert isinstance(title,str), "title must be a string" plt.title(title) plt.xlabel('time [s]') if showLegend is True: plt.legend(legend) plt.grid()
def visualize(u1, t1, u2, t2, U, omega): plt.figure(1) plt.plot(t1, u1, 'r--o') t_fine = np.linspace(0, t1[-1], 1001) # мелкая сетка для точного решения u_e = u_exact(t_fine, U, omega) plt.hold('on') plt.plot(t_fine, u_e, 'b-') plt.legend([u'приближенное', u'точное'], loc='upper left') plt.xlabel('$t$') plt.ylabel('$u$') tau = t1[1] - t1[0] plt.title('$\\tau = $ %g' % tau) umin = 1.2*u1.min(); umax = -umin plt.axis([t1[0], t1[-1], umin, umax]) plt.savefig('tmp1.png'); plt.savefig('tmp1.pdf') plt.figure(2) plt.plot(t2, u2, 'r--o') t_fine = np.linspace(0, t2[-1], 1001) # мелкая сетка для точного решения u_e = u_exact(t_fine, U, omega) plt.hold('on') plt.plot(t_fine, u_e, 'b-') plt.legend([u'приближенное', u'точное'], loc='upper left') plt.xlabel('$t$') plt.ylabel('$u$') tau = t2[1] - t2[0] plt.title('$\\tau = $ %g' % tau) umin = 1.2 * u2.min(); umax = -umin plt.axis([t2[0], t2[-1], umin, umax]) plt.savefig('tmp2.png'); plt.savefig('tmp2.pdf')
def make_overview_plot(filename, title, noip_arrs, ip_arrs): plt.title("Inner parallelism - " + title) plt.ylabel('Time (ms)', fontsize=12) x = 0 barwidth = 0.5 bargroupspacing = 1.5 for z in zip(noip_arrs, ip_arrs): noip,ip = z noip_mean,noip_conf = conf_stats(noip) ip_mean,ip_conf = conf_stats(ip) b_noip = plt.bar(x, noip_mean, barwidth, color='r', yerr=noip_conf, ecolor='black', alpha=0.7) x += barwidth b_ip = plt.bar(x, ip_mean, barwidth, color='b', yerr=ip_conf, ecolor='black', alpha=0.7) x += bargroupspacing plt.xticks([0.5, 2.5, 4.5], ['50k', '100k', '200k'], rotation='horizontal') fontP = FontProperties() fontP.set_size('small') plt.legend([b_noip, b_ip], \ ('no inner parallelism', 'inner parallelism'), \ prop=fontP, loc='upper center', bbox_to_anchor=(0.5, -0.05), fancybox=True, shadow=True, ncol=2) plt.ylim([0,62000]) plt.savefig(output_file(filename)) plt.clf()
def create_lib_complexity_plot(fastqFile,mer_size=20): #def calculate_library_complexity(fastqfiles): report_uniqness_after_N_reads = 100000 #middle_mer_start randm_mer_fastq_complexity = {} starting_mer_fastq_complexity = {} count_uniq_random_mers =0 count_uniq_starting_mers = 0 bucket_percent_uniq_random_mers = [] bucket_percent_uniq_starting_mers = [] #output file name #fastq_prefix = get_guessed_fastq_prefix(fastqFile) fastq_prefix = fastqFile + '_kmer' + str(mer_size) baseDir = os.path.dirname(fastqFile) or '.' lib_complexity_plot_name = baseDir + '/' + fastq_prefix + '_lib_complexity_plot.png' lib_complexity_numbers_file = baseDir + '/' + fastq_prefix + '_lib_complexity.data' fh = open(lib_complexity_numbers_file,'w') for count,read in enumerate(get_genericfastq_iterator(fastqFile)): if count % report_uniqness_after_N_reads == 0 and count > 0: uniq_random_mer_percent = round( (count_uniq_random_mers/float(count+1))*100, 2) uniq_starting_mer_percent = round( (count_uniq_starting_mers/float(count+1))*100, 2) bucket_percent_uniq_random_mers.append(uniq_random_mer_percent) bucket_percent_uniq_starting_mers.append( uniq_starting_mer_percent ) outline = '%s \t %s \t %s \t %s \t %s \n' % (count, count_uniq_random_mers, count_uniq_starting_mers, uniq_random_mer_percent, uniq_starting_mer_percent) fh.write(outline) starting_mer = read.seq[:mer_size] random_mer_loc=random.randint(0,(len(read.seq)-mer_size)) random_mer = read.seq[random_mer_loc:random_mer_loc+mer_size] if randm_mer_fastq_complexity.get(random_mer,None) is None: count_uniq_random_mers +=1 randm_mer_fastq_complexity[random_mer] = 1 if starting_mer_fastq_complexity.get(starting_mer,None) is None: count_uniq_starting_mers += 1 starting_mer_fastq_complexity[starting_mer] = 1 fh.close() #plotting the library complexity x_axis = [ x*report_uniqness_after_N_reads for x in xrange(1,len(bucket_percent_uniq_random_mers)+1) ] plt.plot(x_axis,bucket_percent_uniq_random_mers,label='rdm_%dmer' % mer_size) plt.plot(x_axis,bucket_percent_uniq_starting_mers,label='start_%dmer' % mer_size) plt.ylabel('percent unqiue') plt.xlabel('Read Counts') plt.legend() plt.savefig(lib_complexity_plot_name)
def plot_multiple_likelhood_values(likelihood_arr, time_axis=0, x=None, save_path='', title='', xlabel='', ylabel='', colors=['red', 'green', 'blue'], labels=['red', 'green', 'blue'], linestyle=['-', '-', '-', '-'], marker=['.', '.', '.', '.'], legend_fontsize=18, xlabel_fontsize=20, ylabel_fontsize=20, figsize=(6,5), legend_loc='upper right'): """Plot multiple results. NOTE: The time for each result should be along the time axis. """ assert len(likelihood_arr) <= len(colors), "Missing colors for plot." mean_likelihood, std_likelihood = [], [] if time_axis >= 0: for l in likelihood_arr: mean_likelihood.append(np.mean(l, axis=time_axis)) std_likelihood.append(np.std(l, axis=time_axis)) else: assert len(likelihood_arr[0].shape) == 1, \ "Can only plot vector with -ve time axis" mean_likelihood = likelihood_arr std_likelihood = [np.zeros(likelihood_arr[0].shape)] * len(likelihood_arr) if x is None: x = range(len(mean_likelihood[0])) plots = [] fig = plt.figure(figsize=figsize) ax = fig.add_subplot(111) for i in xrange(len(mean_likelihood)): m, s = mean_likelihood[i], std_likelihood[i] c = colors[i] p, = ax.plot(x, m, 'k', color=c, label=labels[i], linestyle=linestyle[i], # marker=marker[i], ) ax.fill_between(x, m - s, m + s, alpha=0.2, edgecolor=c, facecolor=c, linewidth=4, linestyle='dashdot', antialiased=True) plots.append(p) plt.legend(handles=plots, fontsize=legend_fontsize, loc=legend_loc) ax.set_title(title) ax.set_xlabel(xlabel, fontsize=xlabel_fontsize) ax.set_ylabel(ylabel, fontsize=ylabel_fontsize) if len(save_path) > 0: plt.savefig(save_path, bbox_inches="tight") plt.show()
def make_histograms(truescores, fpscores, title): plt.hist(truescores, normed=True, histtype='step', linewidth=3, label="True Events, %d" % len(truescores)) plt.hist(fpscores, normed=True, histtype='step', linewidth=3, label="FP Events, %d" % len(fpscores)) plt.legend() plt.xlabel("Likelihood Score") plt.ylabel("Density of events") plt.title("Histogram of TP and FP Event Likelihoods")
def plotPath(self, seq, poses_gt, poses_result): plot_keys = ["Ground Truth", "Ours"] fontsize_ = 20 plot_num = -1 poses_dict = {} poses_dict["Ground Truth"] = poses_gt poses_dict["Ours"] = poses_result fig = plt.figure() ax = plt.gca() ax.set_aspect('equal') for key in plot_keys: pos_xz = [] # for pose in poses_dict[key]: for frame_idx in sorted(poses_dict[key].keys()): pose = poses_dict[key][frame_idx] pos_xz.append([pose[0, 3], pose[2, 3]]) pos_xz = np.asarray(pos_xz) plt.plot(pos_xz[:, 0], pos_xz[:, 1], label=key) plt.legend(loc="upper right", prop={'size': fontsize_}) plt.xticks(fontsize=fontsize_) plt.yticks(fontsize=fontsize_) plt.xlabel('x (m)', fontsize=fontsize_) plt.ylabel('z (m)', fontsize=fontsize_) fig.set_size_inches(10, 10) png_title = "sequence_{:02}".format(seq) plt.savefig(self.plot_path_dir + "/" + png_title + ".pdf", bbox_inches='tight', pad_inches=0)
def main(): parser = argparse.ArgumentParser(description="""Compute subset of users who rated at least 10 movies and plot fraction of users satisfied as a function of inventory size.""") parser.add_argument("infilename", help="Read from this file.", type=open) args = parser.parse_args() ratings = read_inputs(args.infilename) ratings = ratings.drop("timestamp", axis=1) movie_rankings = find_movie_rankings(ratings) ratings = ratings.drop("rating", axis=1) user_rankings = find_user_rankings(ratings, movie_rankings) num_users = user_rankings.user_id.unique().size num_movies = movie_rankings.shape[0] user_rankings = clean_rankings(user_rankings) us_levels_100 = find_satisfaction(user_rankings, num_users, num_movies) us_levels_90 = find_satisfaction(user_rankings, num_users, num_movies, satisfaction_level=0.9) rc('text', usetex=True) plt.title('Percent of Users Satisfied vs Inventory Size in the MovieLens Dataset') plt.xlabel('Inventory Size') plt.ylabel('Percent of Users Satisfied') plt.plot(us_levels_100, 'b', label=r'$100\% \ satisfaction$') plt.plot(us_levels_90, 'r--', label=r'$90\% \ satisfaction$') plt.legend() d = datetime.datetime.now().isoformat() plt.savefig('user_satisfaction_%s.png' % d)
def plot_robots_ratio_time_micmac(deploy_robots_mic, deploy_robots_mac, deploy_robots_desired, delta_t): plot_option = 0 # 0: ratio, 1: cost num_iter = deploy_robots_mic.shape[1] total_num_robots = np.sum(deploy_robots_mic[:,0,:]) diffmic_sqs = np.zeros(num_iter) diffmac_sqs = np.zeros(num_iter) diffmic_rat = np.zeros(num_iter) diffmac_rat = np.zeros(num_iter) for t in range(num_iter): diffmic = np.abs(deploy_robots_mic[:,t,:] - deploy_robots_desired) diffmac = np.abs(deploy_robots_mac[:,t,:] - deploy_robots_desired) diffmic_rat[t] = np.sum(diffmic) / total_num_robots diffmic_sqs[t] = np.sum(np.square(diffmic)) diffmac_rat[t] = np.sum(diffmac) / total_num_robots diffmac_sqs[t] = np.sum(np.square(diffmac)) x = np.arange(0, num_iter) * delta_t if(plot_option==0): l1 = plt.plot(x,diffmic_rat) l2 = plt.plot(x,diffmac_rat) if(plot_option==1): l1 = plt.plot(x,diffmic_sqs) l2 = plt.plot(x,diffmac_sqs) plt.xlabel('time [s]') plt.ylabel('ratio of misplaced robots') plt.legend((l1, l2),('Micro','Macro')) plt.show()
def make_bar( x, y, f_name, title=None, legend=None, x_label=None, y_label=None, x_ticks=None, y_ticks=None, ): fig = plt.figure() if title is not None: plt.title(title, fontsize=16) if x_label is not None: plt.ylabel(x_label) if y_label is not None: plt.xlabel(y_label) if x_ticks is not None: plt.xticks(x, x_ticks) if y_ticks is not None: plt.yticks(y_ticks) plt.bar(x, y, align="center") if legend is not None: plt.legend(legend) plt.savefig(f_name) plt.close(fig)
print(df.info()) print(df['price_usd'].describe()) df = df.loc[df['price_usd'] < 5584] #time series visualizations df.plot(x='date_time', y='price_usd', figsize=(12, 6)) plt.xlabel('Date time') plt.ylabel('Price in USD') plt.title('Time Series of room price by date time of search') a = df.loc[df['srch_saturday_night_bool'] == 0, 'price_usd'] b = df.loc[df['srch_saturday_night_bool'] == 1, 'price_usd'] plt.figure(figsize=(10, 6)) plt.hist(a, bins=50, alpha=0.5, label='Search Non-Sat Night') plt.hist(b, bins=50, alpha=0.5, label='Search Sat Night') plt.legend(loc='upper right') plt.xlabel('Price') plt.ylabel('Count') #k-means clustering data = df[['price_usd', 'srch_booking_window', 'srch_saturday_night_bool']] n_cluster = range(1, 20) kmeans = [cluster.KMeans(n_clusters=i).fit(data) for i in n_cluster] scores = [kmeans[i].score(data) for i in range(len(kmeans))] fig, ax = plt.subplots(figsize=(10, 6)) ax.plot(n_cluster, scores) plt.xlabel('Number of Clusters') plt.ylabel('Score') plt.title('Elbow Curve')
# Printing out the training progress train_loss = MSE(network.run(train_features).T, train_targets['cnt'].values) val_loss = MSE(network.run(val_features).T, val_targets['cnt'].values) sys.stdout.write("\rProgress: {:2.1f}".format(100 * ii/float(iterations)) + "% ... Training loss: " + str(train_loss)[:5] + " ... Validation loss: " + str(val_loss)[:5]) sys.stdout.flush() losses['train'].append(train_loss) losses['validation'].append(val_loss) # In[45]: plt.plot(losses['train'], label='Training loss') plt.plot(losses['validation'], label='Validation loss') plt.legend() _ = plt.ylim() # ## 检查预测结果 # # 使用测试数据看看网络对数据建模的效果如何。如果完全错了,请确保网络中的每步都正确实现。 # In[47]: fig, ax = plt.subplots(figsize=(8,4)) mean, std = scaled_features['cnt'] predictions = network.run(test_features).T*std + mean ax.plot(predictions[0], label='Prediction')
def statistiques(request): #On instancie les variables globales: #Les dates dateDebut = datetime.now() dateFin = datetime.now() #Une variable String à vide emotionStats = '' #Une liste contenant les codes ""émotions", ces codes correspondent respectivement à : Joie, Peur, Tristess, Colère, Dégoût listeEmotion = ["JO", 'PE', "TR", "CO", "DE"] #Une liste qui contiendra les QuerySets à trier listeDonnees = [0, 0, 0, 0, 0] #Si on envoie la méthode POST... if request.method == "POST": #... alors on déclare une variable comme étant le StatistiquesForm associé à cet envoi formStatistiques = StatistiquesForm(request.POST) #On instancie les colonnes de l'utilisateur connecté colPeriode = Colonne.objects.filter(utilisateur=request.user) #Si le form est valide on déroule ! if formStatistiques.is_valid(): stats = formStatistiques.save(commit=False) #On sauvegarde dans la BDD, le temps de récupérer les valeurs des champs stats.save() #On récupère la date de début du formulaire dateDebut = stats.dateDeDebut #Comme l'heure est initialisée à minuit on récupère la date de fin du formulaire et on lui ajoute un jour dateFin = stats.dateDeFin + timedelta(days=1) #On récupère la valeur du champ émotion emotionStats = stats.emotion #On supprime directement les valeurs dans la base de données pour ne pas créer de conflits potentiels lors d'une future utilisation stats.delete() #... puis on récupère les colonnes ayant une date contenue entre la date de début et la date de fin entrées dans le formulaire colPeriode = Colonne.objects.filter(date_event__range=(dateDebut, dateFin)) #Si l'utilisateur a choisi l'option 'Toutes' pour le champ 'Émotion' du formulaire nous allons lui afficher les moyennes les intensités respectivement pour chacune des émotions if emotionStats == 'TO': #On récupère toutes les queryset des colonnes pour chaque Emotion : #On délcare un compteur qui servira à parcourir la liste i = 0 #On instancie une nouvelle liste catégorisant les colonnes selon leur attribut emotion for emotions in listeEmotion: listeDonnees[i] = colPeriode.filter( emotion__contains=emotions) i = i + 1 nbrCO = listeDonnees[3].count() #On instancie une liste pour chaque émotion, chaque liste contient les colJO = listeDonnees[0] listeJO = list(colJO) colPE = listeDonnees[1] listePE = list(colPE) colTR = listeDonnees[2] listeTR = list(colTR) colCO = listeDonnees[3] listeCO = list(colCO) colDE = listeDonnees[4] listeDE = list(colDE) #On instancie également les variables qui nous serviront pour calculer les moyennes des attributs d'intensités automatique et alternative intAutJO = 0 intAltJO = 0 intAutPE = 0 intAltPE = 0 intAutTR = 0 intAltTR = 0 intAutCO = 0 intAltCO = 0 intAutDE = 0 intAltDE = 0 #Pour chaque liste et donc chaque émotion on fait la somme des attributs d'intensité automatique et alternative i = 0 for elt in listeJO: intAutJO = intAutJO + listeJO[i].intensiteAut intAltJO = intAltJO + listeJO[i].intensiteAlt i = i + 1 i = 0 for elt in listePE: intAutPE = intAutPE + listePE[i].intensiteAut intAltPE = intAltPE + listePE[i].intensiteAlt i = i + 1 i = 0 for elt in listeTR: intAutTR = intAutTR + listeTR[i].intensiteAut intAltTR = intAltTR + listeTR[i].intensiteAlt i = i + 1 i = 0 for elt in listeCO: intAutCO = intAutCO + listeCO[i].intensiteAut intAltCO = intAltCO + listeCO[i].intensiteAlt i = i + 1 i = 0 for elt in listeDE: intAutDE = intAutDE + listeDE[i].intensiteAut intAltDE = intAltDE + listeDE[i].intensiteAlt i = i + 1 #On instancie les variables des moyennes de l'intensité Automatique et Alternative pour les récupérer plus tard moyIntAutJO = 0 moyIntAltJO = 0 #On compte le nombre d'éléments dans la liste pour calculer la moyenne compteurJO = len(listeJO) #Pour chaque émotion on calcule les moyennes de l'intensite Automatique et Alternative si l'intensite !=0, c'est à dire s'il existe au moins une colonne avec une telle émotion if compteurJO > 0: moyIntAutJO = intAutJO / compteurJO moyIntAltJO = intAltJO / compteurJO moyIntAutPE = 0 moyIntAltPE = 0 compteurPE = len(listePE) if compteurPE > 0: moyIntAutPE = intAutPE / compteurPE moyIntAltPE = intAltPE / compteurPE moyIntAutTR = 0 moyIntAltTR = 0 compteurTR = len(listeTR) if compteurTR > 0: moyIntAutTR = intAutTR / compteurTR moyIntAltTR = intAltTR / compteurTR moyIntAutCO = 0 moyIntAltCO = 0 compteurCO = len(listeCO) if compteurCO > 0: moyIntAutCO = intAutCO / compteurCO moyIntAltCO = intAltCO / compteurCO moyIntAutDE = 0 moyIntAltDE = 0 compteurDE = len(listeDE) if compteurDE > 0: moyIntAutDE = intAutDE / compteurDE moyIntAltDE = intAltDE / compteurDE #On dessine le graphique avec matplotlib f = plt.figure() aut = [ moyIntAutJO, moyIntAutPE, moyIntAutTR, moyIntAutCO, moyIntAutDE ] alt = [ moyIntAltJO, moyIntAltPE, moyIntAltTR, moyIntAltCO, moyIntAltDE ] plt.title('Intensités moyennes pour chaque émotion du : ' + dateDebut.strftime("%d/%m/%Y") + ' au ' + dateFin.strftime("%d/%m/%Y")) plt.ylabel('Intensité Moyenne') plt.ylim(0, 10) barWidth = 0.5 r1 = range(len(aut)) r2 = [x + barWidth for x in r1] b1 = plt.bar(r1, aut, width=barWidth, color=['red' for i in aut], edgecolor='none', linewidth=2) b2 = plt.bar(r2, alt, width=barWidth, color=['green' for i in aut], edgecolor='none', linewidth=4) plt.xticks([0.25, 1.25, 2.25, 3.25, 4.25], ['Joie', 'Peur', 'Tristesse', 'Colère', 'Dégoût']) plt.legend((b1[0], b2[0]), ("Pensée Automatique", "Pensée Alternative")) #On retourne le graphique de statistiques comme HttpResponse canvas = FigureCanvasAgg(f) response = HttpResponse(content_type='image/png') canvas.print_png(response) matplotlib.pyplot.close(f) return response return render(request, 'colonnes/statistiques.html', {'formStatistiques': formStatistiques}) else: formStatistiques = StatistiquesForm() return render(request, 'colonnes/statistiques.html', {'formStatistiques': formStatistiques})
def save_train_results(Train_Loss, Train_Q_mean, Train_Q_max_mean, curr_rl_config): # plot and save the training results """ Save and Plot the Training results """ # get the current training parameter values from curr_rl_config Batch_Size = curr_rl_config.Batch_Size Num_Train_Step = curr_rl_config.Num_Train_Steps Num_Episodes = curr_rl_config.Num_Episodes Num_D2D_feedback = curr_rl_config.Num_Feedback GAMMA = curr_rl_config.Gamma V2I_Weight = curr_rl_config.v2i_weight # check the saving process save_flag = False # Train_Loss size: [num_episodes x num_train_steps] Train_Loss_per_Episode = np.sum(Train_Loss, axis=1)/Num_Train_Step # compute the Target Q value Train_Q_mean_per_Episode = np.sum(Train_Q_mean, axis=1) / Num_Train_Step Train_Q_max_mean_per_Episode = np.sum(Train_Q_max_mean, axis=1) / Num_Train_Step # save results in their corresponding simulation parameter settings curr_sim_set = 'Train-Result' + '-Feedback-' + str(Num_D2D_feedback) + '-BatchSize-' + str(Batch_Size) \ + '-Gamma-' + str(GAMMA) + '-V2Iweight-' + str(V2I_Weight) folder = os.getcwd() + '\\' + curr_sim_set + '\\' if not os.path.exists(folder): os.makedirs(folder) print('Create the new folder in train main ', folder) curr_Result_Dir = folder # plot the results x = range(Num_Episodes) y = Train_Loss_per_Episode plt.figure() plt.plot(x, y, color='red', label='C-Decision') plt.xlabel("Number of Episodes") plt.ylabel("Training Loss") plt.grid(True) plt.title("RL Training Loss") plt.legend() # save results to the file Curr_OS = os.name if Curr_OS == 'nt': print('Current OS is Windows!') Fig_Dir = curr_Result_Dir Fig_Name = 'Training-LOSS-plot' + '-Episode-' + str(Num_Episodes) + '-Step-' + str(Num_Train_Step) \ + '-Batch-' + str(Batch_Size) + '.png' Fig_Para = Fig_Dir + Fig_Name plt.savefig(Fig_Para, dpi=600) # save another format if necessary Fig_Name1 = 'Training-LOSS-plot' + '-Episode-' + str(Num_Episodes) + '-Step-' + str(Num_Train_Step) \ + '-Batch-' + str(Batch_Size) + '.eps' Fig_Para1 = Fig_Dir + Fig_Name1 plt.savefig(Fig_Para1) # plot the Q max mean results of Target value x = range(Num_Episodes) y = Train_Q_mean_per_Episode y1 = Train_Q_max_mean_per_Episode plt.figure() plt.plot(x, y, color='red', label='Q mean') plt.plot(x, y1, color='blue', label='Q-max mean') plt.xlabel("Number of Episodes") plt.ylabel("Return per Episode") plt.grid(True) plt.title("Q Function in Training") plt.legend() # save the figure Fig_Name = 'Train-Q-Function-plot' + '-Episode-' + str(Num_Episodes) + '-Step-' + str(Num_Train_Step) \ + '-Batch-' + str(Batch_Size) + '.png' Fig_Para = Fig_Dir + Fig_Name plt.savefig(Fig_Para, dpi=600) Fig_Name1 = 'Train-Q-Function-plot' + '-Episode-' + str(Num_Episodes) + '-Step-' + str(Num_Train_Step) \ + '-Batch-' + str(Batch_Size) + '.eps' Fig_Para1 = Fig_Dir + Fig_Name1 plt.savefig(Fig_Para1, dpi=600) # save the results to file if Curr_OS == 'nt': # print('Current OS is Windows!') Data_Dir = curr_Result_Dir Data_Name = 'Training-Result' + '-Episode-' + str(Num_Episodes) + '-Step-' + str(Num_Train_Step) \ + '-Batch-' + str(Batch_Size) + '.pkl' Data_Para = Data_Dir + Data_Name # open data file file_to_open = open(Data_Para, 'wb') # write train results to data file pickle.dump((Train_Loss_per_Episode, Train_Loss, Train_Q_mean_per_Episode, Train_Q_mean, Train_Q_max_mean_per_Episode, Train_Q_max_mean), file_to_open) file_to_open.close() save_flag = True return save_flag
def main(): hash_table_size = 1000 percents = (10, 20, 30, 40, 50, 60, 70, 75, 80, 85, 87, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100) test_values = (hash_table_size, percents) pickle.dump(test_values, open("test_input.pickle", "w")) start_tests() results = open("test_result.pickle", "r") chained_results = pickle.load(results) open_results = pickle.load(results) open_collisions = open_results[0] open_inspections = open_results[1] # Chained hash table collisions plot x_axis = percents y_min = chained_results[:, 0] y_avg = chained_results[:, 1] y_max = chained_results[:, 2] plt.plot(x_axis, y_min) plt.plot(x_axis, y_avg) plt.plot(x_axis, y_max) plt.xlabel('Load percentage') plt.ylabel('Collisions number') plt.legend(['Minimum', 'Average', 'Maximum'], loc=2) fig = plt.gcf() fig.canvas.set_window_title('Chained hash table collisions') plt.show() # Open hash table collisions plot y_min = open_collisions[:, 0] y_avg = open_collisions[:, 1] y_max = open_collisions[:, 2] plt.plot(x_axis, y_min) plt.plot(x_axis, y_avg) plt.plot(x_axis, y_max) plt.xlabel('Load percentage') plt.ylabel('Collisions number') plt.legend(['Minimum', 'Average', 'Maximum'], loc=2) fig = plt.gcf() fig.canvas.set_window_title('Open hash table collisions') plt.show() # Open hash table inspection lengths y_min = open_inspections[:, 0] y_avg = open_inspections[:, 1] y_max = open_inspections[:, 2] plt.plot(x_axis, y_min) plt.plot(x_axis, y_avg) plt.plot(x_axis, y_max) plt.xlabel('Load percentage') plt.ylabel('Inspection sequence length') plt.legend(['Minimum', 'Average', 'Maximum'], loc=2) fig = plt.gcf() fig.canvas.set_window_title('Open hash table inspections length') plt.show()
def simGoodness(sim_time, sim_height, sim_length, obs_time, obs_height, obs_length, show_plots=False): """ Calculate the goodness of fit between the modelled meteor and observations. """ # Normalize obseved time to 0 obs_time -= obs_time[0] # Get the maximum and minimum obseved heights ht_max = obs_height[0] ht_min = obs_height[-1] # Get the simulated heights below the maximum observed height sim_obs_max = sim_height[sim_height <= ht_max] # Get the simulated heights above the minimum observed height sim_obs_min = sim_height[sim_height >= ht_min] # If there are no heights below the maximum heights, reject the solution if (not len(sim_obs_max)) or (not len(sim_obs_min)): return np.inf # Get the indices of corresponding max/min heights in simulated data sim_max_i = np.where(sim_obs_max[ 0] == sim_height)[0][0] sim_min_i = np.where(sim_obs_min[-1] == sim_height)[0][0] # Take simulated heights only in the range of observed heights sim_height_obs = sim_height[sim_max_i:sim_min_i] # Take simulated length only in the range of observed lengths sim_length_obs = sim_length[sim_max_i:sim_min_i] # Take simulated time only in the range of observed lengths sim_time_obs = sim_time[sim_max_i:sim_min_i] # Project the first observed height to the simulated lenghts model_ht_interpol_full = scipy.interpolate.CubicSpline(-sim_height_obs, sim_length_obs, extrapolate=True) first_point_length = model_ht_interpol_full(-ht_max) # Project the first observed height to the simulated lengths model_time_interpol_full = scipy.interpolate.CubicSpline(-sim_height_obs, sim_time_obs, extrapolate=True) first_point_time = model_time_interpol_full(-ht_max) # Add the projected point as the first point sim_height_obs = np.r_[ht_max, sim_height_obs] sim_length_obs = np.r_[float(first_point_length), sim_length_obs] sim_time_obs = np.r_[float(first_point_time), sim_time_obs] # Normalize simulated length to 0 sim_length_obs -= sim_length_obs[0] # Normalize simulated time to 0 sim_time_obs -= sim_time_obs[0] # Penalize the occurence when the simulated length does not cover the whole range of observed data # (if it covers the whole range, the penalization should not influence the end result) #penal = (obs_height[0] - obs_height[-1])/(sim_height_obs[1] - sim_height_obs[-1]) penal = 1.0 # print() # print('Obs range:', ht_max, ht_min, 'Sim range:', sim_height_obs[0], sim_height_obs[-1]) # print('Penal:', penal) # NOTE: heights are negative because the CubicSpline function requires that the independant variable # (heights in our case) must be increasing, but that does not influence the end cost function value # Interpolate time vs. length of modelled points model_time_length_interpol = scipy.interpolate.CubicSpline(sim_time_obs, sim_length_obs, extrapolate=True) # Calculate the cost function value between observations and the model cost_value = costFunction(obs_time, obs_length, model_time_length_interpol)*penal if show_plots: print('Cost function:', cost_value) plt.scatter(obs_time, obs_length - model_time_length_interpol(obs_time)) plt.title('Time vs length residuals') plt.show() # Plot modelled points plt.scatter(sim_time_obs, sim_length_obs, label='Modelled', s=5, c='b') x_times = np.linspace(np.min(sim_time_obs), np.max(sim_time_obs), 1000) # Plot cubic fit plt.plot(x_times, model_time_length_interpol(x_times), label='Modelled spline', color='b') # Plot observed points plt.scatter(obs_time, obs_length, label='Observed', s=10, c='r') # Plot observed points projected to the fitted line plt.scatter(obs_time, model_time_length_interpol(obs_time), s=10, c='g', label='Projected') plt.legend() plt.show() # Plot velocity sim_velocity = calcVelocity(sim_time_obs, sim_length_obs)[0] plt.scatter(sim_velocity[1:], sim_time_obs[1:], label='Simulated') obs_velocity = calcVelocity(obs_time, obs_length)[0] plt.scatter(obs_velocity[1:], obs_time[1:], label='Observed') plt.title('Velocities') plt.legend() plt.gca().invert_yaxis() plt.show() ### Plot the simulated vs. observed lag # Take the first part of the simulated meteor len_part = int(0.05*len(sim_time)) sim_time_part = sim_time[:len_part] sim_length_part = sim_length[:len_part] # Fit a line to the first part of the simulated data sim_lag_fit, _ = scipy.optimize.curve_fit(line, sim_time_part, sim_length_part) # Calculate the model lag sim_lag = sim_length - line(sim_time, *sim_lag_fit) # Plot the simulated lag plt.plot(sim_lag, sim_time, label='Simulated') plt.gca().invert_yaxis() # Initerpolate the simulated length with height sim_lag_ht_interpol_full = scipy.interpolate.CubicSpline(-sim_height, sim_lag, extrapolate=True) # Find the length of simulation at the first point of observations sim_lag_at_first_obs = sim_lag_ht_interpol_full(-obs_height[0]) # Interpolate the simulated time vs. height sim_ht_time_interpol_full = scipy.interpolate.CubicSpline(-sim_height, sim_time, extrapolate=True) # Find the time in simulation at the first observed height sim_time_at_first_obs = sim_ht_time_interpol_full(-obs_height[0]) # Plot observed lag obs_lag = obs_length - line(obs_time, *sim_lag_fit) + sim_lag_at_first_obs plt.plot(obs_lag, obs_time + sim_time_at_first_obs, label='Observed') plt.title('Observed vs simulated lag') plt.xlabel('Lag (m)') plt.ylabel('Time (s)') plt.legend() plt.show() ###### # len_residuals = obs_length - model_interpol(-obs_height) # print('Residual range:', np.max(len_residuals) - np.min(len_residuals)) # # Plot residuals between observed and modelled # plt.scatter(len_residuals, obs_height) # plt.show() return cost_value
color=palette(0), alpha=0.2, label="Possible forest carbon values", ) # Make sure the X and y axes are the same for each subplot # (so we can see what's going on easily) ax.set_xlim(left=0, right=10000) ax.set_ylim(bottom=0, top=500) # Add a grid ax.grid(color="k", alpha=0.2, linestyle=":") # add a legend and correct pacing plt.legend(ncol=2, bbox_to_anchor=(-1, -0.7), borderaxespad=0, frameon=False, labelspacing=0.75) plt.subplots_adjust(top=0.937, bottom=0.175, left=0.14, right=0.957, hspace=0.494, wspace=0.125) plt.show() plt.savefig("Fig_05_uncertainty_plumes.png", format="png", dpi=600) # plt.savefig("Fig_05_uncertainty_plumes.svg", format="svg")
lw=3, label='Generated painting', ) plt.plot(PAINT_POINTS[0], 2 * np.power(PAINT_POINTS[0], 2) + 1, c='#74BCFF', lw=3, label='upper bound') plt.plot(PAINT_POINTS[0], 1 * np.power(PAINT_POINTS[0], 2) + 0, c='#FF9359', lw=3, label='lower bound') plt.text(-.5, 2.3, 'D accuracy=%.2f (0.5 for D to converge)' % prob_artist0.data.numpy().mean(), fontdict={'size': 13}) plt.text(-.5, 2, 'D score= %.2f (-1.38 for G to converge)' % -D_loss.data.numpy(), fontdict={'size': 13}) plt.ylim((0, 3)) plt.legend(loc='upper right', fontsize=10) plt.draw() plt.pause(0.01) plt.ioff() plt.show()
data_diff.append(total_diff) data_new.append(total_new) data.append(r) diff_per_day.append(total_diff) total_per_day.append(total) x = total_per_day y = pd.Series(diff_per_day).ewm(span=7).mean() plt.subplot(2,1,1) plt.title("Country={0} T={1}, R={2}, D={3}, Date={4}" .format(sys.argv[1], total_p, recovered, deaths, datetime.datetime.strptime(latest_date, "%Y-%m-%d"))) plt.plot(x,y) plt.legend(["new cases per day average"]) plt.subplot(2,1,2) plt.plot(data) plt.legend(["new", "recover", "deaths"]) plt.show()
best_fit = [] for i in x1_vals: best_fit.append(slope*i+y_intercept) # Separate I. setosa setosa_x = [d[1] for i,d in enumerate(x_vals) if y_vals[i]==1] setosa_y = [d[0] for i,d in enumerate(x_vals) if y_vals[i]==1] not_setosa_x = [d[1] for i,d in enumerate(x_vals) if y_vals[i]==-1] not_setosa_y = [d[0] for i,d in enumerate(x_vals) if y_vals[i]==-1] # Plot data and line plt.plot(setosa_x, setosa_y, 'o', label='I. setosa') plt.plot(not_setosa_x, not_setosa_y, 'x', label='Non-setosa') plt.plot(x1_vals, best_fit, 'r-', label='Linear Separator', linewidth=3) plt.ylim([0, 10]) plt.legend(loc='lower right') plt.title('Sepal Length vs Pedal Width') plt.xlabel('Pedal Width') plt.ylabel('Sepal Length') plt.show() # Plot train/test accuracies plt.plot(train_accuracy, 'k-', label='Training Accuracy') plt.plot(test_accuracy, 'r--', label='Test Accuracy') plt.title('Train and Test Set Accuracies') plt.xlabel('Generation') plt.ylabel('Accuracy') plt.legend(loc='lower right') plt.show() # Plot loss over time
y1 = a[1::3] y2 = a[2::3] x = b[0::3] x1 = b[1::3] x2 = b[2::3] rate = (len(y) - list(y).count(0)) / len(y) rate1 = (len(y1) - list(y1).count(0)) / len(y1) rate2 = (len(y2) - list(y2).count(0)) / len(y2) plt.bar(x, y, color='yellow', label="x rate:{0:.2f}".format(rate), width=1) plt.bar(x1, y1, color="red", label="x1 rate:{0:.2f}".format(rate1), width=1) plt.bar(x2, y2, color="green", label='x2 rate:{0:.2f}'.format(rate2), width=1) plt.legend() # 添 加 图 例 plt.ylim(0, 30) # 设 置 y 轴 的 最 大 高 度 plt.ylabel("random") plt.xlabel("x") plt.title("random.randint") plt.show() # 饼状图 """ def pie( x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, rotatelabels=False, *, data=None):
plt.suptitle('Cross Correlation Times Between Antenna %i and %i'%(i,j)) ax = plt.subplot(2,1,1) n, bins, patches = plt.hist(delays_even[:,i,j],label=('Channel %i and %i'%(2*i,2*j)),bins=bins) best_delay_even = (bins[numpy.argmax(n)+1] + bins[numpy.argmax(n)])/2.0 time_differences_even.append(((i,j),best_delay_even)) plt.xlabel('Delay (ns)',fontsize=16) plt.ylabel('Counts',fontsize=16) plt.axvline(expected_time_difference,c='r',linestyle='--',label='Expected Time Difference = %f'%expected_time_difference) plt.axvline(-expected_time_difference,c='r',linestyle='--') plt.axvline(max_time_difference,c='g',linestyle='--',label='max Time Difference = %f'%max_time_difference) plt.axvline(-max_time_difference,c='g',linestyle='--') plt.axvline(best_delay_even,c='c',linestyle='--',label='Best Time Difference = %f'%best_delay_even) plt.legend(fontsize=16) plt.subplot(2,1,2,sharex=ax) n, bins, patches = plt.hist(delays_odd[:,i,j],label=('Channel %i and %i'%(2*i+1,2*j+1)),bins=bins) best_delay_odd = (bins[numpy.argmax(n)+1] + bins[numpy.argmax(n)])/2.0 time_differences_odd.append(((i,j),best_delay_odd)) plt.xlabel('Delay (ns)',fontsize=16) plt.ylabel('Counts',fontsize=16) plt.axvline(expected_time_difference,c='r',linestyle='--',label='Expected Time Difference = %f'%expected_time_difference) plt.axvline(-expected_time_difference,c='r',linestyle='--') plt.axvline(max_time_difference,c='g',linestyle='--',label='max Time Difference = %f'%max_time_difference) plt.axvline(-max_time_difference,c='g',linestyle='--') plt.axvline(best_delay_odd,c='c',linestyle='--',label='Best Time Difference = %f'%best_delay_odd) plt.legend(fontsize=16)
def plot(args): """ Plot averages per model for specified environment. Must have one .csv file per model for specified environment. Generates .csv average every time for plotting. File format is 'env_{}_model_{}_avg.csv'.format(env, abbrv). """ env = args.env[0] max_timestep = args.max_timestep timescale = args.timescale avg_window = args.avg_window overwrite = args.overwrite # obtain .csv files corresponding to env f_ = list(os.listdir(IN_DIR)) f = [i for i in f_ if '_{}_'.format(env) in i] # initialize plot print('Generating plot for environment {}...'.format(env)) plt.rcParams.update({'font.size': 13}) plt.figure() plt.xlim(0, max_timestep) plt.xticks([x for x in range(0, max_timestep+1, timescale)], [str(x/1000000) + 'M' for x in range(0, max_timestep+1, timescale)]) plt.xlabel('Timesteps') plt.ylabel('Episodic Reward') plt.title('{}'.format(env)) # iterate through each model for plotting for m, a, l in zip(MODELS, ABBRVS, LABELS): # obtain .csv files corresponding to model files = [i for i in f if a in i] if len(files) == 0: raise Exception("No .csv file to plot for environment {}, model {}".format(env, m)) # calculate average of all (env, model) files if necessary avg_file = os.path.join(IN_DIR, 'env_{}_model_{}_avg.csv'.format(env, a)) if not os.path.exists(avg_file) or overwrite: # filter out existing avg file files_ = [i for i in files if 'avg' not in i] average(env, a, files_) with open(avg_file, 'r') as c: csv_reader = csv.reader(c, delimiter=',') x, y = [], [] for row in csv_reader: x.append(float(row[0])) y.append(float(row[1])) x = np.array(x) y = np.array(y) print('Lines read: {}'.format(x.shape[0])) if avg_window > 1: y_moveavg = moving_average(y, avg_window) y_moveavg = np.append(y_moveavg, [y_moveavg[-1]] * (avg_window - 1)) plt.plot(x, y_moveavg, label=l) else: plt.plot(x, y, label=l) # save compiled plot print('Saving...') plt.legend() plt.savefig((os.path.join(OUT_DIR, '{}.jpg'.format(env)))) plt.close()
import data_input data = mem.cache(data_input.get_data)() # Below, we restrict ourselves to working only on the active cases active = data['active'] # %% # Plot the time course of the most affected countries last_day = active.iloc[-1] most_affected_countries = active.columns[last_day.argsort()][::-1] import matplotlib.pyplot as plt ax = active[most_affected_countries[:20]].plot(figsize=(7, 7)) ax.set_yscale('log') ax.set_title("Log-scale plot of number of active cases") plt.legend(loc='best', ncol=3) plt.tight_layout() # %% # Some functions to build normalized smoothing kernels import numpy as np def ramp_kernel(start=14, middle=7): kernel = np.ones(start) kernel[:middle] = np.arange(middle) / float(middle) kernel /= kernel.sum() return kernel def exp_kernel(start=14, growth=1.1):
# split data into 30% training and 70% testing X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0) # train the LogisticRegression knn = KNeighborsClassifier(n_neighbors=5, p=2, metric='minkowski') knn.fit(X_train, y_train) # make predictions y_pred = knn.predict(X_test) print('Misclassified samples: %d' % (y_test != y_pred).sum()) print('Accuracy: %.2f' % accuracy_score(y_test, y_pred)) # graph decision regions X_combined = np.vstack((X_train, X_test)) y_combined = np.hstack((y_train, y_test)) plot_decision_regions(X_combined, y_combined, classifier=knn, test_idx=range(105, 150)) plt.xlabel('petal length [standardized]') plt.ylabel('petal width [standardized]') plt.legend(loc='upper left') plt.tight_layout() # plt.savefig('./figures/k_nearest_neighbors.png', dpi=300) plt.show()
def draw_Battery_Use(consumption, total_power, solar_power, wind_power, dic, configuration, max_power): # Creating mutiple text variables to display in the graph power_generated = total_power.sum() power = total_power total_power = np.mean(np.reshape(total_power[:8760], (365, 24)), axis=1) t1 = "Storage capacity: \nAmount of windturbines: \nCable area: \nMaximum Power Output: \nTotal Power Generated: \nTotal costs: " t2 = str(int(dic['total_storage'])) + " kWh\n" + \ str(int(configuration[-2])) + "\n" + \ str(int(dic['cable_area'])) + " mm²\n" + \ str(int(max_power)) + " kW\n" + \ str(int(power_generated)) + " kWh\n" +\ '€' + str(int(dic['cost'])) # Creating the solar stats text variables to display in the graph t3 = "" for I in range(4): if configuration[0 + I * 3] > 0: t3 = t3 + "SP" + str(I + 1) + " - Area: " + str(int(configuration[0 + I*3])) +\ "m² - Angle: " + str(int(configuration[1 + I*3])) +\ "° - Orientation: " + str(int(configuration[2 + I*3])) + "°\n" plt.subplot(2, 1, 1) plt.plot(total_power, color='green', alpha=0.5, label='Total energy production') plt.plot(solar_power, color='yellow', alpha=0.5, label='Solar energy') plt.plot(wind_power, color='blue', alpha=0.5, label='Wind energy') plt.plot(consumption, color='red', label='Energy demand') plt.text(330, total_power.max() * 1.04, t2, ha='left', va='top', style='italic', wrap=False) plt.text(330, total_power.max() * 1.04, t1, ha='right', va='top', wrap=False) plt.text(362, total_power.max() * 0.725, t3, ha='right', va='top', wrap=False) plt.legend(loc='upper center') plt.title("Power Average per Day") plt.xlabel('Days') plt.ylabel('kW') plt.xlim(0, 365) plt.subplot(2, 1, 2) power = power - 6000 for x in range(2): if x == 0: batterycharge = [int(dic['total_storage'])] else: batterycharge = [batterycharge[-1]] Powershortage = [] for I in power: batterycharge.append(batterycharge[-1] + I) if (int(dic['total_storage']) < batterycharge[-1]): batterycharge[-1] = int(dic['total_storage']) elif (0 > batterycharge[-1]): batterycharge[-1] = 0 Powershortage.append(len(batterycharge) - 1) plt.plot(batterycharge, color='green', alpha=0.5) if len(Powershortage) == 0: plt.scatter(np.zeros(len(Powershortage)), Powershortage, color='red') plt.title("Power supply level over a Year") plt.xlabel('Hour') plt.ylabel('kWh') plt.xlim(0, 8760) plt.show()
def main(): """ The main() function. """ print("STARTING MINITAUR ARS") # TRAINING PARAMETERS # env_name = "MinitaurBulletEnv-v0" seed = 0 max_timesteps = 1e6 file_name = "mini_tg_ars_" # Find abs path to this file my_path = os.path.abspath(os.path.dirname(__file__)) results_path = os.path.join(my_path, "../results") models_path = os.path.join(my_path, "../models") if not os.path.exists(results_path): os.makedirs(results_path) if not os.path.exists(models_path): os.makedirs(models_path) env = MinitaurBulletEnv(render=True, on_rack=False) dt = env._time_step # TRAJECTORY GENERATOR movetype = "walk" # movetype = "trot" # movetype = "bound" # movetype = "pace" # movetype = "pronk" TG = TGPolicy(movetype=movetype, center_swing=0.0, amplitude_extension=0.2, amplitude_lift=0.4) TG_state_dim = len(TG.get_TG_state()) TG_action_dim = 5 # f_tg, Beta, alpha_tg, h_tg, intensity state_dim = env.observation_space.shape[0] + TG_state_dim print("STATE DIM: {}".format(state_dim)) action_dim = env.action_space.shape[0] + TG_action_dim print("ACTION DIM: {}".format(action_dim)) max_action = float(env.action_space.high[0]) print("RECORDED MAX ACTION: {}".format(max_action)) # Initialize Normalizer normalizer = Normalizer(state_dim) # Initialize Policy policy = Policy(state_dim, action_dim) # Initialize Agent with normalizer, policy and gym env agent = ARSAgent(normalizer, policy, env, TGP=TG) agent_num = raw_input("Policy Number: ") if os.path.exists(models_path + "/" + file_name + str(agent_num) + "_policy"): print("Loading Existing agent") agent.load(models_path + "/" + file_name + str(agent_num)) agent.policy.episode_steps = 1000 policy = agent.policy # Set seeds env.seed(seed) torch.manual_seed(seed) np.random.seed(seed) env.reset() episode_reward = 0 episode_timesteps = 0 episode_num = 0 print("STARTED MINITAUR TEST SCRIPT") # Just to store correct action space action = env.action_space.sample() # Record extends for plot # LF_ext = [] # LB_ext = [] # RF_ext = [] # RB_ext = [] LF_tp = [] LB_tp = [] RF_tp = [] RB_tp = [] t = 0 while t < (int(max_timesteps)): action[:] = 0.0 # # Get Action from TG [no policies here] # action = TG.get_utg(action, alpha_tg, h_tg, intensity, # env.minitaur.num_motors) # LF_ext.append(action[env.minitaur.num_motors / 2]) # LB_ext.append(action[1 + env.minitaur.num_motors / 2]) # RF_ext.append(action[2 + env.minitaur.num_motors / 2]) # RB_ext.append(action[3 + env.minitaur.num_motors / 2]) # # Perform action # next_state, reward, done, _ = env.step(action) obs = agent.TGP.get_TG_state() # LF_tp.append(obs[0]) # LB_tp.append(obs[1]) # RF_tp.append(obs[2]) # RB_tp.append(obs[3]) # # Increment phase # TG.increment(dt, f_tg, Beta) # # time.sleep(1.0) # t += 1 # Maximum timesteps per rollout t += policy.episode_steps episode_timesteps += 1 episode_reward = agent.deployTG() # episode_reward = agent.train() # +1 to account for 0 indexing. # +0 on ep_timesteps since it will increment +1 even if done=True print("Total T: {} Episode Num: {} Episode T: {} Reward: {}".format( t, episode_num, policy.episode_steps, episode_reward)) # Reset environment episode_reward = 0 episode_timesteps = 0 episode_num += 1 plt.plot(0) plt.plot(LF_tp, label="LF") plt.plot(LB_tp, label="LB") plt.plot(RF_tp, label="RF") plt.plot(RB_tp, label="RB") plt.xlabel("t") plt.ylabel("EXT") plt.title("Leg Extensions") plt.legend() plt.show() env.close()
import matplotlib.pyplot as plt from math import log,sqrt g=open('genome.fa') r='ac' genome, x = [],[] maxString = int(sys.argv[1]) for i in range(1,maxString): r+=g.read(1) genome+=[compress(r)] x+=[len(r)] g.close() plt.plot(x, [float(x[i])/genome[i] for i in range(len(x))], label = 'genome') #plt.plot(x,[sqrt(n/2) for n in x],label='uniform') plt.xlabel('String Length') plt.ylabel('Compression Ratio') plt.legend(bbox_to_anchor=(0.3,0.9)) plt.show()
16 * f(x[i] + h) - f(x[i] + 2 * h)) / (12 * h**2) for i in range(len(x) - 1): dydx3[i] = (f(x[i] - 3 * h) - 8 * f(x[i] - 2 * h) + 13 * f(x[i] - h) - 13 * f(x[i] + h) + 8 * f(x[i] + 2 * h) - f(x[i] + 3 * h)) / (8 * h**3) dYdx = -np.sin(x) dYdx2 = -np.cos(x) dYdx3 = np.sin(x) plt.figure(0) plt.plot(x, dydx, 'r.', label='Primera derivada') plt.plot(x, dYdx, 'b', label='Valor verdadero') plt.title('Cálculo numérico de la primera derivada de y = cos(x)') plt.legend(loc='best') plt.figure(1) plt.plot(x, dydx2, 'r.', label='2da drivada') plt.plot(x, dYdx2, 'b', label='Valor verdadero') plt.title('Cálculo numérico de la 2da derivada') plt.legend(loc='best') plt.figure(2) plt.plot(x, dydx3, 'r.', label='3ra derivada') plt.plot(x, dYdx3, 'b', label='Valor verdadero') plt.title('Cálculo numérico de la 3ra derivada') plt.legend(loc='best')
if comm.args.v: print('Diffusion coefficients in %d directions!' %(dim//2)) print('Fitting from %f to %f' %(scale[0], scale[1])) pt_start = int(scale[0]*len(msd)) pt_end = int(scale[1]*len(msd)) #label_name = ['OMIm', 'TFSI', 'Solvent'] label_name = ['TEA', 'BF4', 'ACN'] colors = ['red', 'blue', 'green'] for i in range(1, len(msd[0])): coef = np.polyfit(msd[pt_start:pt_end, 0], msd[pt_start:pt_end, i], 1) if comm.args.v: print('Diffusion coefficient for group %d is %.4E m^2/s' %(i, coef[0]/dim/1e6)) else: print(coef[0]/dim/1e6, end = ' ') fit = np.poly1d(coef) plt.plot(msd[:, 0]/1000, msd[:, i], linewidth = lwidth, label = label_name[i-1], color = colors[i-1]) plt.plot(msd[:, 0]/1000, fit(msd[:, 0]), '--', linewidth = lwidth, label = 'fitting '+label_name[i-1]) print('') plt.legend(loc = 'best', fontsize = fsize, frameon = True, numpoints = 1) plt.xlabel('time (ns)', fontsize = fsize) plt.ylabel('MSD ($\mathregular{nm^2\!}$)', fontsize = fsize) for axes in fig.axes: axes.tick_params(labelsize = fsize, width = 2, length = 6, pad = 10) for direction in ['top', 'bottom', 'left', 'right']: axes.spines[direction].set_linewidth(2) plt.tight_layout() plt.savefig('Diff.pdf')
def q4(): # Question 4.4 K = 5 iters = 10 minVary = 0.01 randConst = 1.0 numComponents = np.array([7, 14, 21, 28, 35]) T = numComponents.shape[0] errorTrain = np.zeros(T) errorTest = np.zeros(T) errorValidation = np.zeros(T) # extract data of class 1-Anger, 4-Happy dataQ4 = LoadDataQ4('../toronto_face.npz') # images x_train_anger = dataQ4['x_train_anger'] x_train_happy = dataQ4['x_train_happy'] x_train = np.concatenate([x_train_anger, x_train_happy], axis=1) x_valid = np.concatenate( [dataQ4['x_valid_anger'], dataQ4['x_valid_happy']], axis=1) x_test = np.concatenate( [dataQ4['x_test_anger'], dataQ4['x_test_happy']], axis=1) # label y_train = np.concatenate( [dataQ4['y_train_anger'], dataQ4['y_train_happy']]) y_valid = np.concatenate( [dataQ4['y_valid_anger'], dataQ4['y_valid_happy']]) y_test = np.concatenate([dataQ4['y_test_anger'], dataQ4['y_test_happy']]) # Hints: this is p(d), use it based on Bayes Theorem num_anger_train = x_train_anger.shape[1] num_happy_train = x_train_happy.shape[1] log_likelihood_class = np.log([num_anger_train, num_happy_train]) - np.log(num_anger_train + num_happy_train) for t in xrange(T): K = numComponents[t] # Train a MoG model with K components # Hints: using (x_train_anger, x_train_happy) train 2 MoGs #-------------------- Ad your code here ------------------------------ #------------------- Answers --------------------- # Compute the probability P(d|x), classify examples, and compute error rate # Hints: using (x_train, y_train), (x_valid, y_valid), (x_test, y_test) # to compute error rates, you may want to use mogLogLikelihood function #-------------------- Add your code here ------------------------------ anger = {'prob': 0, 'mu': 0, 'var': 0, 'LL': 0} happy = {'prob': 0, 'mu': 0, 'var': 0, 'LL': 0} anger['prob'], anger['mu'], anger['var'], anger['LL'] = mogEM(x_train_anger, K, iters, randConst, minVary) happy['prob'], happy['mu'], happy['var'], happy['LL'] = mogEM(x_train_happy, K, iters, randConst, minVary) errorTrain[t] = calcAngerHappy(x_train, y_train, anger, happy, mogLogLikelihood, log_likelihood_class) errorValidation[t] = calcAngerHappy(x_valid, y_valid, anger, happy, mogLogLikelihood, log_likelihood_class) errorTest[t] = calcAngerHappy(x_test, y_test, anger, happy, mogLogLikelihood, log_likelihood_class) print("Train: ", errorTrain[t], t) print("Valid: ", errorValidation[t]) print("Test: ", errorTest[t], t) #------------------- Answers --------------------- # Plot the error rate plt.figure(0) plt.clf() #-------------------- Add your code here -------------------------------- #------------------- Answers --------------------- # to be removed before release plt.plot(numComponents, errorTrain, 'r', label='Training') plt.plot(numComponents, errorValidation, 'g', label='Validation') plt.plot(numComponents, errorTest, 'b', label='Testing') plt.xlabel('Number of Mixture Components') plt.ylabel('Error Rate') plt.legend() plt.draw() plt.pause(0.0001)
def plot_life_cycle(self): """ Some plots. Savongs and consumption distribution, policy function for the period 44 (just before retirment) """ s_mean = np.zeros(self.T+1) s_max = np.zeros(self.T+1) s_min = np.zeros(self.T+1) c_mean = np.zeros(self.T+1) c_max = np.zeros(self.T+1) c_min = np.zeros(self.T+1) c_opt_mean = np.zeros(self.T+1) c_opt_max = np.zeros(self.T+1) c_opt_min = np.zeros(self.T+1) c_diff_mean = np.zeros(self.T+1) c_diff_max = np.zeros(self.T+1) c_diff_min = np.zeros(self.T+1) z_mean = np.zeros(self.T+1) z_max = np.zeros(self.T+1) z_min = np.zeros(self.T+1) bc_mean = np.zeros(self.T+1) bc_max = np.zeros(self.T+1) bc_min = np.zeros(self.T+1) for j in range(1,self.T+1,1): s_mean[j] = np.mean(self.sav_distr[:,:,j]) s_max[j] = np.percentile(self.sav_distr[:,:,j],90) s_min[j] = np.percentile(self.sav_distr[:,:,j],10) plt.plot(range(self.T+1), s_mean, label = "mean savings") plt.plot(range(self.T+1), s_max, label = "90th percentile of savings") plt.plot(range(self.T+1), s_min, label = "90th percentile of savings") plt.ylabel('savings profile') plt.legend(loc='best') plt.show() for j in range(1,self.T+1,1): z_mean[j] = np.mean(self.zsim1[:,j]) z_max[j] = np.max(self.zsim1[:,j]) z_min[j] = np.min(self.zsim1[:,j]) plt.plot(range(self.T+1), z_mean, label = "mean shocks") plt.plot(range(self.T+1), z_max, label = "max shocks") plt.plot(range(self.T+1), z_min, label = "min shocks") plt.ylabel('shocks') plt.legend(loc='best') plt.show() for j in range(1,self.T+1,1): c_mean[j] = np.mean(self.cons_distr[:,:,j]) c_max[j] = np.percentile(self.cons_distr[:,:,j],90) c_min[j] = np.percentile(self.cons_distr[:,:,j],10) plt.plot(range(self.T+1), c_mean, label = "mean consumption") plt.plot(range(self.T+1), c_max, label = "90th percentile of consumption") plt.plot(range(self.T+1), c_min, label = "10th percentile consumption") plt.ylabel('savings') plt.legend(loc='best') plt.show() for j in range(1,self.T+1,1): c_opt_mean[j] = np.mean(self.cons_distr_opt[:,:,j]) c_opt_max[j] = np.percentile(self.cons_distr_opt[:,:,j],90) c_opt_min[j] = np.percentile(self.cons_distr_opt[:,:,j],10) plt.plot(range(self.T+1), c_opt_mean, label = "mean optimal consumption") plt.plot(range(self.T+1), c_opt_max, label = "90th percentile of optimal consumption") plt.plot(range(self.T+1), c_opt_min, label = "10th percentile of optimal consumption") plt.ylabel('savings') plt.legend(loc='best') plt.show() c_diff = self.cons_distr_opt - self.cons_distr for j in range(1,self.T+1,1): c_diff_mean[j] = np.mean(c_diff[:,:,j]) c_diff_max[j] = np.percentile(c_diff[:,:,j],99) c_diff_min[j] = np.percentile(c_diff[:,:,j],1) plt.plot(range(self.T+1), c_diff_mean, label = "mean diff consumption") plt.plot(range(self.T+1), c_diff_max, label = "90th percentile of diff consumption") plt.plot(range(self.T+1), c_diff_min, label = "10th percentile of diff consumption") plt.ylabel('savings') plt.legend(loc='best') plt.show() for j in range(1,self.T+1,1): bc_mean[j] = np.mean(self.bc[:,:,j]) bc_max[j] = np.percentile(self.bc[:,:,j],99) bc_min[j] = np.percentile(self.bc[:,:,j],1) plt.plot(range(self.T+1), bc_mean, label = "bc consumption") plt.plot(range(self.T+1), bc_max, label = "bc max") plt.plot(range(self.T+1), bc_min, label = "bc min") plt.ylabel('bc') plt.legend(loc='best') plt.show() plt.plot(self.grid[0:40], self.pf_a_edu[40,0:40,0,1,2], label = "savings for worst group") plt.plot(self.grid[0:40], self.pf_a_edu[40,0:40,1,1,2], label = "savings for second worst group") plt.plot(self.grid[0:40], self.pf_a_edu[40,0:40,2,1,2], label = "savings for mednian group") plt.plot(self.grid[0:40], self.pf_a_edu[40,0:40,3,1,2], label = "savings for second best group") plt.plot(self.grid[0:40], self.pf_a_edu[40,0:40,4,1,2], label = "savings for best group") # plt.plot(self.grid[0:80], self.pf_a_edu[44,0:80,0,1,2], label = "savings for worst2 group") # plt.plot(self.grid[0:80], self.pf_a_edu[44,0:80,1,1,2], label = "savings for second2 worst group") # plt.plot(self.grid[0:80], self.pf_a_edu[44,0:80,2,1,2], label = "savings for mednian2 group") # plt.plot(self.grid[0:80], self.pf_a_edu[44,0:80,3,1,2], label = "savings for second best2 group") # plt.plot(self.grid[0:80], self.pf_a_edu[44,0:80,4,1,2], label = "savings for best 2group") plt.ylabel('saving policy function for eductated') plt.legend(loc='best') plt.show()
else: clf = LinearRegression(n_jobs=-1) clf.fit(X_train, y_train) with open("LR.pickle", 'wb') as f: pickle.dump(clf, f) acc = clf.score(X_test, y_test) forecast_set = clf.predict(X_lately) print(forecast_set, acc, forecast_out) df['forecast'] = np.nan last_date = df.iloc[-1].name last_unix = last_date.timestamp() one_day = 86400 next_unix = last_unix + one_day for i in forecast_set: next_date = datetime.datetime.fromtimestamp(next_unix) next_unix += one_day df.loc[next_date] = [np.nan for _ in range(len(df.columns) - 1)] + [i] df['Adj. Close'].plot() df['forecast'].plot() plt.legend(loc=4) plt.xlabel("Date") plt.ylabel("Price") plt.show()
def start(): portName = request.form['portName'] tempo_experiencia = request.form['tempo_experiencia'] potencia = int(request.form['potencia']) plotInterval = int(request.form['plotInterval']) option = int(request.form['expressar tempo']) filename = request.form['filename'] numPlots = 1 tempo_experiencia = int(tempo_experiencia) #segundos nframes = int((tempo_experiencia*1000)//plotInterval) maxPlotLength = nframes + 1 # Maximo valor do eixo x do grafico (tempo) em segundos xmin = 0 # Valor minimo do X do grafico xmax = maxPlotLength # Valor maximo do X do grafico ymin = 0 # Valor minimo do y do grafico ymax = 80 # Valor maximo do y do grafico fig, ax = plt.subplots(facecolor=(.18, .31, .31),figsize=(17,5)) ax = plt.axes() ax.set_ylim(ymin, ymax) ax.set_xlim(xmin, xmax) #ReScale x label x = np.linspace(xmin,xmax) scale_x = 1000/(plotInterval) ticks_x = ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x/scale_x)) ax.xaxis.set_major_formatter(ticks_x) ax.set_xticks(np.arange(xmin,xmax)) ax.set_title("Projeto IC", color='peachpuff') # Titulo do grafico ax.set_xlabel("Tempo", color='peachpuff') # Titulo do eixo x ax.set_ylabel("Rth: (T1-T2)/P", color='peachpuff') # Titulo do eixo y ax.set_facecolor('#eafff5') ax.grid(True) lineLabel = ["Rth"] style = ["r-"] # linestyles for the different plots timeText = ax.text(0.30, 0.95, "", transform=ax.transAxes) lines = [] lineValueText = [] for i in range(numPlots): lines.append(ax.plot([], [], style[i], label=lineLabel[i])[0]) lineValueText.append(ax.text(0.70, 0.90 - i * 0.05, "", transform=ax.transAxes)) # -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- s = serial_acquire.serialPlot( portName, baudRate, maxPlotLength, dataNumBytes, numPlots, tempo_experiencia, potencia, ax, option, filename,) s.readSerialStart() # Comeca o backgroundThread # Comeca a plotar no grafico # --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- anim = animation.FuncAnimation(fig,s.getSerialData, fargs=(lines, lineValueText, lineLabel, timeText),frames = nframes, interval = plotInterval, repeat = False) # Envia as variaveis para plotar o grafico plt.legend(loc="upper left") # Local da legenda no graafico plt.show() # ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- s.close() # Para de plotar o grafico return render_template('backpage.html')
def pointing_err(dnight, sets): ''' This module is calculating the pointing error of each image. ''' star = Dispatch('NOVAS.Star') site = Dispatch('NOVAS.Site') util = Dispatch('ACP.Util') p = Dispatch('PinPoint.Plate') #looping through all the sets in that night for s in sets: calsetp = filepath.calibdata + dnight + '/S_0' + s[0] + '/' #read in the header to set the site object's parameter H = fits.open(calsetp + 'ib001.fit', unit=False)[0].header site.longitude = H['LONGITUD'] site.latitude = H['LATITUDE'] site.height = 0 #calculate the temperture-pressure correction for refraction temp = (H['AMTEMP_F'] - 32) / 1.8 + 273 #temperature [K] pres = (1 - (0.0065 * H['ELEVATIO'] / 288.15))**5.3 #pressure [atm] tpco = pres * (283 / temp) #correction #refraction at 7.5 altitude refraction = tpco * (1 / n.tan(n.deg2rad(7.5 + 7.31 / 11.9))) / 60 #just for V band solved = [] notsolved = [] True_AZ = [] True_ALT = [] Input_AZ = [] Input_ALT = [] for fn in iglob(calsetp + 'ib???.fit'): fns = fn[:-4] + 's' + fn[-4:] if os.path.exists(fns): H = fits.open(fns)[0].header fnsolved = fns else: H = fits.open(fn)[0].header fnsolved = fn #calculating the pointing error only if the plate is solved if 'PLTSOLVD' not in H or H['PLTSOLVD'] == False: notsolved.append(fn) continue solved.append(int(fn[-7:-4])) p.attachFits(fnsolved) star.RightAscension = p.RightAscension star.Declination = p.Declination JD = H['JD'] #Julian Date TJD = util.Julian_TJD(JD) #Terrestrial Julian Date #updated star's coordinates at the observed date/time and site StarTopo = star.GetTopocentricPosition(TJD, site, False) #local apparent sidereal time [hr] LAST = get_last(JD, H['LONGITUD']) #new CoordinateTransform object ct = util.Newct(H['LATITUDE'], LAST) ct.RightAscension = StarTopo.RightAscension ct.Declination = StarTopo.Declination Input_AZ.append(H['AZ']) Input_ALT.append(H['ALT']) True_AZ.append(ct.Azimuth) #correct for atmospheric refraction on images 1-15 if int(fn[-7:-4]) < 16: True_ALT.append(ct.Elevation + refraction) else: True_ALT.append(ct.Elevation) p.DetachFITS() #interpolate the True_AZ for True_ALT for images that are not solved pterr = n.array([solved, Input_AZ, Input_ALT, True_AZ, True_ALT]) pterr = interp_coord(n.array(notsolved), pterr) # calculate errors pErr = pterr.T pErr[3][n.where((pErr[1] == 0) & (pErr[3] > 180))] -= 360 azmErr = (pErr[1] - pErr[3]) * n.cos(n.deg2rad(pErr[4])) altErr = pErr[2] - pErr[4] totErr = n.sqrt(n.power(azmErr, 2) + n.power(altErr, 2)) #create a pointing error plot errorPlot = plt.figure(figsize=(20, 10)) ax = errorPlot.add_subplot(111) plt.suptitle("Pointing Error by Image Number", fontsize=25, verticalalignment='top') plt.title("Data Set " + s[0], fontsize=20) plt.plot(pErr[0], azmErr, linestyle="-.", marker="o", markerfacecolor='None', markersize=4, color="darkorange", alpha=0.7, label="Azimuth Error") plt.plot(pErr[0], altErr, linestyle="--", marker="o", markersize=4, color="darkgreen", alpha=0.7, label="Altitude Error") plt.plot(pErr[0], totErr, linestyle="-", linewidth=2, marker="o", markersize=6, color="black", alpha=1, label="Total Error") plt.axhline(0, color="black", linestyle="-", alpha=0.5, zorder=-10) plt.ylim(-3, 3) plt.ylabel("Error in Degrees", fontsize=20, labelpad=10) plt.xlabel("Image Number", fontsize=20, labelpad=15) plt.xticks(n.arange(0, 50, 5)) plt.legend(loc='upper left', markerscale=1.8, fontsize=18, framealpha=0.3) ax.tick_params(axis='both', which='major', labelsize=15) plt.text(0.5, -2.8, "Average Total Error: " + '{:.3f}'.format(totErr.mean()) + u'\N{DEGREE SIGN}', fontsize=18) errorPlot.savefig(filepath.calibdata + dnight + '/pointerr_%s.png' % s[0]) #saving the output file outfile = filepath.calibdata + dnight + '/pointerr_%s.txt' % s[0] nformat = ['%4.f', '%8.f', '%8.1f', '%8.2f', '%8.2f'] H = 'file Input_AZ Input_ALT Obs_AZ Obs_ALT' #column names n.savetxt(outfile, pterr, fmt=nformat, header=H)
pca = PCA(n_components=2) X_r = pca.fit(X).transform(X) lda = LinearDiscriminantAnalysis(n_components=2) X_r2 = lda.fit(X, y).transform(X) # Percentage of variance explained for each components print('explained variance ratio (first two components): %s' % str(pca.explained_variance_ratio_)) plt.figure() colors = ['navy', 'turquoise', 'darkorange'] lw = 2 for color, i, target_name in zip(colors, [0, 1, 2], target_names): plt.scatter(X_r[y == i, 0], X_r[y == i, 1], color=color, alpha=.8, lw=lw, label=target_name) plt.legend(loc='best', shadow=False, scatterpoints=1) plt.title('PCA of IRIS dataset') plt.figure() for color, i, target_name in zip(colors, [0, 1, 2], target_names): plt.scatter(X_r2[y == i, 0], X_r2[y == i, 1], alpha=.8, color=color, label=target_name) plt.legend(loc='best', shadow=False, scatterpoints=1) plt.title('LDA of IRIS dataset') # plt.show() import numpy as np import matplotlib.pyplot as plt from sklearn.decomposition import PCA, KernelPCA from sklearn.datasets import make_circles
plt.ylabel("training time") df.plot(kind='line', x='success_fo', y='ticks_fo', color='red', ax=ax) df.plot(kind='line', x='success_so', y='ticks_so', color='blue', ax=ax) df.plot(kind='line', x='success_offline', y='ticks_offline', color='green', ax=ax) plt.savefig('success_trainingtime.png') plt.close() # generate avg % tile-change number (2) # generate matplotlib per agent[zelda-wide-v0, zelda-narrow-v0...], x=change_percentage,y=avg%tile_change (3) import numpy as np agents = ["zelda-wide-v0", "zelda-narrow-v0", "zelda-turtle-v0"] ys = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] for agent in agents: base = tracking[agent] result = [elem['tile_change_actions'] for elem in base] print('average % tile-change number for ' + agent + ': ', np.mean(result)) # (1) plt.plot(ys, result, label=agent) plt.legend(loc="upper left") plt.title("change % vs # tile change actions") plt.xlabel("change_percentage") plt.ylabel("tile change actions") plt.savefig('tileactions_vs_changepercentage.png') # (3) plt.close()
#calling the function model_prediction for y_true and y_pred y_true, y_pred = model_prediction() #plot and save "Confusion matrix" plt.figure(1) print('\nConfusion Matrix') confmat = (confusion_matrix(y_true, y_pred)) fig, ax = plt.subplots(figsize=(5.5, 5.5)) ax.matshow(confmat, cmap=plt.cm.Blues, alpha=0.3) for i in range(confmat.shape[0]): for j in range(confmat.shape[1]): ax.text(x=j, y=i, s=confmat[i, j], va='center', ha='center') plt.xlabel('predicted label') plt.ylabel('true label') plt.show() #plot and save "Accuracy_loss curve" plt.figure(2) plt.plot(history.history['acc']) plt.plot(history.history['val_acc']) plt.plot(history.history['loss']) plt.plot(history.history['val_loss']) plt.title('model Accuracy & Loss') plt.ylabel('accuracy') plt.ylabel('accuracy_loss') plt.xlabel('epoch') plt.legend(['train_acc', 'validation_acc', 'train_loss', 'validation_loss'], loc='best') plt.show() plt.savefig('accuracy_loss.png')
def _plot_ica_sources_evoked(evoked, picks, exclude, title, show, labels=None): """Plot average over epochs in ICA space. Parameters ---------- evoked : instance of mne.Evoked The Evoked to be used. picks : int | array_like of int | None. The components to be displayed. If None, plot will show the sources in the order as fitted. exclude : array_like of int The components marked for exclusion. If None (default), ICA.exclude will be used. title : str The figure title. show : bool Show figure if True. labels : None | dict The ICA labels attribute. """ import matplotlib.pyplot as plt if title is None: title = 'Reconstructed latent sources, time-locked' fig, axes = plt.subplots(1) ax = axes axes = [axes] times = evoked.times * 1e3 # plot unclassified sources and label excluded ones lines = list() texts = list() if picks is None: picks = np.arange(evoked.data.shape[0]) picks = np.sort(picks) idxs = [picks] if labels is not None: labels_used = [k for k in labels if '/' not in k] exclude_labels = list() for ii in picks: if ii in exclude: line_label = 'IC #%03d' % ii if labels is not None: annot = list() for this_label in labels_used: indices = labels[this_label] if ii in indices: annot.append(this_label) line_label += (' - ' + ', '.join(annot)) exclude_labels.append(line_label) else: exclude_labels.append(None) if labels is not None: # compute colors only based on label categories unique_labels = set([k.split(' - ')[1] for k in exclude_labels if k]) label_colors = plt.cm.rainbow(np.linspace(0, 1, len(unique_labels))) label_colors = dict(zip(unique_labels, label_colors)) else: label_colors = dict((k, 'red') for k in exclude_labels) for exc_label, ii in zip(exclude_labels, picks): if exc_label is not None: # create look up for color ... if ' - ' in exc_label: key = exc_label.split(' - ')[1] else: key = exc_label color = label_colors[key] # ... but display component number too lines.extend(ax.plot(times, evoked.data[ii].T, picker=3., zorder=2, color=color, label=exc_label)) else: lines.extend(ax.plot(times, evoked.data[ii].T, picker=3., color='k', zorder=1)) ax.set_title(title) ax.set_xlim(times[[0, -1]]) ax.set_xlabel('Time (ms)') ax.set_ylabel('(NA)') if len(exclude) > 0: plt.legend(loc='best') tight_layout(fig=fig) # for old matplotlib, we actually need this to have a bounding # box (!), so we have to put some valid text here, change # alpha and path effects later texts.append(ax.text(0, 0, 'blank', zorder=3, verticalalignment='baseline', horizontalalignment='left', fontweight='bold', alpha=0)) # this is done to give the structure of a list of lists of a group of lines # in each subplot lines = [lines] ch_names = evoked.ch_names from matplotlib import patheffects path_effects = [patheffects.withStroke(linewidth=2, foreground="w", alpha=0.75)] params = dict(axes=axes, texts=texts, lines=lines, idxs=idxs, ch_names=ch_names, need_draw=False, path_effects=path_effects) fig.canvas.mpl_connect('pick_event', partial(_butterfly_onpick, params=params)) fig.canvas.mpl_connect('button_press_event', partial(_butterfly_on_button_press, params=params)) plt_show(show) return fig
trainMin = 0.5 trainMax = 10 nTrain = 50 scale = trainMax - trainMin trainSet = trainMin + scale * tf.constant(np.random.rand(nTrain, 1)) model.solve(trainSet) # Testing Set nTest = 50 scale = trainMax - trainMin testSet = trainMin + scale * np.random.rand(nTest, 1) xTest = testSet[:, 0] # Comparision with actual function plt.figure(0) plt.scatter(xTest, model(tf.convert_to_tensor(testSet)), label="Neural Net") plt.scatter(xTest, np.log(xTest), label="Actual") plt.legend(bbox_to_anchor=(0, 1.02, 1, 0.2), loc='lower left', ncol=2, mode="expand") # Convergence History plt.figure(1) plt.plot(model.lossHistory) plt.yscale('log') plt.xlabel("Optimizer Iteration") plt.ylabel("Loss Value") plt.show()