Example #1
0
 def plot(self, logscale=True, with_validation=True, xlim=None, ylim=None):
     """
     Plots the loss history.
     :param logscale: if True, logarithmic scale is used
     :param with_validation: if True, validation set loss is plotted
     :param ylim: limits of the y-axis
     """
     if 'figsize' in dir(plt):
         plt.figsize(10, 5)
     plt.hold(True)
     try:
         if logscale:
             plt.yscale('log')
             # plt.xscale('log')
         plt.plot(self.history[0], self.history[1], 'b')
         if with_validation:
             plt.plot(self.history[0], self.history[2], 'c')
         plt.plot(self.history[0], self.history[3], 'r')
     except ValueError:
         # catches: ValueError: Data has no positive values, and therefore can not be log-scaled.
         # when no data is present or we only have NaNs
         pass
     if xlim is not None:
         plt.xlim(xlim)
     if ylim is not None:
         plt.ylim(ylim)
     yl = plt.ylim()
     if with_validation and self.best_iter is not None:
         plt.vlines(self.best_iter, yl[0], yl[1])
     plt.xlabel('iteration')
     plt.ylabel('loss')
     if with_validation:
         plt.legend(['training', 'validation', 'test'])
     else:
         plt.legend(['training', 'test'])
Example #2
0
 def plot(self, logscale=True, with_validation=True, xlim=None, ylim=None):
     """
     Plots the loss history.
     :param logscale: if True, logarithmic scale is used
     :param with_validation: if True, validation set loss is plotted
     :param ylim: limits of the y-axis
     """
     if 'figsize' in dir(plt):
         plt.figsize(10, 5)
     plt.hold(True)
     try:
         if logscale:
             plt.yscale('log')
             # plt.xscale('log')
         plt.plot(self.history[0], self.history[1], 'b')
         if with_validation:
             plt.plot(self.history[0], self.history[2], 'c')
         plt.plot(self.history[0], self.history[3], 'r')
     except ValueError:
         # catches: ValueError: Data has no positive values, and therefore can not be log-scaled.
         # when no data is present or we only have NaNs
         pass
     if xlim is not None:
         plt.xlim(xlim)
     if ylim is not None:
         plt.ylim(ylim)
     yl = plt.ylim()
     if with_validation and self.best_iter is not None:
         plt.vlines(self.best_iter, yl[0], yl[1])
     plt.xlabel('iteration')
     plt.ylabel('loss')
     if with_validation:
         plt.legend(['training', 'validation', 'test'])
     else:
         plt.legend(['training', 'test'])
def plot_lcd_tf_subplots(sub_list, coa, max_x, lcd_y=0.15, tf_y = 300, min_lc= 0):
    from numpy import ceil
    import matplotlib.pyplot as plt
    cpool = ["#1F78B4", "#E31A1C"]
    lcd_list = cluster_lcd_list(sub_list, coa)
    tf_list = cluster_tf_list(sub_list, coa)
    cols = 3
    rows = ceil(len(lcd_list)/3.0)
    height = rows * 2.2
    width = cols * 4
    i = 1
    plt.figsize(width, height)
    while i <= len(lcd_list):
        lcd = lcd_list[i-1];
        tf = tf_list[i-1];
        lc_ax = plt.subplot(rows, cols, i);
        lc_ax.plot(lcd[0], lcd[1], color=cpool[1], lw=1);
        lc_ax.set_ylim(min_lc, lcd_y);
        lc_ax.text(max_x*0.9, lcd_y*0.85, '%d' % i, va='top', ha='right')
        tf_ax = lc_ax.twinx()
        tf_ax.plot(tf[0], tf[1], color=cpool[0], alpha=0.6)
        tf_ax.set_ylim(-100, tf_y)
        lc_ax.set_xlim(0, max_x);
        tf_ax.set_yticks([])
        i = i+1;
        
    plt.tight_layout(pad=2);
    plt.subplots_adjust(top=0.96);
    return plt.gcf();
Example #4
0
def plot_corr(df, size=11):
    #dataPlot = sns.load_dataset(df)
    cols = [
        'SalePrice', 'OverallQual', 'GrLivArea', 'GarageCars', 'TotalBsmtSF',
        'FullBath', 'YearBuilt'
    ]
    sns.pairplot(df[cols])
    plt.figsize(12, 12)
    plt.set_cmap('YlOrRd')
    #plt.subplots(figsize=(50,100))
    plt.show()
Example #5
0
def plotAIMgraphs():
    plt.figsize(40, 10)
    plt.rcParams.update({'font.size': 18})
    fig = plt.figure()
    patches_list = []
    label_list = []
    for (mousetype_i, mousetype) in enumerate(["CP101", "CP73"]):
        s = pd_covar.select(lambda x: pd_covar.ix[x, "MouseType"] == mousetype
                            and pd_covar.ix[x, "LesionType"] == "6-OHDA")
        ax1 = fig.add_subplot(1, 3, mousetype_i + 1)
        ax1.patch.set_facecolor((0.9, 0.9, 0.9))
        ax1.grid(which='both')
        plt.title(mousetype)
        plt.ylabel("AIM score")
        plt.xlabel("Day")
        plt.ylim((-1, 50))
        plt.xlim((-0.5, 4.5))
        drugtreat_group_color = {
            "Acute high levodopa": "green",
            "Chronic saline": "grey",
            "Acute saline": "yellow",
            "Chronic high levodopa": "red",
            "Chronic low levodopa": "blue"
        }
        pd_groups = s.groupby(["MouseType", "LesionType", "DrugTreat"])
        for (mouse, lesion, drug) in pd_groups.groups.keys():
            data = s.ix[
                pd_groups.groups[(mouse, lesion, drug)],
                ["Day1AIM", "Day3AIM", "Day4AIM", "Day5AIM", "Day8AIM"
                 ]].transpose()
            r = np.repeat(range(len(data.index)), len(data.columns)).reshape(
                (len(data.index), -1))
            #xs = [range(len(data))
            r = r + random.random(shape(r)) * 0.15 - 0.075
            ax1.plot(r,
                     data,
                     "o-",
                     color=drugtreat_group_color[drug],
                     lw=1.5,
                     ms=8,
                     alpha=0.5,
                     label=lesion + " " + drug)
            plt.xticks(range(0, 5), ["1", "3", "4", "5", "8"])
            patches, labels = ax1.get_legend_handles_labels()
            patches_list.append(patches[-1])
            label_list.append(labels[-1])
    ax_l = plt.subplot(1, 3, 3)
    ax_l.set_frame_on(False)
    ax_l.set_xticks([])
    ax_l.set_yticks([])
    plt.legend(patches_list[1:4], label_list[1:4], loc="upper left")
def plot_subplots(data_list, id_list, max_x):
    from numpy import ceil
    import matplotlib.pyplot as plt
    cols = 4
    rows = ceil(len(data_list)/4.0)
    height = rows * 2.2
    width = cols * 3
    i = 1
    plt.figsize(width, height)
    while i <= len(data_list):
        data = data_list[i-1];
        plt.subplot(rows, cols, i);
        plt.plot(data[0], data[1]);
        plt.xlim(0, max_x);
        i = i+1;
    plt.tight_layout(pad=2);
    plt.subplots_adjust(top=0.96);
    return plt.gcf();
Example #7
0
def pretty_plot(x, y, xlabel='', ylabel='', title='', xticks=None):
    '''
    Pretty plot a list.

    scatter_fn -- function to select a single point to highlight
        e.g., `scatter_fn=np.max` will highlight the max point of a curve

    '''
    plt.figsize(24, 18)
    plt.grid(b=True, which='major', linestyle='--')
    plt.plot(x, y, color="#348ABD", lw=3)
    plt.fill_between(x, y, alpha=.2, facecolor=["#348ABD"])
    plt.scatter(np.argmax(y), np.max(y), s=140, c="#348ABD")
    plt.xlim(np.min(x) - .5, np.max(x) + .5)
    plt.ylim(np.min(y) - .5, max(y) + .5)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    if xticks:
        plt.xticks(x, xticks, rotation=90)
    plt.title(title)
Example #8
0
def pretty_plot(x, y, xlabel='', ylabel='', title='', xticks=None):
    '''
    Pretty plot a list.

    scatter_fn -- function to select a single point to highlight
        e.g., `scatter_fn=np.max` will highlight the max point of a curve

    '''
    plt.figsize( 24, 18 )
    plt.grid(b=True, which='major', linestyle='--')
    plt.plot( x, y, color = "#348ABD", lw = 3 )
    plt.fill_between( x, y, alpha = .2, facecolor = ["#348ABD"])
    plt.scatter( np.argmax(y), np.max(y), s = 140, c ="#348ABD"  )
    plt.xlim(np.min(x)-.5, np.max(x)+.5)
    plt.ylim(np.min(y)-.5, max(y)+.5)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    if xticks:
        plt.xticks(x, xticks, rotation=90)
    plt.title(title);
def plot_lcd_subplots(data_list, coa, max_x=100, max_y=0.2):
    from numpy import ceil
    import matplotlib.pyplot as plt
    data_list = cluster_lcd_list(data_list, coa)
    cols = 4
    rows = ceil(len(data_list)/4.0)
    height = rows * 2.2
    width = cols * 3
    i = 1
    plt.figsize(width, height)
    while i <= len(data_list):
        data = data_list[i-1];
        plt.subplot(rows, cols, i);
        plt.plot(data[0], data[1]);
        plt.xlim(0, max_x);
        plt.ylim(0, max_y);
        plt.text(max_x/1.2, max_y/1.2, '%d' % i)
        i = i+1;
    plt.tight_layout(pad=2);
    plt.subplots_adjust(top=0.96);
    return plt.gcf();
Example #10
0
def plotAIMgraphs():    
    plt.figsize(40, 10)
    plt.rcParams.update({'font.size': 18})
    fig = plt.figure()
    patches_list = []
    label_list = []
    for (mousetype_i, mousetype) in enumerate(["CP101", "CP73"]):
        s = pd_covar.select(lambda x: pd_covar.ix[x, "MouseType"] == mousetype and pd_covar.ix[x, "LesionType"] == "6-OHDA") 
        ax1 = fig.add_subplot(1, 3, mousetype_i + 1)
        ax1.patch.set_facecolor((0.9, 0.9, 0.9))
        ax1.grid(which='both')
        plt.title(mousetype)
        plt.ylabel("AIM score")
        plt.xlabel("Day")
        plt.ylim( (-1, 50) )
        plt.xlim( (-0.5, 4.5) )
        drugtreat_group_color = { "Acute high levodopa" : "green", 
                                  "Chronic saline" : "grey", 
                                  "Acute saline" : "yellow", 
                                  "Chronic high levodopa" : "red", 
                                  "Chronic low levodopa" : "blue"  }
        pd_groups = s.groupby(["MouseType", "LesionType", "DrugTreat"])
        for (mouse, lesion, drug) in pd_groups.groups.keys():
            data = s.ix[pd_groups.groups[(mouse, lesion, drug)],
                        ["Day1AIM", "Day3AIM", "Day4AIM", "Day5AIM", "Day8AIM"]].transpose()
            r = np.repeat(range(len(data.index)), len(data.columns)).reshape( (len(data.index), -1) )
            #xs = [range(len(data))
            r = r + random.random(shape(r)) * 0.15 - 0.075
            ax1.plot(r, data, "o-", color=drugtreat_group_color[drug], lw=1.5, ms=8, alpha=0.5, label=lesion + " " + drug )
            plt.xticks(range(0, 5), ["1", "3", "4", "5", "8"])
            patches, labels = ax1.get_legend_handles_labels()
            patches_list.append(patches[-1])
            label_list.append(labels[-1])
    ax_l = plt.subplot(1,3,3)
    ax_l.set_frame_on(False)
    ax_l.set_xticks([])
    ax_l.set_yticks([])
    plt.legend(patches_list[1:4], label_list[1:4], loc="upper left" )
def plot_av_tf_subplots(sub_list, coa, max_x, lcd_y=0.15, tf_y = 300):
    from numpy import ceil
    import matplotlib.pyplot as plt
    cpool = ["#1F78B4", "#E31A1C"]
    lcd_list = cluster_lcd_list(sub_list, coa)
    tf_list = cluster_tf_list(sub_list, coa)
    cols = 3
    rows = ceil(len(lcd_list)/3.0)
    height = rows * 2.2
    width = cols * 4
    i = 1
    plt.figsize(width, height)
    while i <= len(lcd_list):
        lcd = lcd_list[i-1];
        tf = tf_list[i-1];
        ax = plt.subplot(rows, cols, i);
        plt.ylim(-100, lcd_y);
        plt.xlim(0, max_x)
        plt.text(max_x/1.2, lcd_y/1.2, '%d' % i)
        plt.plot(tf[0], tf[1], color=cpool[0], alpha=0.6)
        plt.plot(lcd[0], lcd[1], color=cpool[1], lw=1);
        i = i+1;
    plt.tight_layout(pad=2);
Example #12
0
File: util.py Project: surban/ml
    def plot(self, final=True, logscale=True):
        self.end_time = time.time()

        if 'figsize' in dir(plt):
            plt.figsize(10,5)
        # plt.clf()
        plt.hold(True)
        if logscale:
            plt.yscale('log')
            #plt.xscale('log')
        plt.plot(self.history[0], self.history[1], 'b')
        plt.plot(self.history[0], self.history[2], 'c')
        plt.plot(self.history[0], self.history[3], 'r')
        yl = plt.ylim()
        if self.best_iter is not None:
            plt.vlines(self.best_iter, yl[0], yl[1])
        plt.xlabel('iteration')
        plt.ylabel('loss')
        plt.legend(['training', 'validation', 'test'])

        if final and self.best_iter is not None:
            print "best iteration: %5d  best validation test loss: %9.5f  best test loss: %9.5f" % \
                (self.best_iter, self.best_val_loss, self.best_tst_loss)
            print "training took %.2f s" % (self.end_time - self.start_time)
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np

nx=41
ny=41
dataU=np.genfromtxt("solBurgersU.dat",delimiter=",")
dataV=np.genfromtxt("solBurgersV.dat",delimiter=",")
dataU=np.delete(dataU,-1,1)
dataV=np.delete(dataV,-1,1)
x=np.linspace(0,2,nx)
y=np.linspace(0,2,nx)
X,Y=np.meshgrid(x,y)
contador=0
print type(dataU)
for u,v in zip(dataU,dataV):
    a=u.reshape(nx,ny)
    fig=plt.figure(plt.figsize(10,10))
    ax=fig.gca(projection='3d')
    wire1=ax.plot_wireframe(X,Y,a[:], cmap=cm.coolwarm)
    plt.savefig("iteracion" + str(contador) + ".png")
    contador+=1

Example #14
0
    print(np.sum(occurrences))
    # Occurrences.mean is equal to n/N.
    print("What is the observed frequency in Group A? %.4f" % np.mean(occurrences))
    print("Does this equal the true frequency? %s" % (np.mean(occurrences) == p_true))
    
    #include the observations, which are Bernoulli
    with model:
        obs = pm.Bernoulli("obs", p, observed=occurrences)
        # To be explained in chapter 3
        step = pm.Metropolis()
        #trace = pm.sample(18000, step=step)
        trace = pm.sample(6000, step=step, tune=1000, cores=1)
        burned_trace = trace[1000:]
        
    # -- Plot posterior of unknown p_A: --
    plt.figsize(12.5, 4)
    plt.title("Posterior distribution of $p_A$, the true effectiveness of site A")
    plt.vlines(p_true, 0, 90, linestyle="--", label="true $p_A$ (unknown)")
    plt.hist(burned_trace["p"], bins=25, histtype="stepfilled", normed=True)
    plt.legend();

if 2 in run_models:
    # These two quantities are unknown to us.
    true_p_A = 0.05
    true_p_B = 0.04
    
    # Notice the unequal sample sizes -- no problem in Bayesian analysis.
    N_A = 1500
    N_B = 750
    
    # Generate some observations..
Example #15
0
print cm
'''
[[88  3]
 [ 1 48]]'''

# To check or predict a perticular values from dataset or by our own features

example_measures = np.array([[4, 2, 1, 1, 1, 2, 3, 2, 1],
                             [4, 2, 1, 2, 2, 2, 3, 2, 1]])
example_measures = example_measures.reshape(len(example_measures), -1)

prediction = clf.predict(example_measures)
print prediction  # [2 2]

# to print Confusion matrix

import seaborn as sns
plt.figsize(figsize=(9, 9))
sns.heatmap(cm,
            annot=True,
            fmt=".3f",
            linewidths=.5,
            square=True,
            cmap='Blues_r')
plt.xlabel('Predicted label')
plt.ylabel('True label')
title = 'Accuracy Score : {0}'.format(accuracy)
plt.title(title, size=14)
plt.savefig('CM_After_prediction')
plt.show()
# We average out the expected outcomes of 1000 hands played 100 times to get a good approximation of expected utility

# <codecell>

l = 0
for i in list1:
  l = l + i/1000.
print l/100.

# <markdowncell>

# Plot the variance. See that the mean is somewhere around -25 but it varies from one set of 1000  hands to another.

# <codecell>

plt.figsize(6,4)
plt.figure(1)
plt.hist(list1, color='k', alpha = .15)
plt.show()

# <codecell>

print '-------'
print '''Summary stats for Player 1 (never bust)'''
print '-------'
Size,  Range, Mean, variance, skewness, kurtosis = describe(list1)
print 'Number of observations: ', Size
print 'Mean: ',  Mean
print 'Min to Max: ', Range
print 'Variance: ', variance
print 'Standard Dev.: ', sqrt(variance)
Example #17
0
svm_clf.fit_transform(X, y).shape
svm_clf.fit(X, y)

scores = cross_val_score(svm_clf, X, y, cv=5)
print(scores)
param_grid = {'C': 10.**np.arange(-3, 4)}
grid_search = GridSearchCV(svm_clf, param_grid=param_grid, cv=5, verbose=3)
grid_search.fit(X, y)

print(grid_search.best_params_)
print(grid_search.best_score_)

for c in grid_search.grid_scores_:
    print(c.mean_validation_score)

plt.figsize(12, 6)
plt.plot([c.mean_validation_score for c in grid_search.grid_scores_],
         label="validation accuracy")
plt.xticks(np.arange(6), param_grid['C'])
plt.xlabel("C")
plt.ylabel("Accuracy")
plt.legend(loc='best')
plt.show()

lr_clf = LogisticRegression(C=0.1, penalty='l1', dual=False)
lr_clf.fit(X, y)

scores = cross_val_score(lr_clf, X, y, cv=5)
print(scores)
param_grid = {'C': 10.**np.arange(-3, 4)}
grid_search = GridSearchCV(lr_clf, param_grid=param_grid, cv=5, verbose=3)