예제 #1
0
    def plot_various_trial_analyses(self,neuron_ind, var_level):
        plt.figure(figsize=(16, 5))

        #the first thing we want to do is just plot the data average
        #so first get the data for all trials
        neuron_i_data_by_trial = self.by_trial_IT_Neural_Data_objmeans_sorted_by_category[var_level][:, :, neuron_ind]
        #now take the mean over the second dimension -- the trial dimension
        neuron_i_data_trial_mean = neuron_i_data_by_trial.mean(1)
        #for convenience, let's compute the min and max values of the neural response
        minval = neuron_i_data_trial_mean.min()
        maxval = neuron_i_data_trial_mean.max()
        #now let's plot the responses across objects
        plt.plot(neuron_i_data_trial_mean)
        #and block stuff to make the categories easier to see
        plt.fill_between(np.arange(64), minval, maxval, 
                         where=(np.arange(64) / 8) % 2, color='k', alpha=0.2)
        plt.xticks(np.arange(0, 64, 8) + 4, self.unique_categories, rotation=30);
        plt.ylabel('Neural Response of neuron %d' % neuron_ind)
        plt.ylim(minval, maxval)
        plt.xlabel('Responses for Variation %s images' % var_level)

        #now let's look at two trials -- the first and 6th ones, for example 
        t1 = 0; t2 = 5
        t1_data = neuron_i_data_by_trial[:, t1]
        t2_data = neuron_i_data_by_trial[:, t2]
        plt.figure(figsize=(12, 5))
        plt.subplot(1, 2, 1)
        plt.plot(t1_data)
        plt.xticks(np.arange(0, 64, 8), self.unique_categories, rotation=30);
        plt.title('Neuron %d, trial %d, var %s' % (neuron_ind, t1, var_level))
        plt.subplot(1, 2, 2)
        plt.plot(t2_data)
        plt.xticks(np.arange(0, 64, 8), self.unique_categories, rotation=30);
        plt.title('Neuron %d, trial %d, var %s' % (neuron_ind, t2, var_level))

        #let's do a scatter plot of the responses to one trial vs the other
        plt.figure()
        plt.scatter(t1_data, t2_data)
        plt.xlabel('responses of neuron %d, trial %d, %s'% (neuron_ind, t1, var_level))
        plt.ylabel('responses of neuron %d, trial %d, %s'% (neuron_ind, t2, var_level))

        #how correlated are they exactly between trials? let's use pearson correlation
        rval = stats.pearsonr(t1_data, t2_data)[0]
        plt.title('Correlation for varlevel %s images = %.3f' % (var_level, rval))

        #in fact, let's have a look at the correlation for all pairs of trials 
        fig = plt.figure(figsize = (7, 7))
        #the numpy corrcoef function basically gets the pairwise pearson correlation efficiently
        corrs = np.corrcoef(neuron_i_data_by_trial.T)
        #now let's plot the matrix of correlations using the matshow function
        plt.colorbar(fig.gca().matshow(corrs))
        plt.xlabel('trials of neuron %d' % neuron_ind)
        plt.ylabel('trials of neuron %d' % neuron_ind)
        plt.title('Between-trial correlations for varlevel %s' % var_level)
예제 #2
0
def plot_learning_curve(X, y, maxdepth, plt):
    # create cv training and test scores for various training set sizes
    train_sizes, train_scores, test_scores = learning_curve(
        DecisionTreeClassifier(max_depth=maxdepth, random_state=42),
        X,  # feature matrix
        y,  # target vector
        cv=5,  # number of folds in cross-validation
        scoring="f1",  # metric
        #scoring="neg_mean_squared_error",  # metric
        n_jobs=-1,  # use all computer cores,
        train_sizes=np.linspace(0.01, 1.0,
                                30),  # 30 different sizes of the training set
    )
    # create means and standart deviations of training set scores
    train_mean = np.mean(train_scores, axis=1)
    train_std = np.std(train_scores, axis=1)

    # create means and standart deviations of test set scores
    test_mean = np.mean(test_scores, axis=1)
    test_std = np.std(test_scores, axis=1)

    # draw lines
    plt.plot(train_sizes,
             train_mean,
             "--",
             color="#111111",
             label="Training score")
    plt.plot(train_sizes,
             test_mean,
             color="#111111",
             label="Cross-validation score")

    # draw bands
    plt.fill_between(train_sizes,
                     train_mean - train_std,
                     train_mean + train_std,
                     color="#DDDDDD")
    plt.fill_between(train_sizes,
                     test_mean - test_std,
                     test_mean + test_std,
                     color="#f4d0d7")

    # create plot
    plt.title("Learning curve for Decision Tree")
    plt.xlabel("Training set size", fontsize=18)
    plt.ylabel("F1 score", fontsize=18)
    plt.legend(loc="best")
    plt.tight_layout()

    return
예제 #3
0
def pb1_1(test_x, test_t, train_x, train_t, theta_0, theta_1, theta_2,
          theta_3):
    C_N = [[kernel(x, y, theta_0, theta_1, theta_2, theta_3) for x in train_x]
           for y in train_x]
    for i in range(len(train_x)):
        C_N[i][i] = C_N[i][i] + 1
    K = [[
        kernel(x, y, theta_0, theta_1, theta_2, theta_3)
        for x in np.arange(0, 2, 0.01)
    ] for y in train_x]
    K_train = [[
        kernel(x, y, theta_0, theta_1, theta_2, theta_3) for x in train_x
    ] for y in train_x]
    K_test = [[
        kernel(x, y, theta_0, theta_1, theta_2, theta_3) for x in test_x
    ] for y in train_x]
    C_N_inv = np.linalg.inv(np.matrix(C_N))
    K = np.matrix(K)
    m = np.matmul(np.matmul(np.transpose(K), C_N_inv),
                  np.transpose(np.matrix(train_t)))
    m_train = np.matmul(np.matmul(np.transpose(K_train), C_N_inv),
                        np.transpose(np.matrix(train_t)))
    m_test = np.matmul(np.matmul(np.transpose(K_test), C_N_inv),
                       np.transpose(np.matrix(train_t)))
    A = np.matmul(np.matmul(np.transpose(K), C_N_inv), K)
    s = [
        kernel(x, x, theta_0, theta_1, theta_2, theta_3) + 1 -
        A[int(100 * x), int(100 * x)] for x in np.arange(0, 2, 0.01)
    ]
    # 將資料處理成 [200,] 畫圖比較不會有 dimension 的問題
    s = np.asarray(s)
    m = np.squeeze(np.asarray(m))
    m_train = np.squeeze(np.asarray(m_train))
    m_test = np.squeeze(np.asarray(m_test))
    plt.plot(np.arange(0, 2, 0.01), m, color='red')
    plt.fill_between(np.arange(0, 2, 0.01),
                     np.transpose(m) - s,
                     np.transpose(m) + s,
                     facecolor=(1, 0.5, 0.5, 0.5))
    plt.scatter(train_x, train_t, s=3, color='red')

    rms_train = sum((m_train - np.array(train_t))**2) / len(train_t)
    rms_test = sum((m_test - np.array(test_t))**2) / len(test_t)

    return {'train': rms_train, 'test': rms_test}
예제 #4
0
    def plot_trial_avg_data_with_stds(self, neuron_ind, var_level):

        neuron_i_data_by_trial = self.by_trial_IT_Neural_Data_objmeans_sorted_by_category[var_level][:, :, neuron_ind]
        neuron_i_data_trial_mean = neuron_i_data_by_trial.mean(1)
        neuron_i_data_trial_std = neuron_i_data_by_trial.std(1)
        minval = neuron_i_data_trial_mean.min()
        maxval = neuron_i_data_trial_mean.max()
        plt.plot(neuron_i_data_trial_mean)
        plt.fill_between(np.arange(64), minval, maxval, 
                         where=(np.arange(64) / 8) % 2, color='k', alpha=0.2)

        plt.fill_between(np.arange(64), 
                         neuron_i_data_trial_mean - neuron_i_data_trial_std,
                         neuron_i_data_trial_mean + neuron_i_data_trial_std,
                         color='b', alpha=0.2)

        plt.xticks(np.arange(0, 64, 8) + 4, self.unique_categories, rotation=30);
        plt.ylabel('Neural Responses')
        plt.ylim(minval, maxval)
        plt.title('Responses for neuron %d Variation %s images' % (neuron_ind, var_level))
        plt.xlim(0, 64)
oms_list = [[s[1] for s in samples] for samples in samples_list]

percentiles = [16, 50, 84]
colors = ["crimson", "royalblue", "lightslategrey", "seagreen", "black"]
nbins = 35
title = r"${{{0}}}_{{-{1}}}^{{+{2}}}$"
fmt = "{{0:{0}}}".format(".1f").format


# loop over the lenses, shaded histograms
for il, lens in enumerate(lenses):
    h, be = np.histogram(H0s_list[il], bins=nbins, density=True)
    xs = [(b+be[ind+1])/2. for ind, b in enumerate(be[:-1])]
    plt.plot(xs, h, alpha=0.5, color=colors[il], linewidth=0.0)
    plt.fill_between(
            xs, h, alpha=0.5, color=colors[il], linewidth=0.0,
            label=r'%s' % lens.longname)

    # add the values
    pcs = np.percentile(H0s_list[il], q=percentiles)
    txt = r'$H_{0}: $' + \
            title.format(fmt(pcs[1]), fmt(pcs[1]-pcs[0]), fmt(pcs[2]-pcs[1]))
    plt.annotate(
            txt, xy=(0.0, 0.0), xytext=(0.02, 0.9-0.1*il),
            xycoords='axes fraction', fontsize=18, color=colors[il])


# plot the combined result
h, be = np.histogram(H0s_list[-1], bins=nbins, density=True)
xs = [(b+be[ind+1])/2. for ind, b in enumerate(be[:-1])]
plt.plot(xs, h, alpha=1.0, color=colors[-1], linewidth=2.0, label=r'All')
예제 #6
0
average_precision = average_precision_score(test_label_trans, lp_probs_trans)

precision, recall, thresholds = precision_recall_curve(test_label_trans, lp_probs_trans)

pos_rate = sum(test_label_trans)/len(test_label_trans)

f=plt.figure()
matplotlib.rcParams.update({'font.size': 20})
# In matplotlib < 1.5, plt.fill_between does not have a 'step' argument
step_kwargs = ({'step': 'post'}
               if 'step' in signature(plt.fill_between).parameters
               else {})
plt.step(recall, precision, color='b', alpha=0.2,
         where='post', label='AP = %0.2f' % average_precision)
plt.fill_between(recall, precision, alpha=0.2, color='b', **step_kwargs)
plt.plot([0, 1], [pos_rate, pos_rate], 'r--')
plt.legend(loc='upper right')

plt.xlabel('Recall')
plt.ylabel('Precision')
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
# plt.title('PRC for skip-chain CRF with threads \n AP={0:0.2f}'.format(average_precision))
f.savefig("data_cmv/figures/exp-skip-thread-prc-large-6.pdf", bbox_inches='tight')

print(precision)
print(recall)


예제 #7
0
        color='r', lw=3, label='Decade Average')
plt.scatter(data.year, data.score, alpha=.04, lw=0, color='k')
plt.xlabel("Year")
plt.ylabel("Score")
plt.legend(frameon=False)
remove_border()
#Again, there were AttributeErrors when I tried to create graphs

grouped_scores = data.groupby(decade).score

mean = grouped_scores.mean()
std = grouped_scores.std()

plt.plot(decade_mean.index, decade_mean.values, 'o-',
        color='r', lw=3, label='Decade Average')
plt.fill_between(decade_mean.index, (decade_mean + std).values,
                 (decade_mean - std).values, color='r', alpha=.2)
plt.scatter(data.year, data.score, alpha=.04, lw=0, color='k')
plt.xlabel("Year")
plt.ylabel("Score")
plt.legend(frameon=False)
remove_border()
#Again, there were AttributeErrors when I tried to create graphs

for year, subset in data.groupby('year'):
    print year, subset[subset.score == subset.score.max()].title.values
    
fig, axes = plt.subplots(nrows=4, ncols=6, figsize=(12, 8), 
                         tight_layout=True)

bins = np.arange(1950, 2013, 3)
for ax, genre in zip(axes.ravel(), genres):
        df = clean_source(onlyfiles[i])
        df = df.fillna(method='ffill')
        merged = pd.concat([merged, df])
    merged = merged.groupby(pd.TimeGrouper(freq='1D')).mean()
    plt.figure(figsize=(12, 14))
    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.ylim(0.54, 0.85)
    plt.fill_between(merged.index,
                     merged.sentiment - merged['std'],
                     merged.sentiment + merged['std'],
                     color="#3F5D7D")
    plt.plot(merged.index, merged.sentiment, color="white", lw=2)

    prices = pd.read_csv('../twitter_stream/market/this.csv')
    prices.Date = pd.to_datetime(prices.Date)
    prices.index = prices.Date
    prices.drop(['Date', 'Open*', 'High', 'Low', 'Volume', 'Market Cap'],
                inplace=True,
                axis=1)
    prices = prices.Close.rolling(3, center=True)
    prices = pd.DataFrame({'Close': prices.mean()})
    prices = prices.fillna(method='ffill')
    axes2 = plt.twinx()
    axes2.bar(prices.index,
              prices['Close'],
예제 #9
0
def plot_scalar_mean(attribute):
    meanResponseTime = []
    meanResponseTimeLognormal = []
    index = []

    for i in interarrival_time:
        meanResponseTime.clear()
        index.clear()
        meanResponseTimeLognormal.clear()

        for m in monitoring_time:
            filename = './csv/' + 'Exponential-capacity-' + str(i) + "," + str(
                m) + '.csv'
            filename2 = './csv/' + 'Lognormal-capacity-' + str(i) + "," + str(
                m) + '.csv'

            index.append("m=" + str(m))

            with open(filename, 'r') as f:
                df = scalar_df_parse(filename)
                df = df[df.name == attribute]
                del df['run']
                meanResponseTime.append(df.value.mean())

            with open(filename2, 'r') as f:
                df = scalar_df_parse(filename2)
                df = df[df.name == attribute]
                del df['run']
                meanResponseTimeLognormal.append(df.value.mean())

        dataframe = pd.DataFrame()
        dataframe['file'] = index
        dataframe['meanResponseTimeExponential'] = meanResponseTime
        dataframe['meanResponseTimeLognormal'] = meanResponseTimeLognormal
        plt.plot(dataframe['meanResponseTimeExponential'],
                 "g:o",
                 label="exponential")
        plt.plot(dataframe['meanResponseTimeLognormal'],
                 "r:o",
                 label="lognormal")
        plt.fill_between(index,
                         dataframe['meanResponseTimeExponential'],
                         dataframe['meanResponseTimeLognormal'],
                         where=dataframe['meanResponseTimeExponential'] >
                         dataframe['meanResponseTimeLognormal'],
                         facecolor='green',
                         alpha=0.3)
        plt.fill_between(index,
                         dataframe['meanResponseTimeExponential'],
                         dataframe['meanResponseTimeLognormal'],
                         where=dataframe['meanResponseTimeExponential'] <=
                         dataframe['meanResponseTimeLognormal'],
                         facecolor='red',
                         alpha=0.3)
        plt.title('Response time for k=' + str(i) + "ms")
        plt.xticks(rotation=25)
        plt.xlabel("Value of m")
        plt.ylabel(attribute)

        plt.xticks([k for k in range(len(index))], [k for k in index])

        filename3 = './csv/' + 'Nonmonitoring-exponential-' + str(i) + '.csv'
        filename4 = './csv/' + 'Nonmonitoring-lognormal-' + str(i) + '.csv'

        with open(filename3, 'r') as f:
            df = scalar_df_parse(filename3)
            df = df[df.name == attribute]
            del df['run']
            plt.plot([df['value'].mean() for k in range(len(index))],
                     "b:v",
                     label="Non-monitoring exponential")

        with open(filename4, 'r') as f:
            df = scalar_df_parse(filename4)
            df = df[df.name == attribute]
            del df['run']
            plt.plot([df['value'].mean() for k in range(len(index))],
                     "y:v",
                     label="Non-monitoring lognormal")

        plt.legend(loc='upper left')
        plt.savefig(
            f'./analysis/immagini per clarissa/{attribute}/k={i}ms.png')
        plt.show()
예제 #10
0
import matplotlib as plt
import numpy as np

#I hold x a line while defining new values for each y
x = np.linspace(0, 20, 2000)
#1*x[1] + 0*x[2] <= 5
#y0*0=5-x  #No initialization with respect to y0 because it is zero.
#0*x[1] + 1*x[2] <= 5
y1 = 5 + x * 0
#1*x[1] + 0*x[2] >= 1
#y2*0=1-x  #No inititialization
#0*x[1] + 1*x[2] >= 1
y3 = 1 - x * 0
#1*x[1] + 1*x[2] <= 6
y4 = 6 - x

#TODO: HOW TO DRAW THE ABOVE LINEAR EQUATIONS ELEGANTLY in matplotlib?
#Drawing the lines by end points because of the zeroes.
plt.plot(x, y4, label=r'$x[1]+x[2]<=6$')
plt.plot([5, 5], [10, -10])  #x  <  5
plt.plot([10, -2], [5, 5])  #y2 <  5
plt.plot([1, 1], [10, -10], 'r-')  #x  >= 1
plt.plot([10, -2], [1, 1], 'b--')  #y3 >= 1
#TODO: how to fill the upper triangle only?
# http://benalexkeen.com/linear-programming-with-python-and-pulp-part-1/
plt.fill_between(x, y5, y6, where=y5 > y6, color='grey', alpha=0.5)
plt.show()