示例#1
0
def plot2D(x,y,x_ex,y_ex,ylabl):

    #static variable counter
    plot2D.fig_num += 1

    plt.subplot(2,2,plot2D.fig_num)
    plt.xlabel('$x$ (cm)')
    plt.ylabel(ylabl)
    plt.plot(x,y,"b+-",label="Lagrangian")
    plt.plot(x_ex,y_ex,"r--",label="Exact")
    plt.savefig("var_"+str(plot2D.fig_num)+".pdf")
示例#2
0
def generate_plot(array, vmin, vmax, figNumber=1):
    plt.figure(figNumber)
    plt.subplot(2,3,i)
    print i
    plt.imshow(array, vmin = vmin, vmax= vmax, interpolation = None) 
    plt.xlabel('Sample')
    plt.ylabel('Line')
    plt.title(row[0])
    cb = plt.colorbar(orientation='hor', spacing='prop',ticks = [vmin,vmax],format = '%.2f')
    cb.set_label('Reflectance / cos({0:.2f})'.format(incAnglerad*180.0/math.pi))
    plt.grid(True)
示例#3
0
def log_transform(data, columns, plot=True, figsize=(12, 6)):
    '''
    Nomralizes the dataset to be as close to the gaussian distribution.

    Parameter:
    -----------------------------------------
    data: DataFrame, Series.
        Data to Log transform.

    columns: List, Series
        Columns to be transformed to normality using log transformation
    
    plot: bool, default True
        Plots a before and after log transformation plot
    
    Returns:
        Log-transformed dataframe
    '''

    if data is None:
        raise ValueError(
            "data: Expecting a DataFrame/ numpy2d array, got 'None'")

    if columns is None:
        raise ValueError(
            "columns: Expecting at least a column in the list of columns but got 'None'"
        )

    df = data.copy()
    for col in columns:
        df[col] = np.log1p(df[col])

    if plot:
        for col in columns:
            _ = plt.figure(figsize=figsize)
            plt.subplot(1, 2, 1)
            sns.distplot(data[col],
                         color="m",
                         label="Skewness : %.2f" % (df[col].skew()))
            plt.title('Distribution of ' + col + " before Log transformation")
            plt.legend(loc='best')

            plt.subplot(1, 2, 2)
            sns.distplot(df[col],
                         color="m",
                         label="Skewness : %.2f" % (df[col].skew()))
            plt.title('Distribution of ' + col + " after Log transformation")
            plt.legend(loc='best')
            plt.tight_layout(2)
            plt.show()

    return df
示例#4
0
def plot2D(x, y, ylabl, x_ex=None, y_ex=None):

    #static variable counter
    plot2D.fig_num += 1

    plt.subplot(2, 2, plot2D.fig_num)
    plt.xlabel('$x$ (cm)')
    plt.ylabel(ylabl)
    plt.plot(x, y, "b+-", label="Numerical")
    if (x_ex != None):
        plt.plot(x_ex, y_ex, "r-x", label="Exact")

    plt.savefig("var_" + str(plot2D.fig_num) + ".pdf")
示例#5
0
def plot2D(x,y,ylabl,x_ex=None,y_ex=None):

    #static variable counter
    plot2D.fig_num += 1

    plt.subplot(2,2,plot2D.fig_num)
    plt.xlabel('$x$ (cm)')
    plt.ylabel(ylabl)
    plt.plot(x,y,"b+-",label="Numerical")
    if (x_ex != None):
        plt.plot(x_ex,y_ex,"r-x",label="Exact")

    plt.savefig("var_"+str(plot2D.fig_num)+".pdf")
def histogram_rgb():
    img = cv2.imread("2.jpeg", )
    cv2.imshow("image", img)
    color = ('r', 'g', 'b')
    for channel, c in enumerate(color):
        hist = cv2.calcHist([img], [channel], None, [256], [0, 256])
        plt.subplot(1, 3, channel + 1)
        plt.title('Channel {}, color {} '.format(channel, c))
        plt.plot(hist, color=c)
        plt.xlim([0, 256])

    plt.show()
    key = cv2.waitKey(0)
示例#7
0
def plot_food_sample_predictions(testX,
                                 predictions_array,
                                 testY,
                                 number_of_classes=3,
                                 num_rows=10,
                                 num_cols=4,
                                 width=None,
                                 height=None,
                                 is_random=True):
    """
        this method plots a sample of predictions from the test dataset and highlight correct and wrong predictions
        testX: this is the test dataset
        image_index: index of the image that we will plot from the test dataset
        predictions_array: it is the array that contains all the predictions of the test dataset as output of model.predict(testX)
        true_binary_labels: these are the true labels array expressed as INTEGER values. It does not work with hot-encoding and string labels. 
    """
    num_images = num_rows * num_cols
    true_binary_labels = np.argmax(testY, axis=1)
    if (num_images > testX.shape[0]):
        raise Exception(
            "num_rows*num_cols is", (num_rows * num_cols),
            "must be smaller than number of images in the Test Dataset",
            testX.shape[0])

    if width is None:
        width = 6 * num_cols

    if height is None:
        height = 2 * num_rows

    plt.figure(figsize=(width, height))
    plt.style.use(['seaborn-bright'])

    image_index = -1
    for i in range(num_images):
        if (is_random == True):
            image_index = randint(0, testX.shape[0] - 1)
        else:
            image_index = image_index + 1

        #print(image_index)
        #---------------
        plt.subplot(num_rows, 2 * num_cols, 2 * i + 1)
        plot_test_image(testX, image_index, predictions_array[image_index],
                        true_binary_labels)
        #---------------
        plt.subplot(num_rows, 2 * num_cols, 2 * i + 2)
        plot_value_array(image_index, predictions_array[image_index],
                         true_binary_labels, number_of_classes)
    plt.tight_layout()
    plt.show()
def main():

    #DEFINITON OF THE PATHS TO THE FILES WITH THE CONTENT
    path_to_train_accuracy = 'accuracy_train_data.csv'
    path_to_train_loss = 'loss_train_data.csv'
    path_to_validation_accuracy = 'accuracy_validation_data.csv'
    path_to_validation_loss = '"loss_validation_data.csv"'

    # CREATE LIST OF NUMBER OF EPOCHS COMPUTED
    eval_indices = range(1, EPOCHS + 1)

    #LOADS THE DATA FROM THE FILES
    accuracy_train, loss_train, accuracy_validation, loss_validation = read_data(path_to_train_accuracy,path_to_train_loss,
                                                                                 path_to_validation_accuracy,path_to_validation_loss)

    #SHOW THE INFORMATION FOR CONTROL OF QUALITY
    print(eval_indices)
    print("Accuracy Train: ",accuracy_train)
    print("Loss Train: " ,loss_train)
    print("Accuracy Validation: ", accuracy_validation)
    print("Loss validation: ", loss_validation)

    # DRAW THE ACCURACY GRAPH FOR VALIDATION AND TRAIN
    plt.clf()
    plt.subplot(211)
    plt.plot(eval_indices, accuracy_train, 'k--', label='TREINO')
    plt.plot(eval_indices, accuracy_validation, 'g-x', label='VALIDAÇÃO')
    plt.legend(loc='upper right')
    plt.xlabel('Épocas')
    plt.ylabel('ACERTO')
    plt.grid(which='major', axis='both')

    # DRAW THE LOSS GRAPH FOR VALIDATION AND TRAIN
    plt.subplot(212)
    # plt.plot(eval_indices, train, 'g-x', label='Train Set Accuracy')
    plt.plot(eval_indices, loss_train, 'r-x', label='TREINO')
    # plt.plot(eval_indices, np.ones(len(eval_indices))/TOT_CLASSES, 'k--')
    plt.plot(eval_indices, loss_validation, 'k--', label='VALIDAÇÃO')
    plt.legend(loc="upper right")
    plt.xlabel('Épocas')
    plt.ylabel('ERRO')
    plt.ylim(0, 1)
    plt.grid(which='both', axis='y')

    plt.subplots_adjust(left=0.2, wspace=0.2, hspace=0.3)

    plt.show()
    plt.pause(0.01)

    #SAVES BOTH OF THE GRAPHICS IN ONE FILE NAMED "Learning.png"
    plt.savefig('Learning.png')
示例#9
0
def display_image(image_data):
  # show the image
  mpplt.figure(figsize=(16,16))
  for i in range(3):
    if(i == 0):
      img_string = 'source'
    elif(i == 1):
      img_string = 'mask'
    else:
      img_string = 'target'
    img = image_data[img_string]
    mpplt.subplot(1,3,i+1)
    mpplt.imshow(img[:,:,[2,1,0]])
    
示例#10
0
    def plot_colocation(self):

        ind = np.random.randint(self.x.shape[0])

        f = plt.figure(figsize=(16, 4))
        gs = GridSpec(1, 4)

        for i, j in enumerate([5, 9, 12]):
            ax = plt.subplot(gs[i])
            ax.pcolormesh(self.x[ind, :, :, j])

        ax = plt.subplot(gs[-1])
        ax.pcolormesh(self.y[ind, :, :])
        return f
def get_trajectories_for_DF(DF):
    GetXYS=ct.get_xys(DF)
    xys_s=ct.get_xys_s(GetXYS['xys'],GetXYS['Nmin'])
    plt.figure(figsize=(5, 5),frameon=False)
    for m in list(range(9)):
        plt.plot()
        plt.subplot(3,3,m+1)
        xys_s_x_n=xys_s[m]['X']-min(xys_s[m]['X'])
        xys_s_y_n=xys_s[m]['Y']-min(xys_s[m]['Y'])
        plt.plot(xys_s_x_n,xys_s_y_n)
        plt.axis('off')
        axes = plt.gca()
        axes.set_ylim([0,125])
        axes.set_xlim([0,125])
def iPlotPredictions(yActualTrain, yActualVal, yPredTrain, yPredVal):
  ''' Plot both train and validation predictions '''
  #  plt.figure(figsize=(18, 12))
  plt.figure()
  plt.subplot(2, 2, 1)
  plt.scatter(yActualTrain[:,0], yPredTrain[:,0], s=1)
  plt.xlabel("Actual Values")
  plt.ylabel("Predicted Values")
  plt.title("In sample")
  plt.subplot(2, 2, 2)
  plt.scatter(yActualVal[:,0],yPredVal[:,0], s=1)
  plt.xlabel("Actual Values")
  plt.ylabel("Predicted Values")
  plt.title("Out of sample")
  plt.subplot(2, 2, 3)
  plt.scatter(yActualTrain[:,1], yPredTrain[:,1], s=1)
  plt.xlabel("Actual Values")
  plt.ylabel("Predicted Values")
  plt.title("In sample")
  plt.subplot(2, 2, 4)
  plt.scatter(yActualVal[:,1],yPredVal[:,1], s=1)
  plt.xlabel("Actual Values")
  plt.ylabel("Predicted Values")
  plt.title("Out of sample")
  plt.show()
示例#13
0
def display_image(originalImage, imageWithPath, algorithmType):
    """
    Display the original image and the image with path side by side
    :param originalImage: The original image with pixel densities
    :param imageWithPath: The image with the best path using the specified method
    :param algorithmType: The selected algorithm type
    """
    plt.subplot(1, 2, 1)
    plt.gca().set_title('Original')
    plt.imshow(originalImage)
    plt.subplot(1, 2, 2)
    plt.gca().set_title("Best Path: " + algorithmType.value)
    plt.imshow(imageWithPath)
    plt.show()
def DrawResults(results,name="Model"):
    plt.figure(figsize=(12,6))
    plt.subplot(121)
    plt.plot(range(epochs),results[2])
    plt.title("Model : "+name+"\n Training loss among epochs")
    plt.subplot(122)
    plt.plot(range(epochs),results[3])
    plt.title("Model : "+name+"\n Validation accuracy among epochs \n Global test accuracy : "+str(results[0]))
    
    r_mapping={0:"Democrat",1:"Republican"}

    results[1].index=results[1].index.map(r_mapping)
    results[1]["Accuracy"]=results[1][True]/(results[1][True]+results[1][False])
    print(results[1])
示例#15
0
def _show_training_data(positions, rewards):
    """
    plot the training data
    """
    positions[0] = [0, -0.4]
    plt.figure(1, figsize=[10, 5])
    plt.subplot(211)
    plt.plot(positions[:, 0], positions[:, 1])
    plt.xlabel('Episode')
    plt.ylabel('Furthest Position')
    plt.subplot(212)
    plt.plot(rewards)
    plt.xlabel('Episode')
    plt.ylabel('Reward')
    plt.show()
示例#16
0
def plot_samples(XList, TList, width=28, ncol=4, fName=None):
    fig = plt.figure()
    num_samples = XList.shape[0]
    for n, x in enumerate(XList):
        sample = x.reshape(width, width)
        plt.subplot((num_samples + ncol - 1) / ncol, ncol, n + 1)
        plt.imshow(sample, interpolation='None', cmap='gray')
        plt.title("{}".format(TList[n]))
        plt.xticks([])
        plt.yticks([])
    plt.tight_layout(pad=1, w_pad=0.2, h_pad=0.2)
    fig.set_size_inches(6, 6)
    if fName != None:
        savefig(fName, bbox_inches='tight')
    plt.show()
示例#17
0
def plotROC(predStrengths, classLabels):
    predStrengths = np.array(predStrengths)

    cur = (1.0, 1.0)  # cursor
    ySum = 0.0  # variable to calculate AUC
    numPosClas = sum(np.array(classLabels) == 1.0)
    yStep = 1 / float(numPosClas);
    xStep = 1 / float(len(classLabels) - numPosClas)
    sortedIndicies = np.argsort(predStrengths, axis=0)  # get sorted index, it's reverse

    fig = plt.figure()
    fig.clf()
    ax = plt.subplot(111)
    # loop through all the values, drawing a line segment at each point

    for index in sortedIndicies.tolist():
        if classLabels[index[0]] == 1.0:
            delX = 0;
            delY = yStep;
        else:
            delX = xStep;
            delY = 0;
            ySum += cur[1]
        # draw line from cur to (cur[0]-delX,cur[1]-delY)
        ax.plot([cur[0], cur[0] - delX], [cur[1], cur[1] - delY], c='b')
        cur = (cur[0] - delX, cur[1] - delY)
    ax.plot([0, 1], [0, 1], 'b--')
    plt.xlabel('False positive rate');
    plt.ylabel('True positive rate')
    plt.title('ROC curve')
    ax.axis([0, 1, 0, 1])
    plt.show()
    print("the Area Under the Curve is: ", ySum * xStep)
示例#18
0
def plot_sample_from_dataset(images,
                             labels,
                             rows=5,
                             colums=5,
                             width=8,
                             height=8):

    plt.figure(figsize=(width, height))
    for i in range(rows * colums):
        plt.subplot(rows, colums, i + 1)
        plt.xticks([])
        plt.yticks([])
        plt.grid(False)
        plt.imshow(images[i], cmap=plt.cm.binary)
        plt.xlabel(labels[i])
    plt.show()
示例#19
0
    def forward(self, x):
        # polar plot

        dic = creatRealDictionary(self.T, self.rr, self.theta, self.gid)
        sparsecode = fista(dic, x, self.lam, 100, self.gid)
        if random.randint(1, 20) == 1:
            plt.figure()
            plt.pcolormesh(sparsecode[0, :, :].data.cpu().detach().numpy(),
                           cmap='RdBu')
            plt.colorbar()
            plt.savefig('C_evolution_subsIni.png')  #, dpi=200
            # plt.show()
            plt.close()

            rr = self.rr.data.cpu().detach().numpy()
            theta = self.theta.data.cpu().detach().numpy()
            ax = plt.subplot(1, 1, 1, projection='polar')
            ax.scatter(0, 1, c='black')
            # unactive poles
            ax.scatter(theta, rr)
            ax.scatter(-theta, rr)
            ax.scatter(np.pi - theta, rr)
            ax.scatter(theta - np.pi, rr)
            #
            ax.set_rmax(1.2)
            ax.set_title("Dictionary", va='bottom')
            plt.savefig('usedPolesDCT.png')
            # plt.show()
            plt.close()

        return Variable(sparsecode)
def showpaint():
    data = pd.read_csv(
        r'C:\Users\ThinkPad\PycharmProjects\dataScience\output.csv',
        encoding='GBK')
    dic = dict()
    for i in data.values:
        dic[i[0][6:]] = i[1:5]
    list = [0, 0, 0, 0]
    for i in dic:
        for j in range(0, 4):
            list[j] += float(dic[i][j])
    print(list)
    averagestrength = sum(list) / (4 * len(dic))
    lst = [averagestrength / (i / len(dic)) for i in list]
    x = dic.keys()
    font = FontProperties(fname=r'C:\Windows\Fonts\simsun.ttc')
    y1 = [dic[i][0] for i in x]
    y2 = [dic[i][1] for i in x]
    y3 = [dic[i][2] for i in x]
    y4 = [dic[i][3] for i in x]
    x = [i for i in x]
    ax = plt.subplot(111)
    ax.plot(
        x,
        y1,
        label='积极,有信心,充满希望',
    )
    ax.plot(x, y2, label='担忧,紧张,质疑')
    ax.plot(x, y3, label='不松懈,对已取得的成功不放松警惕')
    ax.plot(x, y4, label='感激,祝福,加油,支持')
    ax.legend(prop=font)
    plt.title('疫情下的大众心态变化', fontproperties=font)
    plt.xlabel('时间', fontproperties=font)
    plt.ylabel('强度', fontproperties=font)
    plt.show()
示例#21
0
 def plot_x_rec(self, x_rec_list):
     # Plot all reconstructed samples along epochs
     # input: x_rec_list size=(num_sampling, num_img, rows, cols, dim).
     cur_path = os.getcwd()
     if not os.path.exists('{0}/Images'.format(cur_path)):
         os.makedirs('Images')
     plt.figure(figsize=(5,5))
     for s in range(len(x_rec_list)):
         for i in range(x_rec_list[s].shape[0]):
             plt.subplot(1, x_rec_list[s].shape[0], i+1)
             x_rec_s_i = np.squeeze(x_rec_list[s][i], axis=2)
             plt.imshow(x_rec_s_i, interpolation='nearest', cmap='gray')
             plt.axis('off')
         plt.tight_layout()
         plt.savefig("Images/Image_{0}.png".format(s))
         plt.close()
示例#22
0
def plotHistory(history):
    plt.subplot(1, 2, 1)
    plt.plot(history.history['acc'])
    plt.plot(history.history['val_acc'])
    plt.title('model accuracy')
    plt.ylabel('accuracy')
    plt.xlabel('epoch')
    plt.legend(['train', 'test'], loc='upper left')
    # summarize history for loss
    plt.subplot(1, 2, 2)
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('model loss')
    plt.ylabel('loss')
    plt.xlabel('epoch')
    plt.legend(['train', 'test'], loc='upper left')
    plt.show()
示例#23
0
def tresholds():
    img = cv2.imread('./data/frame500.jpg', 0)
    ret, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
    ret, thresh2 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
    ret, thresh3 = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)
    ret, thresh4 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)
    ret, thresh5 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)

    titles = ['Original Image', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV']
    images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]

    for i in range(6):
        plt.subplot(2, 3, i + 1), plt.imshow(images[i], 'gray')
        plt.title(titles[i])
        plt.xticks([]), plt.yticks([])

    plt.show()
示例#24
0
def imgOut(epoch, generator):
    rand_nums = np.random.randint(0, x_test_hr.shape[0], size=1)
    image_batch_hr = denormalize(x_test_hr[rand_nums])
    image_batch_lr = x_test_lr[rand_nums]
    gen_img = generator.predict(image_batch_lr)
    generated_image = denormalize(gen_img)
    image_batch_lr = denormalize(image_batch_lr)
    image_batch_bc = upsample(image_batch_lr, 4)

    plt.figure(figsize=(15, 5))

    dim = (1, 4)  # 4 wide

    # lowres
    plt.subplot(dim[0], dim[1], 1)
    plt.title('Low Res')
    plt.imshow(image_batch_lr[0], interpolation='nearest')
    plt.axis('off')

    # naive resize
    plt.subplot(dim[0], dim[1], 2)
    plt.title('Naive Resize')
    plt.imshow(image_batch_bc[0], interpolation='nearest')
    plt.axis('off')

    # generated
    plt.subplot(dim[0], dim[1], 3)
    plt.title('Generated SR')
    plt.imshow(generated_image[0], interpolation='nearest')
    plt.axis('off')

    # truth
    plt.subplot(dim[0], dim[1], 4)
    plt.title('Truth')
    plt.imshow(image_batch_hr[0], interpolation='nearest')
    plt.axis('off')

    # save file
    plt.tight_layout()
    plt.savefig(imgSav % epoch)
    plt.close('all')

    # learning gif images
    me_dir = os.path.join('C:\\', 'Users', 'cruze', 'Documents', 'CS767',
                          'finalProj--')
    me_path = os.path.join(me_dir, 'lowresme.jpg')
    me = Image.open(me_path)
    me = np.expand_dims(me, axis=0)
    me_lr = np.array(me)
    me_lr_norm = normalize(me_lr)
    gen_img = generator.predict(me_lr_norm)
    generated_image = denormalize(gen_img)
    plt.imshow(generated_image[0])
    plt.savefig(imgMeSav % epoch)
    plt.close('all')
示例#25
0
def display_NMNIST(images,labels):
    # converting from NX28X28 array into NX784 array
    flatimages = list()
    for i in images:
        flatimages.append(i.ravel())
    X = asarray(flatimages)
    T=labels
    vectortoimg(X[1])
    
    print("Checking multiple training vectors by plotting images.\nBe patient:")
    plt.close('all')
    fig = plt.figure()
    nrows=8
    ncols=8
    for row in range(nrows):
        for col in range(ncols):
            plt.subplot(nrows, ncols, row*ncols+col + 1)
            vectortoimg(X[np.random.randint(len(T))],show=False)
    plt.show()
示例#26
0
def plot_sinad_sfdr (label, data_x, data_y, chans=[0,1,2,3],
                     titles=['SFDR','SINAD']):
    """
    x   x values of data (same for all chans)
    y   array with shape (2, chans, data)
    """
    n=len(chans)    
    n2=len(titles)
    pos=np.arange(n2*n)+1

    for t in range(n2):
        pos_val=pos[t::n2]
        for chan in chans:
            plt.subplot(n,n2,pos_val[chan])
            plt.plot(data_x,data_y[t][chan],label=label)
            if t==0:
                plt.ylabel('Chan %i' %chan)
            if chan==0:
                plt.title(titles[t])
示例#27
0
    def lasso_regression(data, predictors, alpha, models_to_plot={}):
        # Fit the model
        lassoreg = Lasso(alpha=alpha, normalize=True, max_iter=1e5)
        lassoreg.fit(data[predictors], data['y'])
        y_pred = lassoreg.predict(data[predictors])
        # Check if a plot is to be made for the entered alpha
        if alpha in models_to_plot:
            plt.subplot(models_to_plot[alpha])
            plt.tight_layout()
            plt.plot(data['x'], y_pred)
            plt.plot(data['x'], data['y'], '.')
            plt.title('Plot for alpha:%.3g' % alpha)

        # Return the result in pre-defined format
        rss = sum((y_pred - data['y'])**2)
        ret = [rss]
        ret.extend([lassoreg.intercept_])
        ret.extend(lassoreg.coef_)
        return ret
def print_save_all_mean_faces(x_class_mean,global_mean,show,save):
	rows = 4
	cols = 14
	index = 1
	font_size = 10
	plt.figure(figsize=(20,10))
	plt.subplot(rows,cols,index), plt.imshow(np.reshape(global_mean,(46,56)).T, cmap = 'gist_gray')
	title = str("Global Mean")
	plt.title(title, fontsize=font_size).set_position([0.5,0.95]), plt.xticks([]), plt.yticks([])
	index=index+1
	for i in range(0,x_class_mean.shape[1]):
		title = str("Class Mean "+str(i+1))
		plt.subplot(rows,cols,index), plt.imshow(np.reshape(x_class_mean[:,i],(46,56)).T, cmap = 'gist_gray')
		plt.title(title, fontsize=font_size).set_position([0.5,0.95]), plt.xticks([]), plt.yticks([])
		index = index+1
	if show == 'yes':
		plt.show()
	if save == 'yes':
		plt.savefig('Global and Class Mean')
	plt.close()
def plot_top_M_fisherfaces(eigenvec,M,Mpca,mode):
	if mode == 'None':
		return None
	cols = 10
	rows = M/cols
	index = 1
	font_size = 10
	plt.figure(figsize=(20,20))
	for i in range(0,M):
		plt.subplot(rows,cols,index),plt.imshow(np.reshape(eigenvec[:,i],(46,56)).T, cmap = 'gist_gray')
		face_title = str("M="+str(i+1))
		plt.title(face_title, fontsize=font_size).set_position([0.5,1.05]), plt.xticks([]), plt.yticks([])	# set_position([a,b]) adjust b for height
		index = index+1
	if mode == 'show':
		plt.show()
		plt.close()
	if mode == 'save':
		title = str("Top 50 Fisherfaces for Mpca="+str(Mpca))
		plt.savefig(title)
		plt.close()
def outputFeatureMap(sess,
                     x,
                     image_input,
                     tf_activation,
                     activation_min=-1,
                     activation_max=-1,
                     plt_num=1):
    # Here make sure to preprocess your image_input in a way your network expects
    # with size, normalization, ect if needed
    # image_input =
    # Note: x should be the same name as your network's tensorflow data placeholder variable
    # If you get an error tf_activation is not defined it maybe having trouble accessing the variable from inside a function
    activation = tf_activation.eval(session=sess, feed_dict={x: image_input})
    featuremaps = activation.shape[3]
    plt.figure(plt_num, figsize=(15, 15))
    for featuremap in range(featuremaps):
        plt.subplot(
            6, 8, featuremap + 1
        )  # sets the number of feature maps to show on each row and column
        plt.title('FeatureMap ' +
                  str(featuremap))  # displays the feature map number
        if activation_min != -1 & activation_max != -1:
            plt.imshow(activation[0, :, :, featuremap],
                       interpolation="nearest",
                       vmin=activation_min,
                       vmax=activation_max,
                       cmap="gray")
        elif activation_max != -1:
            plt.imshow(activation[0, :, :, featuremap],
                       interpolation="nearest",
                       vmax=activation_max,
                       cmap="gray")
        elif activation_min != -1:
            plt.imshow(activation[0, :, :, featuremap],
                       interpolation="nearest",
                       vmin=activation_min,
                       cmap="gray")
        else:
            plt.imshow(activation[0, :, :, featuremap],
                       interpolation="nearest",
                       cmap="gray")
示例#31
0
    def ridge_regression(data, predictors, alpha, models_to_plot={}):
        # Fit the model:1.初始化模型配置  2.模型拟合  3.模型预测
        ridgereg = Ridge(alpha=alpha, normalize=True)
        ridgereg.fit(data[predictors], data['y'])
        # predictors的内容实际是data(定义的一种DataFrame数据结构)的某列名称
        y_pred = ridgereg.predict(data[predictors])

        # Check if a plot is to be made for the entered alpha
        if alpha in models_to_plot:
            plt.subplot(models_to_plot[alpha])
            plt.tight_layout()
            plt.plot(data['x'], y_pred)  # 画出拟合曲线图
            plt.plot(data['x'], data['y'], '.')  # 画出样本的散点图
            plt.title('Plot for alpha: %.3g' % alpha)

        # Return the result in pre-defined format
        rss = sum((y_pred - data['y'])**2)
        ret = [rss]
        ret.extend([ridgereg.intercept_])
        ret.extend(ridgereg.coef_)
        return ret
示例#32
0
def Harris(img):
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    blur = cv.GaussianBlur(gray, (5, 5), 7)
    Ix = cv.Sobel(blur, cv.CV_64F, 1, 0, ksize=5)
    Iy = cv.Sobel(blur, cv.CV_64F, 0, 1, ksize=5)

    IxIy = np.multiply(Ix, Iy)
    Ix2 = np.multiply(Ix, Ix)
    Iy2 = np.multiply(Iy, Iy)

    Ix2_blur = cv.GaussianBlur(Ix2, (7, 7), 10)
    Iy2_blur = cv.GaussianBlur(Iy2, (7, 7), 10)
    IxIy_blur = cv.GaussianBlur(IxIy, (7, 7), 10)

    det = np.multiply(Ix2_blur, Iy2_blur) - np.multyply(IxIy_blur, IxIy_blur)
    trace = Ix2_blur + Iy2_blur

    R = det - 0.05 * np.multiply(trace, trace)

    plt.subplot(1, 2, 1), plt.imshow(img), plt.axis('off')
    plt.subplot(1, 2, 2), plt.imshow(R, cmap='gray'), plt.axis('off')
示例#33
0
    def plot(self, plot_col, model=None, max_subplots=3):
        inputs, labels = self.example
        plt.figure(figsize=(12, 8))

        plot_col_index = self.column_indices[plot_col]
        max_n = min(max_subplots, len(inputs))

        for n in range(max_n):
            plt.subplot(3, 1, n + 1)
            plt.ylabel(f'{plot_col} [normed]')
            plt.plot(
                self.input_indices, inputs[n, :, plot_col_index],
                label='Inputs', marker='.', zorder=-10
            )

            if self.label_columns:
                label_col_index = self.label_columns_indices.get(plot_col, None)
            else:
                label_col_index = plot_col_index

            if label_col_index is None:
                continue

            plt.scatter(
                self.label_indices, labels[n, :, label_col_index],
                edgecolors='k', label='Labels', c='#2ca02c', s=64
            )

            if model is not None:
                predictions = model(inputs)
                plt.scatter(
                    self.label_indices, predictions[n, :, label_col_index],
                    marker='X', edgecolors='k', label='Predictions',
                    c='#ff7f0e', s=64
                )

            if n == 0:
                plt.legend()

        plt.xlabel('Time')
示例#34
0
def plot_hist_(path):
    hist = pd.read_csv(path)
    acc = hist["accuracy"]
    val_acc = hist["val_accuracy"]

    loss = hist["loss"]
    val_loss = hist["val_loss"]
    epochs_range = range(len(hist))

    plt.figure(figsize=(8, 8))
    plt.subplot(1, 2, 1)
    plt.plot(epochs_range, acc, label='Training Accuracy')
    plt.plot(epochs_range, val_acc, label='Validation Accuracy')
    plt.legend(loc='lower right')
    plt.title('Training and Validation Accuracy')

    plt.subplot(1, 2, 2)
    plt.plot(epochs_range, loss, label='Training Loss')
    plt.plot(epochs_range, val_loss, label='Validation Loss')
    plt.legend(loc='upper right')
    plt.title('Training and Validation Loss')
    plt.show()
示例#35
0
def main(path):
    data = [(row["Year"], float(row["Average income per tax unit"]))
            for row in dataset(path, "Country", "United States")]
    width = 0.35
    ind = np.arange(len(data))
    fig = plt.figure()
    ax = plt.subplot(111)
    ax.bar(ind, list(d[1] for d in data))
    ax.set_xticks(np.arange(0, len(data), 4))
    ax.set_xticklabels(list(d[0] for d in data)[0::4], rotation=45)
    ax.set_ylabel("Income in USD")
    plt.title("U.S. Average Income 1913-2008")
    plt.show()
示例#36
0
文件: api.py 项目: nbingham1/Patch
def plotter(flows, args, uid):

    figure()
    plot(flows[0], 'o-')
    xlabel("Sentence Number")
    ylabel("Correlation to previous flow")
    title("Segmentation of Article into Primary Thoughts (Fused signal)")
    for el in args:
        axvline(x=el, ymin=0, linewidth=1, color='r')
    savefig('/var/www/patch/'+str(uid)+'flow.png')

    figure()
    subplot(2,1,1)
    plot(flows[1], 'o-')
    ylabel("Correlation to previous flow")
    title("Part Of Speech Signal")

    plt.subplot(2, 1, 2)
    plot(flows[2], 'o-')
    xlabel("Sentence Number")
    ylabel("Correlation to previous flow")
    title("Proper Noun Signal") 
    savefig('/var/www/patch/'+str(uid)+'subplots.png')
示例#37
0
def plotCentroidFitDiagnostic(img, hdr, ccdMod, ccdOut, res, prfObj):
    """Some diagnostic plots showing the performance of fitPrfCentroid()

    Inputs:
    -------------
    img
        (np 2d array) Image of star to be fit. Image is in the
        format img[row, col]. img should not contain Nans

    hdr
        (Fits header object) header associated with the TPF file the
        image was drawn from

    ccdMod, ccdOut
        (int) CCD module and output of image. Needed to
        create the correct PRF model

    prfObj
        An object of the class prf.KeplerPrf()


    Returns:
    -------------
    **None**

    Output:
    ----------
    A three panel subplot is created
    """
    mp.figure(1)
    mp.clf()
    mp.subplot(131)
    plotTpf.plotCadence(img, hdr)
    mp.colorbar()
    mp.title("Input Image")

    mp.subplot(132)
    c,r = res.x[0], res.x[1]
    bbox = getBoundingBoxForImage(img, hdr)
    model = prfObj.getPrfForBbox(ccdMod, ccdOut, c, r, bbox)
    model *= res.x[2]
    plotTpf.plotCadence(model, hdr)
    mp.colorbar()
    mp.title("Best fit model")

    mp.subplot(133)
    diff = img-model
    plotTpf.plotCadence(diff, hdr)
    mp.colorbar()
    mp.title("Residuals")

    print "Performance %.3f" %(np.max(np.abs(diff))/np.max(img))
示例#38
0
"""
For channels 2 and 3
"""
adc_cal.clear_ogp()
adc_cal.clear_inl()

freqarray=[50,800,30]

sfdr,sinad=adc_cal.do_sfdr_sinad_cw_sweep(chans=[2,3], freqarray=freqarray)

sinad_values,freqs= dic2arr(sinad)
sfdr_values,f = dic2arr(sfdr)

""" No corrections """
plt.subplot(221)
plt.plot(freqs,sinad_values[0],'-*', label='No corrections')
plt.title('SINAD')
plt.ylabel('Chan 2')

plt.subplot(223)
plt.plot(freqs,sinad_values[1],'-*', label='No corrections')
plt.ylabel('Chan 3')

plt.subplot(222)
plt.plot(freqs,sfdr_values[0],'-*', label='No corrections')
plt.title('SFDR')

plt.subplot(224)
plt.plot(freqs,sfdr_values[1],'-*', label='No corrections')
示例#39
0
# Now fit the data (R) using the model we just instatiated. Note that we only need trial-to-trial data when we want to optimize over the regularization parameter.

# In[5]:


Z = dpca.fit_transform(R,trialR)


# In[6]:


time = arange(T)

plt.figure(figsize=(16,7))
plt.subplot(131)

for s in range(S):
    plt.plot(time,Z['t'][0,s])

plt.title('1st time component')
    
subplot(132)

for s in range(S):
    plot(time,Z['s'][0,s])
    
title('1st stimulus component')
    
subplot(133)
import numpy as np
import cv2

import matplotlib as plt

'''
img = cv2.imread('/Users/Haoyang/Downloads/Pics/boldt.jpg',0)
b,g,r = cv2.split(img)
img2 = cv2.merge([r,g,b])
plt.subplot(121);plt.imshow(img) # expects distorted color
plt.subplot(122);plt.imshow(img2) # expect true color
plt.show()

cv2.imshow('bgr image',img) # expects true color
cv2.imshow('rgb image',img2) # expects distorted color
cv2.waitKey(0)
cv2.destroyAllWindows()
'''
示例#41
0
def train_dcgan_labeled(gen, dis, epoch0=0):
    print('CHAINER begin training');    sys.stdout.flush()

    o_gen = optimizers.Adam(alpha=0.0002, beta1=0.5)
    o_dis = optimizers.Adam(alpha=0.0002, beta1=0.5)
    print('CHAINER begin gen');    sys.stdout.flush()
    o_gen.setup(gen)
    o_dis.setup(dis)
    print('CHAINER begin add');    sys.stdout.flush()
    o_gen.add_hook(chainer.optimizer.WeightDecay(0.00001))
    o_dis.add_hook(chainer.optimizer.WeightDecay(0.00001))
    print('CHAINER begin zvis');    sys.stdout.flush()
    # zvis = (xp.random.uniform(-1, 1, (100, nz), dtype=np.float32))
    print('CHAINER begin for');    sys.stdout.flush()
    for epoch in range(epoch0,n_epoch):
        print("epoch:",epoch)
        sys.stdout.flush()
        perm = np.random.permutation(n_train)
        sum_l_dis = np.float32(0)
        sum_l_gen = np.float32(0)
        
        for i in range(0, n_train, batchsize):
            # discriminator
            # 0: from dataset
            # 1: from noise

            #print "load image start ", i
            x2 = np.zeros((batchsize, 3, 96, 96), dtype=np.float32)
            for j in range(batchsize):
                #try:
                    rnd = np.random.randint(len(dataset))
                    rnd2 = np.random.randint(2)

                    img = np.asarray(Image.open(io.BytesIO(dataset[rnd])).convert('RGB')).astype(np.float32).transpose(2, 0, 1)
                    x2[j,:,:,:] = (img[:,0:96,0:96]-128.0)/128.0
                #except:
                #    print('read image error occured', fs[rnd])
            #print "load image done"
            
            # train generator
            z = Variable(xp.random.uniform(-1, 1, (batchsize, nz), dtype=np.float32))
            x = gen(z)
            yl = dis(x)
            L_gen = F.softmax_cross_entropy(yl, Variable(xp.zeros(batchsize, dtype=np.int32)))
            L_dis = F.softmax_cross_entropy(yl, Variable(xp.ones(batchsize, dtype=np.int32)))
            
            # train discriminator
                    
            x2 = Variable(cuda.to_gpu(x2))
            yl2 = dis(x2)
            L_dis += F.softmax_cross_entropy(yl2, Variable(xp.zeros(batchsize, dtype=np.int32)))
            
            #print "forward done"

            o_gen.zero_grads()
            L_gen.backward()
            o_gen.update()
            
            o_dis.zero_grads()
            L_dis.backward()
            o_dis.update()
            
            sum_l_gen += L_gen.data.get()
            sum_l_dis += L_dis.data.get()
            
            #print "backward done"

            if i%image_save_interval==0:
                pylab.rcParams['figure.figsize'] = (16.0,16.0)
                pylab.clf()
                vissize = 100
                z = zvis
                z[50:,:] = (xp.random.uniform(-1, 1, (50, nz), dtype=np.float32))
                z = Variable(z)
                x = gen(z, test=True)
                x = x.data.get()
                for i_ in range(100):
                    tmp = ((np.vectorize(clip_img)(x[i_,:,:,:])+1)/2).transpose(1,2,0)
                    pylab.subplot(10,10,i_+1)
                    pylab.imshow(tmp)
                    pylab.axis('off')
                pylab.savefig('%s/vis_%d_%d.png'%(out_image_dir, epoch,i))
                
        serializers.save_hdf5("%s/dcgan_model_dis_%d.h5"%(out_model_dir, epoch),dis)
        serializers.save_hdf5("%s/dcgan_model_gen_%d.h5"%(out_model_dir, epoch),gen)
        serializers.save_hdf5("%s/dcgan_state_dis_%d.h5"%(out_model_dir, epoch),o_dis)
        serializers.save_hdf5("%s/dcgan_state_gen_%d.h5"%(out_model_dir, epoch),o_gen)
        print('epoch end', epoch, sum_l_gen/n_train, sum_l_dis/n_train)
    	return 0
	
def read():
	pickle_file = open("pickled_data.pkl", "r")
	t = pickle.load(pickle_file)
	v = pickle.load(pickle_file)
	print t
	print v

initialize(v, s, t, dt, n)
calculate(v, s, t, dt, n)
store(v, t, n)

#plot
plt.figure(1)
plt.subplot(211)
plt.plot(t, v,"g-", linewidth=2.0)
plt.scatter(t, v)
plt.title('The Velocity of a Free Falling Object')
plt.xlabel('Time($t$)', fontsize=14)
plt.ylabel('Velocity($m/s$)', fontsize=14)
plt.text(3,-60,r'$g = 9.8 m/s^2$', fontsize=16)
plt.grid(True)

plt.subplot(212)
plt.plot(t, s,"g-", linewidth=2.0)
plt.scatter(t, s)
plt.title('The Displacement of a Free Falling Object')
plt.xlabel('Time($t$)', fontsize=14)
plt.ylabel('Displacement($m$)', fontsize=14)
plt.text(3,-300,r'$g = 9.8 m/s^2$', fontsize=16)