Beispiel #1
0
def cumulative_distribution(
    data,
    scaled=False,
    survival=False,
    label="Cumulative",
    fill=False,
    target_length=np.inf,
    downsample_method="geom",
    flip=False,
    preserve_ends=0,
    **kwargs
):
    """
    plots cumulative (or survival) step distribution
    """
    data = np.sort(data)
    if survival:
        data = data[::-1]
    y = np.arange(data.size + 1, dtype=float)
    if scaled:
        y /= y[-1]
    x = np.concatenate([data, data[[-1]]])
    if len(x) > target_length:
        if downsample_method == "geom":
            x, y = log_downsample(x, y, target_length=target_length, flip=flip)
        elif downsample_method == "even_end":
            x, y = downsample(
                x, y, target_length=target_length, preserve_ends=preserve_ends
            )
        else:
            raise Exception("invalid downsample_method")
    plt.step(x, y, label=label, **kwargs)
    if fill:
        plt.fill_between(x, y, alpha=0.5, step="pre", **kwargs)
Beispiel #2
0
    def _print_summary(self):
        #self.fig.clf()
        try:
            from IPython.display import clear_output
            clear_output(True)
        finally:
            pass
        pylab.figure(figsize=(20, 20))
        for i, key in enumerate(sorted(self.history.keys())):
            pylab.subplot((len(self.history) % self.col) + 1, self.col, i + 1)

            x = numpy.array([i[0] for i in self.history[key]])
            mean = numpy.array([i[1] for i in self.history[key]])
            std = numpy.array([i[2] for i in self.history[key]])
            max = numpy.array([i[3] for i in self.history[key]])
            min = numpy.array([i[4] for i in self.history[key]])

            pylab.fill_between(x, min, max, facecolor='r', alpha=0.1)
            pylab.fill_between(x,
                               mean - std,
                               mean + std,
                               facecolor='b',
                               alpha=0.2)
            pylab.plot(x, mean, label="mean")
            pylab.title(key)
        pylab.legend()
        pylab.show()
Beispiel #3
0
def confidence_intervals(X,
                         Y,
                         c,
                         label="",
                         mid_50_percentile=False,
                         lw=2,
                         ls="-"):
    X, Y = np.array(X), np.array(Y)
    low, upp = st.t.interval(0.99,
                             Y.shape[0] - 1,
                             loc=np.mean(Y, axis=0),
                             scale=st.sem(Y))
    plt.fill_between(np.mean(X, axis=0), low, upp, alpha=0.5, color=c, lw=0)
    plt.plot(np.mean(X, axis=0),
             np.mean(Y, axis=0),
             lw=lw,
             ls=ls,
             c=c,
             label=label)
    if mid_50_percentile:
        plt.fill_between(np.mean(X, axis=0),
                         np.percentile(Y, 25, axis=0),
                         np.percentile(Y, 75, axis=0),
                         alpha=.25,
                         color=c)
def plot_precision_recall_curve(y_true, y_pred_scores, nclasses, pos_label=1):
    """
    :param y_true:
    :param y_pred_scores:
    :param pos_label:
    :return:
    """
    for cls_index, cls in enumerate(nclasses):

        average_precision = average_precision_score(
            y_true[:, cls_index], y_pred_scores[:, cls_index])
        precision, recall, _ = precision_recall_curve(y_true[:, cls_index],
                                                      y_pred_scores[:,
                                                                    cls_index],
                                                      pos_label=pos_label)

        plt.step(recall, precision, color='b', alpha=0.2, where='post')
        plt.fill_between(recall, precision, step='post', alpha=0.2, color='b')

        plt.xlabel('Recall')
        plt.ylabel('Precision')
        plt.ylim([0.0, 1.05])
        plt.xlim([0.0, 1.0])
        plt.title('Precision-Recall curve for class "{0}": AP={1:0.2f}'.format(
            cls.capitalize(), average_precision))
def plot_learning_curve(estimator, title, X, y, cv=5, train_sizes=np.linspace(0.1, 1, 20)):
        
    plt.figure(figsize=(8, 8))
    plt.title(title,fontsize=14)
    
    plt.xlabel("Number of training samples",fontsize=14)
    plt.ylabel("Accuracy score",fontsize=14)
    
    #设置坐标轴刻度
    plt.yticks(np.linspace(0, 1, 20))

    train_sizes, train_scores, test_scores= learning_curve(estimator, X, y, cv=cv,train_sizes=train_sizes)
    print(train_sizes)
    plt.xticks(train_sizes)
    train_scores_mean = np.mean(train_scores, axis=1)
    train_scores_std = np.std(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)
    test_scores_std = np.std(test_scores, axis=1)


    # Plot learning curve
    plt.grid(True)
    plt.fill_between(train_sizes, train_scores_mean - train_scores_std,train_scores_mean + train_scores_std, alpha=0.1,color="r")
    plt.fill_between(train_sizes, test_scores_mean - test_scores_std,test_scores_mean + test_scores_std, alpha=0.1,color="g")
    plt.plot(train_sizes, train_scores_mean, 'o-', color="r",label="Training score")
    plt.plot(train_sizes, test_scores_mean, 'o-', color="g",label="Cross-validation score")
    plt.legend(loc="best",fontsize=14)
    # plt.savefig('./validation_curve.png', dpi=300)
    return plt
Beispiel #6
0
def learn_curve_plot(estimator,title,X,y,cv=None,train_sizes=np.linspace(0.1,1.0,5)):
    '''
    :param estimator: the model/algorithem you choose
    :param title: plot title
    :param x: train data numpy array style
    :param y: target data vector
    :param xlim: axes x lim
    :param ylim: axes y lim
    :param cv:
    :return: the figure
    '''
    plt.figure()

    train_sizes,train_scores,test_scores=\
        learning_curve(estimator,X,y,cv=cv,train_sizes=train_sizes)
    '''this is the key score function'''
    train_scores_mean=np.mean(train_scores,axis=1)
    train_scores_std=np.std(train_scores,axis=1)
    test_scores_mean=np.mean(test_scores,axis=1)
    test_scores_std=np.std(test_scores,axis=1)

    plt.fill_between(train_sizes,train_scores_mean - train_scores_std,
                     train_scores_mean + train_scores_std,alpha=0.1,color='b')
    plt.fill_between(train_sizes,test_scores_mean - test_scores_std,
                     test_scores_mean + test_scores_std,alpha=0.1,color='g')
    plt.plot(train_sizes,train_scores_mean,'o-',color='b',label='training score')
    plt.plot(train_sizes,test_scores_mean,'o-',color='g',label='cross valid score')
    plt.xlabel('training examples')
    plt.ylabel('score')
    plt.legend(loc='best')
    plt.grid('on')
    plt.title(title)
    plt.show()
Beispiel #7
0
def plot_pyro(model, ideal, sd_val=1):
    #plot_observed_data=False, plot_predictions=False, n_prior_samples=0,
    x_test = ideal[0]
    p_test = ideal[1]
    plt.figure(figsize=(12, 6))
    plt.plot(model.X.numpy(), model.y.numpy(), 'kx')
    Xtest = torch.Tensor(x_test)  # test inputs
    with torch.no_grad():
        if type(model) in (pyro.contrib.gp.models.VariationalSparseGP,
                           pyro.contrib.gp.models.VariationalGP):
            mean, cov = model(Xtest, full_cov=True)
        else:
            mean, cov = model(Xtest, full_cov=True, noiseless=False)
    sd = cov.diag().sqrt()  # standard deviation at each input point x
    mean_p = model.likelihood.response_function(mean)
    inf, sup = mean - sd_val * sd, mean + sd_val * sd
    inf_p = model.likelihood.response_function(inf)
    sup_p = model.likelihood.response_function(sup)
    plt.plot(Xtest.numpy(), mean_p.numpy(), 'b', lw=2)  # plot the mean
    plt.plot(Xtest.numpy(), p_test, 'r--', lw=2)  # plot real landscape
    plt.fill_between(
        Xtest.numpy().reshape(
            -1),  # plot the two-sigma uncertainty about the mean
        inf_p.numpy().reshape(-1),
        sup_p.numpy().reshape(-1),
        color='C0',
        alpha=0.3)
def printAccCurve(regType='OLS'):
    for time in times:
        curveData = np.array(
            list(
                csv.reader(
                    open(
                        './5-accCurveNoSameTake-' + time + '_' + regType +
                        '.csv', 'r')))).astype(float)
        CIi = []
        CIa = []
        for l in curveData:
            CIi.append(l[0] - (1.96 * (l[1] / math.sqrt(l[5]))))
            CIa.append(l[0] + (1.96 * (l[1] / math.sqrt(l[5]))))

        #plt.axhline(1.2)

        # Set up dashed lines
        for y in np.arange(0.1, 1.11, 0.1):
            plt.axhline(y, color='k', ls='--', alpha=0.25)

        Xs = range(5, 121, 5)

        plt.plot(Xs, curveData[:, 4], label='Maximum')
        plt.plot(Xs, curveData[:, 2], label='Median')
        plt.plot(Xs, curveData[:, 0], label='Mean')
        plt.plot(Xs, CIi, 'k', alpha=0.2, label='95% Confidence Interval')
        plt.plot(Xs, CIa, 'k', alpha=0.2)
        plt.fill_between(Xs, CIi, CIa, facecolor='k', alpha=0.1)
        plt.plot(Xs, curveData[:, 3], label='Minimum')

        plt.title('Accuracy Curves for ' + time + ',' + regType)
        plt.legend(loc='best')
        plt.xlabel('Time Points')
        plt.ylabel('Accuracy')
        plt.show()
Beispiel #9
0
 def plot_fit_function(self, num_points=100):
     '''
     try:
         x_coords = np.linspace(self.x_vec[0], self.x_vec[-1], num_points)
     except Exception as message:
         print 'no x axis information specified', message
         return
     '''
     if self.landscape:
         for trace in self.landscape:
             try:
                 #plt.clear()
                 plt.plot(self.x_vec, trace)
                 plt.fill_between(self.x_vec,
                                  trace + float(self.span) / 2,
                                  trace - float(self.span) / 2,
                                  alpha=0.5)
             except Exception:
                 print 'invalid trace...skip'
         plt.axhspan(self.y_vec[0],
                     self.y_vec[-1],
                     facecolor='0.5',
                     alpha=0.5)
         plt.show()
     else:
         print 'No trace generated.'
def visualise_posterior(model, X_test, Y_test, Y_test_pred, mixture=True, title=None, new_fig=False):

    ''' Visualising posterior predictive (a single distribution) '''

    if new_fig:
        plt.figure(figsize=(5,5))
    #f, ax = plt.subplots(1, 1, figsize=(8, 8))
    if mixture:
        sample_means, sample_stds = get_posterior_predictive_means_stds(Y_test_pred)

        sample_stds = torch.nan_to_num(sample_stds, nan=1e-5)
        lower, upper = get_posterior_predictive_uncertainty_intervals(sample_means, sample_stds)
        pred_mean = sample_means.mean(dim=0)
    else:
        lower, upper = Y_test_pred.confidence_region()
        lower=lower.detach().numpy()
        upper=upper.detach().numpy()
        pred_mean = Y_test_pred.mean.numpy()

    plt.plot(X_test.numpy(), pred_mean, 'b-', label='Mean')
    plt.scatter(model.covar_module.inducing_points.detach(), [-2.5]*model.num_inducing, c='r', marker='x', label='Inducing')
    plt.fill_between(X_test.detach().numpy(), lower, upper, alpha=0.5, label=r'$\pm$2\sigma', color='g')
    plt.scatter(model.train_x.numpy(), model.train_y.numpy(), c='k', marker='x', alpha=0.7, label='Train')
    plt.plot(X_test, Y_test, color='b', linestyle='dashed', alpha=0.7, labeL='True')
    #ax.set_ylim([-3, 3])
    plt.legend(fontsize='small')
    plt.title(title)
Beispiel #11
0
def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None,
                        n_jobs=1, train_sizes=np.linspace(.1, 1.0, 5)):
    plt.figure()
    plt.title(title)
    if ylim is not None:
        plt.ylim(*ylim)
    plt.xlabel("Training examples")
    plt.ylabel("Score")
    train_sizes, train_scores, test_scores = learning_curve(
        estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
    train_scores_mean = np.mean(train_scores, axis=1)
    train_scores_std = np.std(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)
    test_scores_std = np.std(test_scores, axis=1)

    plt.grid()

    plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
                     train_scores_mean + train_scores_std, alpha=0.1,
                     color="r")
    plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
                     test_scores_mean + test_scores_std, alpha=0.1, color="g")
    plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
             label="Training score")
    plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
             label="Cross-validation score")

    plt.legend(loc="best")
    plt.show()
    return train_sizes, train_scores_mean, test_scores_mean
Beispiel #12
0
	def transition_related_averaging_run(self, simulation_data, smoothing_kernel_width = 200, sampling_interval = [-50, 150], plot = True ):
		"""docstring for transition_related_averaging"""
		transition_occurrence_times = self.transition_occurrence_times(simulation_data = simulation_data, smoothing_kernel_width = smoothing_kernel_width)
		# make sure only valid transition_occurrence_times survive
		transition_occurrence_times = transition_occurrence_times[(transition_occurrence_times > -sampling_interval[0]) * (transition_occurrence_times < (simulation_data.shape[0] - sampling_interval[1]))]
		
		# separated into on-and off periods:
		transition_occurrence_times_separated = [transition_occurrence_times[::2], transition_occurrence_times[1::2]]
		
		mean_time_course, std_time_course = np.zeros((2, sampling_interval[1] - sampling_interval[0], 5)), np.zeros((2, sampling_interval[1] - sampling_interval[0], 5))
		if transition_occurrence_times_separated[0].shape[0] > 2:
			
			for k in [0,1]:
				averaging_interval_times = np.array([transition_occurrence_times_separated[k] + sampling_interval[0],transition_occurrence_times_separated[k] + sampling_interval[1]]).T
				interval_data = np.array([simulation_data[avit[0]:avit[1]] for avit in averaging_interval_times])
				mean_time_course[k] = interval_data.mean(axis = 0)
				std_time_course[k] = (interval_data.std(axis = 0) / np.sqrt(interval_data.shape[0]))
			
			if plot:
				f = pl.figure(figsize = (10,8))
				for i in range(simulation_data.shape[1]):
					s = f.add_subplot(simulation_data.shape[1], 1, 1 + i)
					for j in [0,1]:
						pl.plot(np.arange(mean_time_course[j].T[i].shape[0]), mean_time_course[j].T[i], ['r--','b--'][j], linewidth = 2.0 )
						pl.fill_between(np.arange(mean_time_course[j].shape[0]), mean_time_course[j].T[i] + std_time_course[j].T[i], mean_time_course[j].T[i] - std_time_course[j].T[i], ['r','b'][j], alpha = 0.2)
					s.set_title(self.variable_names[i])
				pl.draw()
			
		return (mean_time_course, std_time_course)
Beispiel #13
0
    def _print_summary(self):
        # self.fig.clf()
        try:
            from IPython.display import clear_output
            clear_output(True)
        finally:
            pass
        pylab.figure(figsize=(20, 20))
        x = numpy.array([i[0] for i in self.history])
        mean = numpy.array([i[1] for i in self.history])
        std = numpy.array([i[2] for i in self.history])
        max = numpy.array([i[3] for i in self.history])
        min = numpy.array([i[4] for i in self.history])

        pylab.fill_between(x, min, max, facecolor='r', alpha=0.1)
        pylab.fill_between(x, mean - std, mean + std, facecolor='b', alpha=0.2)
        pylab.plot(
            x,
            mean,
            "+-",
            label="mean",
        )
        pylab.legend()
        pylab.show()

        print(f"mean:{mean[-1]}, std:{std[-1]}, max:{max[-1]}, min:{min[-1]}")
Beispiel #14
0
def plot_beta_dist( ctr, trials, success, alphas, betas, turns ):
	"""
	Pass in the ctr, trials and success, alphas, betas returned
	by the `experiment` function and the number of turns 
	and plot the beta distribution for all the arms in that turn
	"""
	subplot_num = len(turns) / 2
	x = np.linspace( 0.001, .999, 200 )
	fig = plt.figure( figsize = ( 14, 7 ) ) 

	for idx, turn in enumerate(turns):

		plt.subplot( subplot_num, 2, idx + 1 )

		for i in range( len(ctr) ):
			y = beta( alphas[i] + success[ turn, i ], 
					  betas[i] + trials[ turn, i ] - success[ turn, i ] ).pdf(x)
			line = plt.plot( x, y, lw = 2, label = "arm {}".format( i + 1 ) )
			color = line[0].get_color()
			plt.fill_between( x, 0, y, alpha = 0.2, color = color )
			plt.axvline( x = ctr[i], color = color, linestyle = "--", lw = 2 )
			plt.title("Posteriors After {} turns".format(turn) )
			plt.legend( loc = "upper right" )

	return fig
Beispiel #15
0
def estimateErrorBlocking(valuesIn,minBlocks=10,makePlot=False):
    
    if len(valuesIn)<minBlocks:
        raise NotEnoughData()
        
    values=valuesIn
    errors=[]
    Ms=[]
    i=len(values)
    
    while i>=minBlocks:
        values=block(values,i)
        errors.append(np.sqrt(np.mean(values**2)-np.mean(values)**2)/sqrt(i))
        i=i/2

    errors=np.array(errors)
    Ms=2**np.linspace(0,len(errors),num=len(errors))
   
    index2=1./np.sqrt(Ms)
    params,covs=optimize.curve_fit(lambda x,a,b: a*x + b,index2,errors)
    res=stats.linregress(index2,errors)
    errorsFit=[np.sqrt(covs[0][0]),np.sqrt(covs[1][1])]
    
    if makePlot:
        x=np.linspace(np.min(Ms),np.max(Ms),num=1000)
        plt.plot(Ms,errors,"o")
        plt.plot(x,params[1] + params[0]/np.sqrt(x))
        y1=params[1]-errorsFit[1] + (params[0]-errorsFit[0])/np.sqrt(x)
        y2=params[1]+errorsFit[1] + (params[0]+errorsFit[0])/np.sqrt(x)
        plt.fill_between(x, y1, y2, where=y2 >= y1, interpolate=True,alpha=0.2)
        plt.plot(x,params[1]+0*x ,"--")
    
    return [params[1],errorsFit[1],abs(params[0]/(params[1]*sqrt(np.max(Ms))))]
def plot_micro_prec_recall(precision, recall, average_precision):
    """
    Created an average precision plot, micro-averaged over all classes
    :param precision: dict.
    :param recall: dict.
    :param average_precision: dict.
    :return: plot
    """
    sns.set()
    sns.set_style("dark")
    plt.figure()
    plt.step(recall['micro'],
             precision['micro'],
             color='b',
             alpha=0.2,
             where='post')
    plt.fill_between(recall["micro"],
                     precision["micro"],
                     step='post',
                     alpha=0.2,
                     color='b')

    plt.xlabel('Recall', fontsize=18)
    plt.ylabel('Precision', fontsize=18)
    plt.ylim([0.0, 1.05])
    plt.xlim([0.0, 1.0])
    plt.title(
        'Average precision score, micro-averaged over all classes: AP={0:0.2f}'
        .format(average_precision["micro"]),
        fontsize=24)

    plt.show()
Beispiel #17
0
def ocean():
    dmax = 1650.e3
    dp = np.linspace(0., dmax, 3301)
    zp = maketopo.shelf1(dp) 
    plt.clf()
    plt.plot(dp*1e-3,zp,'k-',linewidth=3)
    plt.fill_between(dp*1e-3,zp,0.,where=(zp<0.), color=[0,0.5,1])
    plt.xlim([0.,1650])
    plt.ylim([-4500.,500])
    plt.title("Topography as function of radius",fontsize=18)
    plt.xlabel("kilometers from center",fontsize=15)
    plt.ylabel("meters",fontsize=15)
    plt.xticks(fontsize=15)
    plt.yticks(fontsize=15)
    plt.savefig('topo1.png')
    #plt.savefig('topo1.tif')
    print "Cross-section plot in topo1.png"

    plt.xlim([1500,1650])
    plt.ylim([-200.,20])
    plt.title("Topography of shelf and beach",fontsize=18)
    plt.xlabel("kilometers from center",fontsize=15)
    plt.ylabel("meters",fontsize=15)
    plt.xticks(fontsize=15)
    plt.yticks(fontsize=15)
    plt.savefig('topo2.png')
    #plt.savefig('topo2.tif')
    print "Zoom near shore in topo2.png"
def  showMeanSTDMetric(metric_name, m_table, title, output_fn = None):
    pl.figure()
    ax = pl.subplot(111)
    #pl.set_context("talk", font_scale=1, rc={"lines.linewidth": 0.2})
    siz = table.shape[0]
  #  ax=pl.errorbar(range(siz),m_table[metric_name],yerr=m_table[metric_name], fmt='o')

    ax.spines["top"].set_visible(False)
    ax.spines["right"].set_visible(False)

    ax.get_xaxis().tick_bottom()
    ax.get_yaxis().tick_left()

    pl.fill_between(range(siz), m_table[metric_name+'_mean'] - m_table[metric_name+'_std'],
                m_table[metric_name+'_mean'] + m_table[metric_name+'_std'], color="#3F5D7D",alpha=0.8, lw=0.001)
    pl.plot(range(siz), m_table[metric_name+'_mean'], color="r", lw=2)

    labels = range(1,siz,30)

    #g = sns.FacetGrid(m_table)
    #g.map(pl.errorbar, "csv_path", metric_name, yerr= m_table[metric_name+'_std'] , marker="o")

    pl.xticks(labels,labels,fontsize=9)
    pl.title(title)
    pl.xlabel('Image ID 1 ~ ' + str(siz))
    if ( output_fn is None):
        output_fn = data_DIR+'/'+metric_name+title+'.mean_std.pdf'
    pl.savefig(output_fn)
    pl.show()
Beispiel #19
0
def plot_effective_mass(data_file,settings_file="input.xml",label="",param_file="effective_mass_t.dat",factor=1,fit_lines=True):
    root=ET.parse(settings_file).getroot()
    skip=int(root.find("measures").find("winding_number").attrib["skip"])
    time_step=float(root.find("method").find("delta_tau").text)
    fit_mean=[]
    fit_low=[]
    fit_up=[]
    
    data=tools.read_matrix_from_file(data_file)
    data[1]=data[1]*factor
    data[2]=data[2]*factor
    #plt.ylim(0,1)
    if (os.path.isfile(param_file) and fit_lines==True):
        
        with open(param_file,"r") as f:
            cut_low=int(f.readline())
            cut_heigh=int(f.readline())
            params=f.readline().split();
            errors=f.readline().split();
            a=(time_step*(skip+1))
            for x in (data[0][cut_low:cut_heigh]*a):

                #values.append(effective_mass_exp_curve(float(x),float(params[0]),float(params[1]),float(params[2])))
                fit_mean.append(hyperbole(float(x),float(params[0]),float(params[1])*a))
                fit_low.append(hyperbole(float(x),float(params[0])-float(errors[0]),float(params[1])*a - float(errors[1])*a))
                fit_up.append(hyperbole(float(x),float(params[0])+float(errors[0]),float(params[1])*a + float(errors[1])*a))
            plt.plot(data[0][cut_low:cut_heigh]*a,fit_mean,linewidth=4)
            plt.fill_between(data[0][cut_low:cut_heigh]*a,fit_low,fit_up,alpha=0.4)
            
    return plt.errorbar(data[0]*time_step*(skip+1),data[1]/(data[0]*time_step*(skip+1)), yerr=data[2]/(data[0]*time_step*(skip+1)),label=label,fmt='o',ms=1)
def plot_posterior_linear(params_fname, fig_fname, control=False, M=20):
    # load dataset
    data = np.loadtxt('./sandbox/hh_data.txt')
    # use the voltage and potasisum current
    data = data / np.std(data, axis=0)
    y = data[:, :4]
    xc = data[:, [-1]]
    # init hypers
    Dlatent = 2
    Dobs = y.shape[1]
    T = y.shape[0]
    if control:
        x_control = xc
        no_panes = 5
    else:
        x_control = None
        no_panes = 4
    model_aep = aep.SGPSSM_Linear(y,
                                  Dlatent,
                                  M,
                                  lik='Gaussian',
                                  prior_mean=0,
                                  prior_var=1000,
                                  x_control=x_control)
    model_aep.load_model(params_fname)
    my, vy, vyn = model_aep.get_posterior_y()
    vy_diag = np.diagonal(vy, axis1=1, axis2=2)
    vyn_diag = np.diagonal(vyn, axis1=1, axis2=2)
    cs = ['k', 'r', 'b', 'g']
    labels = ['V', 'm', 'n', 'h']
    plt.figure()
    t = np.arange(T)
    for i in range(4):
        yi = y[:, i]
        mi = my[:, i]
        vi = vy_diag[:, i]
        vin = vyn_diag[:, i]
        plt.subplot(no_panes, 1, i + 1)
        plt.fill_between(t,
                         mi + 2 * np.sqrt(vi),
                         mi - 2 * np.sqrt(vi),
                         color=cs[i],
                         alpha=0.4)
        plt.plot(t, mi, '-', color=cs[i])
        plt.plot(t, yi, '--', color=cs[i])
        plt.ylabel(labels[i])
        plt.xticks([])
        plt.yticks([])

    if control:
        plt.subplot(no_panes, 1, no_panes)
        plt.plot(t, x_control, '-', color='m')
        plt.ylabel('I')
        plt.yticks([])
    plt.xlabel('t')
    plt.savefig(fig_fname)
    if control:
        plot_model_with_control(model_aep, '', '_linear_with_control')
    else:
        plot_model_no_control(model_aep, '', '_linear_no_control')
def showMeanSTDMetric(metric_name, m_table, title, output_fn=None):
    pl.figure()
    ax = pl.subplot(111)
    #pl.set_context("talk", font_scale=1, rc={"lines.linewidth": 0.2})
    siz = table.shape[0]
    #  ax=pl.errorbar(range(siz),m_table[metric_name],yerr=m_table[metric_name], fmt='o')

    ax.spines["top"].set_visible(False)
    ax.spines["right"].set_visible(False)

    ax.get_xaxis().tick_bottom()
    ax.get_yaxis().tick_left()

    pl.fill_between(
        range(siz),
        m_table[metric_name + '_mean'] - m_table[metric_name + '_std'],
        m_table[metric_name + '_mean'] + m_table[metric_name + '_std'],
        color="#3F5D7D",
        alpha=0.8,
        lw=0.001)
    pl.plot(range(siz), m_table[metric_name + '_mean'], color="r", lw=2)

    labels = range(1, siz, 30)

    #g = sns.FacetGrid(m_table)
    #g.map(pl.errorbar, "csv_path", metric_name, yerr= m_table[metric_name+'_std'] , marker="o")

    pl.xticks(labels, labels, fontsize=9)
    pl.title(title)
    pl.xlabel('Image ID 1 ~ ' + str(siz))
    if (output_fn is None):
        output_fn = data_DIR + '/' + metric_name + title + '.mean_std.pdf'
    pl.savefig(output_fn)
    pl.show()
Beispiel #22
0
def plot_link(model, transfo = lambda x:x, percentiles = None, link = True,
                likelihood = False, n_measures = None):
    Xgrid = np.linspace(0, np.pi, 200)[:, np.newaxis]
    mu_f, var_f = model._raw_predict(Xgrid, full_cov=False, kern=None)
    
    s_f = np.random.randn(mu_f.shape[0], 1000) * np.sqrt(var_f) + mu_f
    s_f = transfo(s_f)
    if(link):
        if(likelihood):
            s_p = model.likelihood(s_f, Y_metadata={'trials':n_measures})
        else:
            s_p = model.likelihood.gp_link.transf(s_f)
    else:
        s_p = s_f
    mu_p = np.mean(s_p, axis = 1)
    if(percentiles is None):
        var_p = np.std(s_p, axis = 1)
        muplus = mu_p + 1.96 * var_p
        muminus = mu_p - 1.96 * var_p
    else:
        muminus, muplus = np.percentile(s_p, percentiles, axis = 1)
    
    X_data, Y_data = model.X, model.Y
    x = np.squeeze(Xgrid)    
    xd = np.squeeze(X_data)
    yd = np.squeeze(transfo(Y_data))
    
    plt.plot(x, mu_p, 'b', label = 'proxy')
    plt.fill_between(x, muminus, muplus, color = 'blue', alpha = 0.5, label = 'confidence')
    plt.scatter(xd, yd, c='black', marker='x', label = 'observations')
Beispiel #23
0
def get_metrics(model, X, Y):

    x = np.linspace(-1, 1.2, 1000).reshape(-1, 1)
    x_var = Variable(torch.FloatTensor(x))

    if isinstance(model, BayesMLP):
        # Make several predictions
        list_preds = []
        for i in range(100):
            list_preds.append(model(x_var).data.cpu().numpy())
        preds = np.stack(list_preds)

        plt.scatter(X, Y, s=5, color="C0")
        plt.plot(x, np.median(preds, 0), color="C1")

        upper = np.median(preds, 0) + np.std(preds, 0)
        lower = np.median(preds, 0) - np.std(preds, 0)

        plt.fill_between(x[:, 0],
                         lower[:, 0],
                         upper[:, 0],
                         color='C1',
                         alpha=0.2)

        plt.show()

    else:
        preds = model(x_var).data.cpu().numpy()

        plt.scatter(X, Y, s=5, color="C0")
        plt.plot(x, preds, color="C1")
        plt.show()
def plot_beta_dist(ctr, trials, success, alphas, betas, turns):
    """
	Pass in the ctr, trials and success, alphas, betas returned
	by the `experiment` function and the number of turns 
	and plot the beta distribution for all the arms in that turn
	"""
    subplot_num = len(turns) / 2
    x = np.linspace(0.001, .999, 200)
    fig = plt.figure(figsize=(14, 7))

    for idx, turn in enumerate(turns):

        plt.subplot(subplot_num, 2, idx + 1)

        for i in range(len(ctr)):
            y = beta(alphas[i] + success[turn, i],
                     betas[i] + trials[turn, i] - success[turn, i]).pdf(x)
            line = plt.plot(x, y, lw=2, label="arm {}".format(i + 1))
            color = line[0].get_color()
            plt.fill_between(x, 0, y, alpha=0.2, color=color)
            plt.axvline(x=ctr[i], color=color, linestyle="--", lw=2)
            plt.title("Posteriors After {} turns".format(turn))
            plt.legend(loc="upper right")

    return fig
Beispiel #25
0
def make_graph(folder, c, i):
	ls = "dotted"
	if "f100" in folder:
		ls = "dashed"
	if "f1000" in folder or "qlearning" in folder:
		ls = "solid" 
	df = pd.read_csv("{}/{}/{}".format(directory_path, folder, data_path))
	x = np.array(df.loc[:, x_axis].values)
	y = np.array(df.loc[:, y_axis].values)
	#print x
	#print y
	if rolling_mean == 1:
		y = pd.Series(y).rolling(window=rolling_width).mean()
		#print y
	split = folder.split("_")
	index = None
	for i in range(len(split)):
		if re.match("f" + r"[0-9]+", split[i]):
			index = i
	if update_freq_label == True and index:
		plt.plot(x, y, color = c, label = re.search(r'[0-9]+', split[index]).group(), linestyle = ls)
	else:
		plt.plot(x, y, color = c, label="{}".format(folder), linestyle = ls)
	if mode == "test" and variance == True:
		reward_std = np.array(df.loc[:, "reward_std"].values)
		plt.fill_between(x, y+reward_std, y-reward_std, facecolor=c, alpha=0.3)
Beispiel #26
0
def SNPhot_plotter_filt(obs, gp, filt=b'r'):
    df = obs[obs.FLT == filt]
    x = df.MJD.values
    y = df.FLUXCAL.values
    dy = df.FLUXCALERR.values
    colors = {b'g': 'g', b'r': 'r', b'i': 'c', b'z': 'm'}

    pred = pred_var = 0.
    if gp is not None:
        x_pred = np.linspace(x.min() - 100., x.max() + 100., 1000)
        pred, pred_var = gp.predict(y, x_pred, return_var=True)
        #pred, pred_cov = gp.predict(y, x_pred, return_cov=True)
        pred_sig = np.sqrt(np.abs(pred_var))
        plt.fill_between(x_pred,
                         pred - 2 * pred_sig,
                         pred + 2 * pred_sig,
                         color=colors[filt],
                         alpha=0.2)
        plt.plot(x_pred, pred, "k", lw=1.5, alpha=0.5)
    plt.ylim(
        np.append(np.append(y - dy * 1.1, [0]), [pred - pred_sig]).min(),
        np.append(y + dy * 1.1, [pred + pred_sig]).max())
    plt.errorbar(x, y, yerr=dy, fmt=".k", capsize=0)
    plt.xlim(x.min() - 30, x.max() + 30)
    plt.title(filt)
    return plt
Beispiel #27
0
    def plot(self, gp_num=0):
        """
        Plot the mixture of Gaussian Processes.
        """
        from matplotlib import pylab as plt
        from matplotlib import cm

        XX = np.linspace(self.X.min(), self.X.max())[:, None]

        YY_mu, YY_var = self.predict_components(XX)

        plt.scatter(self.X,
                    self.Y,
                    c=self.phi[:, gp_num],
                    cmap=cm.RdBu,
                    vmin=0.,
                    vmax=1.,
                    lw=0.5)
        plt.colorbar(label='GP {} assignment probability'.format(gp_num))

        GPy.plotting.matplot_dep.Tango.reset()

        for i in range(self.phi.shape[1]):
            col = GPy.plotting.matplot_dep.Tango.nextMedium()
            plt.fill_between(XX[:, 0],
                             YY_mu[:, i] - 2 * np.sqrt(YY_var[:, i]),
                             YY_mu[:, i] + 2 * np.sqrt(YY_var[:, i]),
                             alpha=0.1,
                             facecolor=col)
            plt.plot(XX, YY_mu[:, i], c=col, lw=2)
Beispiel #28
0
def nova_plot():

	erg2mev=624151.

	fig=plot.figure()
	yrange = [1e-6,2e-4]
	xrange = [1e-1,1e5]
	plot.fill_between([0.2,10e3],[yrange[1],yrange[1]],[yrange[0],yrange[0]],facecolor='yellow',interpolate=True,color='yellow',alpha=0.5)
	plot.annotate('AMEGO',xy=(3,9e-5),xycoords='data',fontsize=26,color='black')

	lat=ascii.read("data/NMon2012.LAT.dat",names=['energy','en_low','en_high','flux','flux_err','tmp'])
	plot.scatter(lat['energy'],lat['flux']*erg2mev,color='red')
	plot.errorbar(lat['energy'],lat['flux']*erg2mev,xerr=[lat['en_low'],lat['en_high']],yerr=lat['flux_err']*erg2mev,ecolor='red',capsize=0,fmt='none')
	latul=ascii.read("data/NMon2012.LAT.limits.dat",names=['energy','en_low','en_high','flux','tmp1','tmp2','tmp3','tmp4'])
	plot.errorbar(latul['energy'],latul['flux']*erg2mev,xerr=[latul['en_low'],latul['en_high']],yerr=0.5*latul['flux']*erg2mev,uplims=True,ecolor='red',capsize=0,fmt='none')
	plot.scatter(latul['energy'],latul['flux']*erg2mev,color='red')

	leptonic=ascii.read("data/sp-NMon12-IC-best-fit-1MeV-30GeV.txt",names=['energy','flux'],data_start=1)
	hadronic=ascii.read("data/sp-NMon12-pi0-and-secondaries.txt",names=['energy','flux1','flux2'],data_start=1)	

	plot.plot(leptonic['energy'],leptonic['flux']*erg2mev,'r--',color='black',lw=2,label='Leptonic')
	plot.plot(hadronic['energy'],hadronic['flux2']*erg2mev,color='black',lw=2,label='Hadronic+Secondary Leptons')

	plot.legend(loc='upper right',fontsize='small',frameon=False,framealpha=0.5)
	plot.xscale('log')
	plot.yscale('log')
	plot.ylim(yrange)
	plot.xlim(xrange)
	plot.xlabel(r'Energy (MeV)')
	plot.ylabel(r'Energy$^2 \times $ Flux (Energy) (erg cm$^{-2}$ s$^{-1}$)')
	plot.title('Nova V339 Del 2013')
	plot.savefig('Nova_SED.png', bbox_inches='tight')
	plot.savefig('Nova_SED.eps', bbox_inches='tight')
	plot.show()
	plot.close()
Beispiel #29
0
def extractEffectiveMass(df,cut_low=0,cut_right=None,n_cuts=5,makePlot=False):
    if cut_right==None:
        cut_right=max(df["t"])
    window=(cut_right-cut_low)/n_cuts
    
    slope=np.zeros(n_cuts)
    intercept=np.zeros(n_cuts)
    slopeError=np.zeros(n_cuts)
    interceptError=np.zeros(n_cuts)
    
    for i in range(0,n_cuts):
        # select the right intervals for the linear fit
        cut_low_current=cut_low + i*window
        cut_high_current=cut_low + (i+1)*window
        df1=df[(df["t"]>cut_low_current) & (df["t"]<cut_high_current) ]
        if len(df1["t"]) <= 3:
            raise not_enough_data()
        
        params,covs=curve_fit(linear,df1["t"],df1["W"],sigma=df1["deltaW"],maxfev=100000)
        slope[i]=params[0]
        intercept[i]=params[1]
        slopeError[i]=sqrt(covs[0][0])
        interceptError[i]=sqrt(covs[1][1])
        
        if makePlot==True:
            up=(slope[i]+slopeError[i])+(intercept[i]+interceptError[i])/df1["t"]
            down=(slope[i]-slopeError[i])+(intercept[i]-interceptError[i])/df1["t"]
            
            plt.fill_between(df1["t"],up,down,alpha=0.4)
            plt.errorbar(np.array(df1["t"]),np.array(df1["W"])/np.array(df1["t"]),np.array(df1["deltaW"])/np.array(df1["t"]),fmt="or")
    return np.array([slope.mean(),sqrt( slopeError.mean()**2 + slope.var())])
Beispiel #30
0
def plot_spectra(eigval_col, savename="spectrum_onetrial.jpg", figdir=savedir, fig=None, label="BP"):
    """A local function to compute these figures for different subspaces. """
    eigmean = eigval_col.mean(axis=0)
    eiglim = np.percentile(eigval_col, [5, 95], axis=0)
    sortidx = np.argsort(-np.abs(eigmean))
    eigmean = np.abs(eigmean[sortidx])
    eiglim = eiglim[:, sortidx]
    eigN = len(eigmean)
    if fig is None:
        fig, axs = plt.subplots(1, 2, figsize=[10, 5])
    else:
        # plt.figure(fig.number)
        plt.figure(num=fig.number)
        axs = fig.axes
    plt.sca(axs[0])
    plt.plot(range(eigN), eigmean, alpha=0.6)
    plt.fill_between(range(eigN), eiglim[0, :], eiglim[1, :], alpha=0.3, label=label)
    plt.ylabel("eigenvalue")
    plt.xlabel("eig id")
    plt.legend()
    plt.sca(axs[1])
    plt.plot(range(eigN), np.log10(eigmean), alpha=0.6)
    plt.fill_between(range(eigN), np.log10(eiglim[0, :]), np.log10(eiglim[1, :]), alpha=0.3, label=label)
    plt.ylabel("eigenvalue(log)")
    plt.xlabel("eig id")
    plt.legend()
    st = plt.suptitle("Hessian Spectrum of StyleGAN\n (error bar for [5,95] percentile among all samples)")
    plt.savefig(join(figdir, savename), bbox_extra_artists=[st]) # this is working.
    # fig.show()
    return fig
def plot_statistics_of(cvar, monitoring):
    ''' plots min/max/mean timeseries,
    takes variable without suffix as string (e.g. dynstat_sst)'''
    meanvar = monitoring[monitoring.gcmvariable == cvar +
                         '_mean'].gcmvalue.values
    minvar = monitoring[monitoring.gcmvariable == cvar +
                        '_min'].gcmvalue.values
    maxvar = monitoring[monitoring.gcmvariable == cvar +
                        '_max'].gcmvalue.values

    fig = plt.figure(figsize=[12, 8])

    plt.subplot(121)
    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%Y'))
    plt.gca().xaxis.set_major_locator(mdates.MonthLocator())
    plt.gca().xaxis.set_minor_formatter(mdates.DateFormatter('%d'))
    plt.gca().xaxis.set_minor_locator(mdates.DayLocator())
    plt.fill_between(monitoring['dates'], minvar, maxvar, color='grey')
    plt.plot(monitoring['dates'], meanvar, 'r.')
    plt.title('range and mean ' + cvar)
    plt.gcf().autofmt_xdate()

    plt.subplot(122)
    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%Y'))
    plt.gca().xaxis.set_major_locator(mdates.MonthLocator())
    plt.gca().xaxis.set_minor_formatter(mdates.DateFormatter('%d'))
    plt.gca().xaxis.set_minor_locator(mdates.DayLocator())
    plt.plot(monitoring['dates'], meanvar, 'r.')
    plt.title('zoom on mean ' + cvar)
    plt.gcf().autofmt_xdate()
    plt.show()
def get_test_result(model, lr, isplot = True):
    print(dataset_filename)
    tasks = pickle.load(open(dataset_filename, "rb"))
    tasks_test = get_torch_tasks(tasks["tasks_test"], task_id_list[0], start_id = num_train_tasks, num_forward_steps = forward_steps[-1], is_oracle = is_oracle, is_cuda = is_cuda)

    task_keys_all = list(tasks_test.keys())
    mse_list_all = []
    for i in range(int(len(tasks_test) / 100)):
        print("{0}:".format(i))
        task_keys_iter = task_keys_all[i * 100: (i + 1) * 100]
        tasks_test_iter = {task_key: tasks_test[task_key] for task_key in task_keys_iter}
        mse = plot_quick_learn_performance(model, tasks_test_iter, is_time_series = is_time_series, forward_steps = forward_steps, lr = lr, epochs = 20, isplot = isplot)['model_0'].mean(0)
        mse_list_all.append(mse)
    mse_list_all = np.array(mse_list_all)
    info_dict["mse_test_lr_{0}".format(lr)] = mse_list_all
    pickle.dump(info_dict, open(filename + "info.p", "wb"))
    print("mean:")
    print(mse_list_all.mean(0))
    print("std:")
    print(mse_list_all.std(0))
    if isplot:
        plt.figure(figsize = (8,6))
        mse_list_all = np.array(mse_list_all)
        mse_mean = mse_list_all.mean(0)
        mse_std = mse_list_all.std(0)

        plt.fill_between(range(len(mse_mean)), mse_mean - mse_std * 1.96 / np.sqrt(int(len(tasks_test) / 100)), mse_mean + mse_std * 1.96 / np.sqrt(int(len(tasks_test) / 100)), alpha = 0.3)
        plt.plot(range(len(mse_mean)), mse_mean)
        plt.title("{0}, {1}-shot regression, lr = {2}".format(task_id_list[0], num_shots, lr), fontsize = 20)
        plt.xlabel("Number of gradient steps", fontsize = 18)
        plt.ylabel("Mean Squared Error", fontsize = 18)
        plt.show()
    return mse_list_all
Beispiel #33
0
def visualize(sample, lib, **kwargs):
    lab = ["out. temp", "indoor temp", "Hvac W", "Sun W"]
    col = ["blue", "green", "red", "orange"]
    xrange = np.arange(sample.shape[0])
    ax1 = plt.subplot(111)
    ax1.set_ylabel("°C")
    plt.title(lib)
    plt.plot(sample[:, 1], label=lab[1], color=col[1])
    plt.plot(sample[:, 0], label=lab[0], color=col[0])
    if len(kwargs):
        # you can plot 4 extra curves
        icons = [':', '--', 'o', '*']
        indice = 0
        for key, vals in kwargs.items():
            plt.plot(vals,
                     icons[indice],
                     markersize=2,
                     label="{}.".format(key))
            indice += 1
    plt.legend(loc='upper left')
    ax2 = ax1.twinx()
    ax2.set_ylabel("W")
    plt.plot(sample[:, 2], label=lab[2], color=col[2])
    plt.fill_between(xrange, 0, sample[:, 2], color=col[2], alpha=0.2)
    plt.plot(sample[:, 3], label=lab[3], color=col[3])
    plt.fill_between(xrange, 0, sample[:, 3], color=col[3], alpha=0.4)
    plt.legend(loc='upper right')
    plt.show()
Beispiel #34
0
def spectra_montage(GANlist, fnlist, ylog=True, xnorm=False, ynorm=True, shade=True, xlim=(-25, 525), ylim=None,\
                                             lw=1, fn="spectra_synopsis_log_rank"):
    fig = plt.figure(figsize=[5,4.5])
    for i, GAN in enumerate(GANlist):
        with np.load(join(rootdir, fnlist[i])) as data:
            eigval_col = data["eigval_col"]
        if eigval_col[:, -1].mean() > eigval_col[:, 0].mean():
            eigval_col = eigval_col[:, ::-1]
        eva_mean = eigval_col.mean(axis=0)
        eva_lim = np.percentile(eigval_col, [5, 95], axis=0)
        # eva_lim_pos = np.maximum(eva_lim, cutoffR * eva_mean.max())
        eva_lim_pos = eva_lim.copy()
        if ylog:
            negmask = eva_lim_pos[0, :] < 0
            eva_lim_pos[0, negmask] = eva_mean[negmask]
        xnormalizer = len(eva_mean) if xnorm else 1
        ynormalizer = eva_mean.max() if ynorm else 1
        ytfm = np.log10 if ylog else lambda x: x
        plt.plot(np.arange(len(eva_mean))/xnormalizer, ytfm(eva_mean / ynormalizer), alpha=.8, lw=lw, label=GAN)  # ,
        # eigval_arr.std(axis=0)
        if shade:
            plt.fill_between(np.arange(len(eva_mean))/xnormalizer, ytfm(eva_lim_pos[0, :] / ynormalizer),
                         ytfm(eva_lim_pos[1, :] / ynormalizer), alpha=0.1)
    plt.ylabel("log10(eig/eigmax)" if ylog else "eig/eigmax")
    plt.xlabel("rank normalized to latent dim" if xnorm else "ranks")
    plt.xlim(xlim)
    if ylim is not None: plt.ylim(ylim)
    plt.title("Spectra Compared Across GANs")
    plt.legend(loc="best")
    plt.savefig(join(rootdir, fn+".png"))
    plt.savefig(join(rootdir, fn+".pdf"))
    plt.show()
    return fig
Beispiel #35
0
def ocean():
    dmax = 1650.e3
    dp = np.linspace(0., dmax, 3301)
    zp = maketopo.shelf1(dp)
    plt.clf()
    plt.plot(dp * 1e-3, zp, 'k-', linewidth=3)
    plt.fill_between(dp * 1e-3, zp, 0., where=(zp < 0.), color=[0, 0.5, 1])
    plt.xlim([0., 1650])
    plt.ylim([-4500., 500])
    plt.title("Topography as function of radius", fontsize=18)
    plt.xlabel("kilometers from center", fontsize=15)
    plt.ylabel("meters", fontsize=15)
    plt.xticks(fontsize=15)
    plt.yticks(fontsize=15)
    plt.savefig('topo1.png')
    #plt.savefig('topo1.tif')
    print("Cross-section plot in topo1.png")

    plt.xlim([1500, 1650])
    plt.ylim([-200., 20])
    plt.title("Topography of shelf and beach", fontsize=18)
    plt.xlabel("kilometers from center", fontsize=15)
    plt.ylabel("meters", fontsize=15)
    plt.xticks(fontsize=15)
    plt.yticks(fontsize=15)
    plt.savefig('topo2.png')
    #plt.savefig('topo2.tif')
    print("Zoom near shore in topo2.png")
Beispiel #36
0
def plot_corridor(x,y,axis=1,alpha=0.2,**kwargs):
    x = np.array(x,dtype=float)
    from matplotlib.pylab import fill_between, plot
    fill_between(x, y.mean(axis)-y.std(axis), y.mean(axis)+y.std(axis),alpha=alpha,**kwargs)
    plot(x, y.mean(axis)+y.std(axis),'-',alpha=alpha,**kwargs)
    plot(x, y.mean(axis)-y.std(axis),'-',alpha=alpha,**kwargs)
    plot(x,y.mean(axis),**kwargs)
    def plot_total_dos(self, **kwargs):
        """
        Plots the total DOS

        Args:
            **kwargs: Variables for matplotlib.pylab.plot customization (linewidth, linestyle, etc.)

        Returns:
            matplotlib.pylab.plot
        """
        try:
            import matplotlib.pylab as plt
        except ImportError:
            import matplotlib.pyplot as plt
        fig = plt.figure(1, figsize=(6, 4))
        ax1 = fig.add_subplot(111)
        ax1.set_xlabel("E (eV)", fontsize=14)
        ax1.set_ylabel("DOS", fontsize=14)
        for i, energies in enumerate(self.energies):
            plt.fill_between(energies,
                             self.t_dos[i],
                             label="spin {}".format(i),
                             **kwargs)
        plt.legend()
        return plt
Beispiel #38
0
def plot_nowcast(model, updates):
    fig = plt.figure(figsize=(20, 10))
    y = model.trace['y_hat_%s' % model.name]
    ddf = pd.DataFrame(
        [
            np.percentile(y, 50, axis=0),
            np.max(y, axis=0),
            np.min(y, axis=0),
        ]
    ).T
    ddf["ds"] = model.data["ds"]
    ddf.columns = ["y_hat", "y_low", "y_high", "ds"]
    ddf["orig_y"] = model.data["y"]
    ddf.plot("ds", "y_hat", ax=plt.gca())
    plt.fill_between(
        ddf["ds"].values,
        ddf["y_low"].values.astype(float),
        ddf["y_high"].values.astype(float),
        alpha=0.3,
    )
    ddf.plot("ds", "orig_y", style="k.", ax=plt.gca(), alpha=0.3)
    for update in updates:
        plt.axvline(
            update, color="C3", lw=1, ls="dotted"
        )
    plt.grid(axis="y")
    return fig
Beispiel #39
0
def my_hist(data, bins, range, field, color, label):
    #plt.hist(data, bins, label=label, alpha=0.2, normed=True,
    #         range=range, color=color)
    y, bin_edges = np.histogram(data, bins=bins, normed=True)
    bin_centers = 0.5 * (bin_edges[1:] + bin_edges[:-1])
    plt.fill_between(bin_centers, y, color=color, alpha=0.2, label=label)
    plt.xlabel('$log_{10}($ ' + field+' $)$')
    plt.ylabel('Density')
Beispiel #40
0
def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None,
                        n_jobs=1, train_sizes=np.linspace(.1, 1.0, 5)):
    """
    Generate a simple plot of the test and traning learning curve.

    Parameters
    ----------
    estimator : object type that implements the "fit" and "predict" methods
        An object of that type which is cloned for each validation.

    title : string
        Title for the chart.

    X : array-like, shape (n_samples, n_features)
        Training vector, where n_samples is the number of samples and
        n_features is the number of features.

    y : array-like, shape (n_samples) or (n_samples, n_features), optional
        Target relative to X for classification or regression;
        None for unsupervised learning.

    ylim : tuple, shape (ymin, ymax), optional
        Defines minimum and maximum yvalues plotted.

    cv : integer, cross-validation generator, optional
        If an integer is passed, it is the number of folds (defaults to 3).
        Specific cross-validation objects can be passed, see
        sklearn.cross_validation module for the list of possible objects

    n_jobs : integer, optional
        Number of jobs to run in parallel (default 1).
    """
    plt.figure()
    plt.title(title)
    if ylim is not None:
        plt.ylim(*ylim)
    plt.xlabel("Training examples")
    plt.ylabel("Score")
    train_sizes, train_scores, test_scores = learning_curve(
        estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
    train_scores_mean = np.mean(train_scores, axis=1)
    train_scores_std = np.std(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)
    test_scores_std = np.std(test_scores, axis=1)
    plt.grid()

    plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
                     train_scores_mean + train_scores_std, alpha=0.1,
                     color="r")
    plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
                     test_scores_mean + test_scores_std, alpha=0.1, color="g")
    plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
             label="Training score")
    plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
             label="Cross-validation score")

    plt.legend(loc="best")
    return plt
Beispiel #41
0
def plot_particles(PARTICLE_FILENAME, TRUTH_FILENAME, OUT_FILENAME):
    T_DELTA = 1/30.

    a = np.load(PARTICLE_FILENAME)
    truth = pickle.load(open(TRUTH_FILENAME, 'r'))
    weights = a['weights']
    particles = a['particles']
    truth_state = truth['state'][:len(particles)]

    STATEVARS = ['x', 'y', 'xdot', 'ydot', 'phi', 'theta']
    vals = dict([(x, []) for x in STATEVARS])
    for p in particles:
        for v in STATEVARS:
            vals[v].append([s[v] for s in p])
    for v in STATEVARS:
        vals[v] = np.array(vals[v])

    pylab.figure()
    for vi, v in enumerate(STATEVARS):


        if 'dot' in v:
            v_truth = np.diff(truth_state[v[0]])/T_DELTA
        else:
            v_truth = truth_state[v]

        v_bar = np.average(vals[v], axis=1, weights=weights)
        x = np.arange(0, len(v_bar))
        cred = np.zeros((len(x), 2), dtype=np.float)
        for ci, (p, w) in enumerate(zip(vals[v], weights)):
            cred[ci] = util.credible_interval(p, w)

        pylab.subplot(len(STATEVARS) + 1,1, 1+vi)
        pylab.plot(v_truth, color='g', linestyle='-', 
                   linewidth=1)

        pylab.plot(x, v_bar, color='b')
        pylab.fill_between(x, cred[:, 0],
                           cred[:, 1], facecolor='b', 
                           alpha=0.4)


        pylab.ylim([np.min(v_truth), 
                    np.max(v_truth)])
        
    pylab.subplot(len(STATEVARS) + 1, 1, len(STATEVARS)+1)
    # now plot the # of particles consuming 95% of the prob mass
    real_particle_num = []
    for w in weights:
        w = w / np.sum(w) # make sure they're normalized
        w = np.sort(w)[::-1] # sort, reverse order
        wcs = np.cumsum(w)
        wcsi = np.searchsorted(wcs, 0.95)
        real_particle_num.append(wcsi)

    pylab.plot(real_particle_num)

    pylab.savefig(OUT_FILENAME)
Beispiel #42
0
def plot_label(label,offset=0):
    Y = df.groupby(cut)[label].mean()
    YERR = df.groupby(cut)[label].std()
    X = df.groupby(cut)["L-cut"].mean()

    color = current_palette.next()
    plt.plot(X+offset,Y,label=label,lw=2,color=color)
    plt.fill_between(X+offset,Y+YERR, Y-YERR,alpha=0.15,color=color)
    return X,Y
Beispiel #43
0
Datei: main.py Projekt: sho-o/DQN
def make_test_graph(directory_path, comment):
	df = pd.read_csv("{}/{}/evaluation/evaluation.csv".format(directory_path, comment))
	total_step = np.array(df.loc[:, "total_step"].values)
	reward_mean = np.array(df.loc[:, "reward_mean"].values)
	reward_std = np.array(df.loc[:, "reward_std"].values)
	#step_mean = np.array(df.loc[:, "step_mean"].values, dtype=np.float)
	#step_std = np.array(df.loc[:, "step_std"].values, dtype=np.float)
	plt.figure()
	plt.plot(total_step, reward_mean, color="red")
	plt.fill_between(total_step, reward_mean+reward_std, reward_mean-reward_std, facecolor='red', alpha=0.3)
	plt.savefig("{}/{}/evaluation/reward.png".format(directory_path, comment))
Beispiel #44
0
def plot_pr(auc_score, precision, recall, label=None):
    pylab.figure(num=None, figsize=(6, 5))
    pylab.xlim([0.0, 1.0])
    pylab.ylim([0.0, 1.0])
    pylab.xlabel('Recall')
    pylab.ylabel('Precision')
    pylab.title('P/R (AUC=%0.2f) / %s' % (auc_score, label))
    pylab.fill_between(recall, precision, alpha=0.5)
    pylab.grid(True, linestyle='-', color='0.75')
    pylab.plot(recall, precision, lw=1)
    pylab.show()
Beispiel #45
0
 def plot_auc(self, auc_score, tpr, fpr, label=None):
     pylab.figure(num = None, figsize=(6, 5))
     pylab.xlim([0.0, 1.0])
     pylab.ylim([0.0, 1.0])
     pylab.xlabel('False Positive Rate')
     pylab.ylabel('True Positive Rate')
     pylab.title('ROC (AUC=%0.2f) / %s' % (auc_score, label))
     pylab.fill_between(fpr, tpr, alpha=0.5)
     pylab.grid(True, linestyle='-', color='0.75')
     pylab.plot(fpr, tpr, lw=1)
     pylab.show()
Beispiel #46
0
def plot_pr(auc_score, name, precision, recall, label=None):
    pylab.figure(num=None, figsize=(6, 5))
    pylab.xlim([0.0, 1.0])
    pylab.ylim([0.0, 1.0])
    pylab.xlabel('Recall')
    pylab.ylabel('Precision')
    pylab.title('P/R (AUC=%0.2f) / %s' % (auc_score, label))
    pylab.fill_between(recall, precision, alpha=0.5)
    pylab.grid(True, linestyle='-', color='0.75')
    pylab.plot(recall, precision, lw=1)
    filename = name.replace(" ", "_")
    pylab.savefig(os.path.join(CHART_DIR, "pr_" + filename + ".png"))
def plot_roc(auc_score, name, fpr, tpr):
    pylab.figure(num=None, figsize=(6, 5))
    pylab.plot([0, 1], [0, 1], "k--")
    pylab.xlim([0.0, 1.0])
    pylab.ylim([0.0, 1.0])
    pylab.xlabel("False Positive Rate")
    pylab.ylabel("True Positive Rate")
    pylab.title("Receiver operating characteristic (AUC=%0.2f)\n%s" % (auc_score, name))
    pylab.legend(loc="lower right")
    pylab.grid(True, linestyle="-", color="0.75")
    pylab.fill_between(tpr, fpr, alpha=0.5)
    pylab.plot(fpr, tpr, lw=1)
    pylab.savefig(os.path.join(CHART_DIR, "roc_" + name.replace(" ", "_") + ".png"))
Beispiel #48
0
def plot_pr(auc_score, name, phase, precision, recall, label=None):
    pylab.clf()
    pylab.figure(num=None, figsize=(5, 4))
    pylab.grid(True)
    pylab.fill_between(recall, precision, alpha=0.5)
    pylab.plot(recall, precision, lw=1)
    pylab.xlim([0.0, 1.0])
    pylab.ylim([0.0, 1.0])
    pylab.xlabel('Recall')
    pylab.ylabel('Precision')
    pylab.title('P/R curve (AUC=%0.2f) / %s' % (auc_score, label))
    filename = name.replace(" ", "_")
    pylab.savefig(os.path.join(CHART_DIR, "pr_%s_%s.png"%(filename, phase)), bbox_inches="tight")
Beispiel #49
0
def plot_pr(auc_score, name, phase, precision, recall, label=None):
    from matplotlib import pylab
    pylab.clf()
    pylab.figure(num=None, figsize=(5, 4))
    pylab.grid(True)
    pylab.fill_between(recall, precision, alpha=0.5)
    pylab.plot(recall, precision, lw=1)
    pylab.xlim([0.0, 1.0])
    pylab.ylim([0.0, 1.0])
    pylab.xlabel('Recall')
    pylab.ylabel('Precision')
    pylab.title('P/R curve (AUC=%0.2f) / %s' % (auc_score, label))
    filename = name.replace(" ", "_")
Beispiel #50
0
def plot_pr(auc_score, precision, recall, label=None):
	pylab.figure(num=None, figsize=(6, 5))
	pylab.xlim([0.0, 1.0])
	pylab.ylim([0.0, 1.0])
	pylab.xlabel('Recall')
	pylab.ylabel('Precision')
	pylab.title('P/R (AUC=%0.2f) / %s' % (auc_score, label))
	pylab.fill_between(recall, precision, alpha=0.5)
	pylab.grid(True, linestyle='-', color='0.75')
	pylab.plot(recall, precision, lw=1)
	pylab.savefig("test.png")
	pylab.show()



# #读取  
# movie_data   = sp.load('movie_data.npy')  
# movie_target = sp.load('movie_target.npy')  
# x = movie_data  
# y = movie_target  
  
# #BOOL型特征下的向量空间模型,注意,测试样本调用的是transform接口  
# count_vec = TfidfVectorizer(binary = False, decode_error = 'ignore', \
#                             stop_words = 'english')  
# average = 0  
# testNum = 10  
# for i in range(0, testNum):  
#     #加载数据集,切分数据集80%训练,20%测试  
#     x_train, x_test, y_train, y_test \
#         = train_test_split(movie_data, movie_target, test_size = 0.2)  
#     x_train = count_vec.fit_transform(x_train)  
#     x_test  = count_vec.transform(x_test)  
  
#     #训练LR分类器  
#     clf = LogisticRegression()  
#     clf.fit(x_train, y_train)  
#     y_pred = clf.predict(x_test)  
#     p = np.mean(y_pred == y_test)  
#     print(p)  
#     average += p  
  
      
# #准确率与召回率  
# answer = clf.predict_proba(x_test)[:,1]  
# precision, recall, thresholds = precision_recall_curve(y_test, answer)      
# report = answer > 0.5  
# print(classification_report(y_test, report, target_names = ['neg', 'pos']))  
# print("average precision:", average/testNum)  
# print("time spent:", time.time() - start_time)  
  
# plot_pr(0.5, precision, recall, "pos")  
Beispiel #51
0
def fitconf(xdata,ydata,errx,erry,covxy,nboot=1000,bcesMethod='ort',linestyle='',conf=0.683,confcolor='gray',xplot=None,front=False,**args):
	"""
	This is a wrapper that given the input data performs the BCES
	fit, get the orthogonal parameters and plot the best-fit line and
	confidence band (generated using analytical methods). I decided to put together 
	these commands in a method because I have been using them very frequently.
	
	Assumes you initialized the plot window before calling this method.
	
	Usage:

	>>> a1,b1,erra1,errb1,cov1=nemmen.fitconf(x[i],y[i],errx[i],erry[i],covxy[i],nboot,bces,linestyle='k',confcolor='LightGrey')
	
	Explanation of some arguments:

	- xplot: if provided, will compute the confidence band in the X-values provided
	with xplot
	- front: if True, then will plot the confidence band in front of the data
	points; otherwise, will plot it behind the points
	"""	
	import bces.bces
	from . import stats
	from . import misc	

	# Selects the desired BCES method
	i=misc.whichbces(bcesMethod)
		
	# Performs the BCES fit
	a,b,erra,errb,cov=bces.bces.bcesp(xdata,errx,ydata,erry,covxy,nboot)
	
	# Plots best-fit
	if xplot==None:
		x=numpy.linspace(xdata.min(),xdata.max(),100)
	else:
		x=xplot
	pylab.plot(x,a[i]*x+b[i],linestyle,**args)

	fitm=numpy.array([ a[i],b[i] ])	# array with best-fit parameters
	covm=numpy.array([ (erra[i]**2,cov[i]), (cov[i],errb[i]**2) ])	# covariance matrix
	def func(x): return x[1]*x[0]+x[2]

	# Plots confidence band
	lcb,ucb,xcb=stats.confbandnl(xdata,ydata,func,fitm,covm,2,conf,x)
	if front==True:
		zorder=10
	else:
		zorder=None
	pylab.fill_between(xcb, lcb, ucb, alpha=0.3, facecolor=confcolor, zorder=zorder)
	
	return a,b,erra,errb,cov
Beispiel #52
0
    def plot(self, gp_num=0):
        """
        Plot the mixture of Gaussian Processes.

        Supports plotting 1d and 2d regression.
        """
        from matplotlib import pylab as plt
        from matplotlib import cm

        XX = np.linspace(self.X.min(), self.X.max())[:, None]

        if self.Y.shape[1] == 1:
            plt.scatter(self.X, self.Y, c=self.phi[:, gp_num], cmap=cm.RdBu, vmin=0., vmax=1., lw=0.5)
            plt.colorbar(label='GP {} assignment probability'.format(gp_num))

            try:
                Tango = GPy.plotting.Tango
            except:
                Tango = GPy.plotting.matplot_dep.Tango
            Tango.reset()


            for i in range(self.phi.shape[1]):
                YY_mu, YY_var = self.predict(XX, i)
                col = Tango.nextMedium()
                plt.fill_between(XX[:, 0],
                                 YY_mu[:, 0] - 2 * np.sqrt(YY_var[:, 0]),
                                 YY_mu[:, 0] + 2 * np.sqrt(YY_var[:, 0]),
                                 alpha=0.1,
                                 facecolor=col)
                plt.plot(XX, YY_mu[:, 0], c=col, lw=2);

        elif self.Y.shape[1] == 2:
            plt.scatter(self.Y[:, 0], self.Y[:, 1], c=self.phi[:, gp_num], cmap=cm.RdBu, vmin=0., vmax=1., lw=0.5)
            plt.colorbar(label='GP {} assignment probability'.format(gp_num))

            try:
                Tango = GPy.plotting.Tango
            except:
                Tango = GPy.plotting.matplot_dep.Tango
            Tango.reset()

            for i in range(self.phi.shape[1]):
                YY_mu, YY_var = self.predict(XX, i)
                col = Tango.nextMedium()
                plt.plot(YY_mu[:, 0], YY_mu[:, 1], c=col, lw=2);

        else:
            raise NotImplementedError('Only 1d and 2d regression can be plotted')
Beispiel #53
0
Datei: main.py Projekt: sho-o/DQN
def make_loss_graph(directory_path, comment, fixed_q_update_counter):
	df = pd.read_csv("{}/{}/loss/{}_loss.csv".format(directory_path, comment, fixed_q_update_counter))
	total_step = np.array(df.loc[:, "total_step"].values)
	loss_mean = np.array(df.loc[:, "loss_mean"].values)
	loss_std = np.array(df.loc[:, "loss_std"].values)
	penalty_mean = np.array(df.loc[:, "penalty_mean"].values)
	penalty_std = np.array(df.loc[:, "penalty_std"].values)
	plt.figure()
	plt.plot(total_step, loss_mean, color="red")
	plt.fill_between(total_step, loss_mean+loss_std, loss_mean-loss_std, facecolor='red', alpha=0.3)
	plt.savefig("{}/{}/loss/{}_loss.png".format(directory_path, comment, fixed_q_update_counter))
	plt.figure()
	plt.plot(total_step, penalty_mean, color="blue")
	plt.fill_between(total_step, penalty_mean+penalty_std, penalty_mean-penalty_std, facecolor='blue', alpha=0.3)
	plt.savefig("{}/{}/loss/{}_penalty.png".format(directory_path, comment, fixed_q_update_counter))
Beispiel #54
0
def plot_roc(auc_score, tpr, fpr, label=None):
    pylab.clf()
    pylab.figure(num=None, figsize=(5, 4))
    pylab.grid(True)
    pylab.plot([0, 1], [0, 1], 'k--')
    pylab.plot(fpr, tpr)
    pylab.fill_between(fpr, tpr, alpha=0.5)
    pylab.xlim([0.0, 1.0])
    pylab.ylim([0.0, 1.0])
    pylab.xlabel('False Positive Rate')
    pylab.ylabel('True Positive Rate')
    pylab.title('ROC curve (AUC = %0.2f) / %s Vs Rest' %
                (auc_score, label), verticalalignment="bottom")
    pylab.legend(loc="lower right")
    pylab.show()
def plot_roc(auc_score, name, tpr, fpr, label=None):
    pylab.clf()
    pylab.figure(num=None, figsize=(5, 4))
    pylab.grid(True)
    pylab.plot([0, 1], [0, 1], "k--")
    pylab.plot(fpr, tpr)
    pylab.fill_between(fpr, tpr, alpha=0.5)
    pylab.xlim([0.0, 1.0])
    pylab.ylim([0.0, 1.0])
    pylab.xlabel("False Positive Rate")
    pylab.ylabel("True Positive Rate")
    pylab.title("ROC curve (AUC = %0.2f) / %s" % (auc_score, label), verticalalignment="bottom")
    pylab.legend(loc="lower right")
    filename = name.replace(" ", "_")
    pylab.savefig(os.path.join(CHART_DIR, "roc_" + filename + ".png"), bbox_inches="tight")
def plot_pr(auc_score, name, phase, precision, recall, label=None):
    """This function plots the ROC curve"""
    pylab.clf()
    pylab.figure(num=None, figsize=(10, 6))
    pylab.grid(True)
    pylab.fill_between(recall, precision, alpha=0.5)
    pylab.plot(recall, precision, lw=1)
    pylab.xlim([0.0, 1.0])
    pylab.ylim([0.0, 1.0])
    pylab.xlabel('Recall')
    pylab.ylabel('Precision')
    pylab.title('P/R curve (AUC=%0.2f) / %s' % (auc_score, label))
    filename = name.replace(" ", "_")
    # pylab.show()
    # pylab.savefig(os.path.join(".", "pr_%s_%s.png"%(filename, phase))
    pylab.savefig("pr_%s_%s.png" % (filename, phase))
Beispiel #57
0
def plot_pr(auc_score, name, precision, recall, label=None):
    """
        Plots Precision-Recall curves.
    """
    pylab.clf()
    pylab.figure(num=None, figsize=(5, 4))
    pylab.grid(True)
    pylab.fill_between(recall, precision, alpha=0.5)
    pylab.plot(recall, precision, lw=1)
    pylab.xlim([0.0, 1.0])
    pylab.ylim([0.0, 1.0])
    pylab.xlabel("Recall")
    pylab.ylabel("Precision")
    pylab.title("P/R curve (AUC = %0.2f) / %s" % (auc_score, label))
    filename = name.replace(" ", "_")
    pylab.savefig(os.path.join(CHART_DIR, "pr_" + filename + ".png"), bbox_inches="tight")