Пример #1
0
def main():
    digits = mnist()
    mynvb = nvb()
    x = center_matrix_SVD(digits.train_Images)
    mynvb.fit(digits.train_Images,digits.train_Labels)
    labels = mynvb.predict(digits.test_Images)
    errors_Full, error_Full_index = class_error_rate(labels,digits.test_Labels)
    mynvb.fit(x.PCA[:,:154],digits.train_Labels)
    newtest = (digits.test_Images -x.centers)@np.transpose(x.V[:154,:])
    labels = mynvb.predict(newtest)
    errors_154, error_Full_index = class_error_rate(labels,digits.test_Labels)
    mynvb.fit(digits.train_Images,digits.train_Labels)
    mynvb.fit(x.PCA[:,:50],digits.train_Labels)
    newtest = (digits.test_Images -x.centers)@np.transpose(x.V[:50,:])
    labels = mynvb.predict(newtest)
    errors_50, error_Full_index = class_error_rate(labels,digits.test_Labels)
    mynvb.fit(digits.train_Images,digits.train_Labels)
    mynvb.fit(x.PCA[:,:70],digits.train_Labels)
    newtest = (digits.test_Images -x.centers)@np.transpose(x.V[:70,:])
    labels = mynvb.predict(newtest)
    errors_70, error_Full_index = class_error_rate(labels,digits.test_Labels)
    print(errors_Full)
    print(errors_154)
    print(errors_50)
    print(errors_70)
    prob3_plots(mynvb,digits,newtest,pc=0)
    prob3_plots(mynvb,digits,newtest,pc=1)
    prob3_plots(mynvb,digits,newtest,pc=2)
    prob3_plots(mynvb,digits,newtest,pc=3)
Пример #2
0
def main():
    digits = mnist() # Creates a class with our mnist images and labels
    if open('Training SVD Data','rb')._checkReadable() == 0: # Check if file exist create it if it doesn't
        print("im here")
        x = center_matrix_SVD(digits.train_Images) # Creates a class with our svd and associated info
        pickle.dump(x,open('Training SVD Data','wb'))
    else:
        x = pickle.load(open('Training SVD Data','rb'))  # If we already have the file just load it
    if 0:
        test_Images_Center = np.subtract(digits.test_Images,np.repeat(x.centers,digits.test_Images.shape[0],0))
        tic()
        labels = local_kmeans_class(x.PCA[:,:50],digits.train_Labels,[email protected](x.V[:50,:]),10)
        toc()
        pickle.dump(labels,open('Loc_kmeans_50_lab','wb'))
    loc_full = pickle.load(open('Loc_kmeans_Full_lab','rb'))
    loc_50 = pickle.load(open('Loc_kmeans_50_lab','rb'))
    labels_Full = pickle.load(open('KNN_Full','rb'))
    # Have to transpose these because they came out backwards should fix if i use this agian
    errors_full,ind_full = class_error_rate(np.transpose(loc_full),digits.test_labels)
    errors_50,ind_50 = class_error_rate(np.transpose(loc_50),digits.test_labels)
    errors_near,ind_50 = class_error_rate(labels_Full,digits.test_labels)
    plt.figure()
    plt.plot(np.arange(10)+1, errors_full, color='Green', marker='o', markersize=10, label='Full')  #plots the 82.5%
    plt.plot(np.arange(10)+1, errors_50, color='Yellow', marker='o', markersize=10, label='82.5%')
    plt.plot(np.arange(10)+1, errors_near, color='Blue', marker='o', markersize=10, label='kNN')
    plt.grid(1) # Turns the grid on
    plt.title('Plot of local KNN Error rates')
    plt.legend(loc='upper right') # Puts a legend on the plot
    plt.show()
Пример #3
0
def main():
    digits = mnist()  # Creates a class with our mnist images and labels
    if open('Training SVD Data', 'rb')._checkReadable(
    ) == 0:  # Check if file exist create it if it doesn't
        print("im here")  # Just wanted to check if it was going in here
        x = center_matrix_SVD(
            digits.train_Images
        )  # Creates a class with our svd and associated info
        pickle.dump(x, open('Training SVD Data', 'wb'))
    else:
        x = pickle.load(open('Training SVD Data',
                             'rb'))  # If we already have the file just load it
    if 0:  # if this is zero skip
        test_Images_Center = np.subtract(
            digits.test_Images,
            np.repeat(x.centers, digits.test_Images.shape[0], 0))
        tic()
        myLDA = LDA()  # Create a new instance of the LDA class
        new_train = myLDA.fit_transform(
            x.PCA[:, :154], digits.train_Labels)  # It will fit based on x.PCA
        new_test = myLDA.transform(test_Images_Center @ np.transpose(
            x.V[:154, :]))  # get my transformed test dataset
        Knn_labels, nearest = KNN(new_train, digits.train_Labels, new_test,
                                  10)  # Run kNN on the new data
        toc()
        pickle.dump(Knn_labels, open('FDAKNN_Lables', 'wb'))
        pickle.dump(nearest, open('FDAKNN_neastest', 'wb'))
    fda = pickle.load(open('FDAKNN_Lables', 'rb'))
    labels_Full = pickle.load(open('KNN_Full', 'rb'))
    labels_50 = pickle.load(open('KNN_50', 'rb'))
    errors_fda, ind_fda = class_error_rate(fda, digits.test_labels)
    errors_near, ind_near = class_error_rate(labels_Full, digits.test_labels)
    errors_50, ind_50 = class_error_rate(labels_50, digits.test_labels)
    plt.figure()
    plt.plot(np.arange(10) + 1,
             errors_fda,
             color='Green',
             marker='o',
             markersize=10,
             label='fda')  #plots the 82.5%
    plt.plot(np.arange(10) + 1,
             errors_near,
             color='Blue',
             marker='o',
             markersize=10,
             label='kNN')
    plt.plot(np.arange(10) + 1,
             errors_50,
             color='Yellow',
             marker='o',
             markersize=10,
             label='kNN 50')
    plt.grid(1)  # Turns the grid on
    plt.title('Plot of Knn with FDA Error rates')
    plt.legend(loc='upper right')  # Puts a legend on the plot
    plt.show()
    print(confusion_matrix(digits.test_labels, labels_Full[5]))
    print(confusion_matrix(digits.test_labels, fda[5]))
    print(confusion_matrix(digits.test_labels, labels_50[5]))
    """
Пример #4
0
def main():
    digits = mnist()  # Creates a class with our mnist images and labels
    if open('Training SVD Data', 'rb')._checkReadable(
    ) == 0:  # Check if file exist create it if it doesn't
        print("im here")
        x = center_matrix_SVD(
            digits.train_Images
        )  # Creates a class with our svd and associated info
        pickle.dump(x, open('Training SVD Data', 'wb'))
    else:
        x = pickle.load(open('Training SVD Data',
                             'rb'))  # If we already have the file just load it
    if 0:
        test_Images_Center = np.subtract(
            digits.test_Images,
            np.repeat(x.centers, digits.test_Images.shape[0], 0))
        tic()
        labels = local_kmeans_class(
            x.PCA[:, :50], digits.train_Labels,
            test_Images_Center @ np.transpose(x.V[:50, :]), 10)
        toc()
        pickle.dump(labels, open('Loc_kmeans_50_lab', 'wb'))
    loc_full = pickle.load(open('Loc_kmeans_Full_lab', 'rb'))
    loc_50 = pickle.load(open('Loc_kmeans_50_lab', 'rb'))
    labels_Full = pickle.load(open('KNN_Full', 'rb'))
    # Have to transpose these because they came out backwards should fix if i use this agian
    errors_full, ind_full = class_error_rate(np.transpose(loc_full),
                                             digits.test_labels)
    errors_50, ind_50 = class_error_rate(np.transpose(loc_50),
                                         digits.test_labels)
    errors_near, ind_50 = class_error_rate(labels_Full, digits.test_labels)
    plt.figure()
    plt.plot(np.arange(10) + 1,
             errors_full,
             color='Green',
             marker='o',
             markersize=10,
             label='Full')  #plots the 82.5%
    plt.plot(np.arange(10) + 1,
             errors_50,
             color='Yellow',
             marker='o',
             markersize=10,
             label='82.5%')
    plt.plot(np.arange(10) + 1,
             errors_near,
             color='Blue',
             marker='o',
             markersize=10,
             label='kNN')
    plt.grid(1)  # Turns the grid on
    plt.title('Plot of local KNN Error rates')
    plt.legend(loc='upper right')  # Puts a legend on the plot
    plt.show()
Пример #5
0
def to_plt(digits,p,q):
    thing = pickle.load(open('LDA2DFDA'+ str(p) + 'x' + str(q) + '_EU.p','rb'))
    error, idw = class_error_rate(thing,digits.test_Labels)
    print(error)
    plt.plot(np.arange(10)+1,error,label='LDA2DFDA'+ str(p) + 'x' + str(q) + '_EU')
    thing = pickle.load(open('LDA2DFDA'+ str(p) + 'x' + str(q) + '_CB.p','rb'))
    error, idw = class_error_rate(thing,digits.test_Labels)
    print(error)
    plt.plot(np.arange(10)+1,error,label='LDA2DFDA'+ str(p) + 'x' + str(q) + '_CB')
    thing = pickle.load(open('LDA2DFDA'+ str(p) + 'x' + str(q) + '_Co.p','rb'))
    error, idw = class_error_rate(thing,digits.test_Labels)
    print(error)
    plt.plot(np.arange(10)+1,error,label='LDA2DFDA'+ str(p) + 'x' + str(q) + '_CO')
Пример #6
0
def doLDA(x,digits,s):
    myLDA = LDA()
    myLDA.fit(x.PCA[:,:s],digits.train_Labels)
    newtest = digits.test_Images -x.centers
    [email protected](x.V[:s,:])
    labels = myLDA.predict(newtest)
    errors = class_error_rate(labels.reshape(1,labels.shape[0]),digits.test_Labels)
    return errors
Пример #7
0
def put_into_excel(digits):
    labels_Full = pickle.load(open('KNN_Full','rb'))
    error_Full, error_Full_index = class_error_rate(labels_Full,digits.test_Labels)
    error_154,thing = pickle.load(open('LDA_154.p','rb'))
    error_50,thing = pickle.load(open('LDA_50.p','rb'))
    error_60,thing = pickle.load(open('LDA_60.p','rb'))
    errors = np.hstack((error_Full,error_154,error_50,error_60))
    import pandas
    df = pandas.DataFrame(errors)
    df.to_excel('Errors.xls')
Пример #8
0
def prob1_plots(digits):
    labels_Full = pickle.load(open('KNN_Full','rb'))
    error_Full, error_Full_index = class_error_rate(labels_Full,digits.test_Labels)
    error_154,thing = pickle.load(open('QDA_154.p','rb'))
    error_50,thing = pickle.load(open('QDA_50.p','rb'))
    error_60,thing = pickle.load(open('QDA_60.p','rb'))
    plt.figure()
    plt.bar([0,1,2,3],[error_Full[2],error_154,error_50,error_60])
    plt.title('Bar Plot of Error Rates')
    plt.show()
Пример #9
0
def main():
    digits = mnist() # Creates a class with our mnist images and labels
    if open('Training SVD Data','rb')._checkReadable() == 0: # Check if file exist create it if it doesn't
        x = center_matrix_SVD(digits.train_Images) # Creates a class with our svd and associated info
        pickle.dump(x,open('Training SVD Data','wb'))
    else:
        x = pickle.load(open('Training SVD Data','rb'))  # If we already have the file just load it
    if 1: # if this is zero skip
        test_Images_Center = np.subtract(digits.test_Images,np.repeat(x.centers,digits.test_Images.shape[0],0))
        tic()
        myLDA = LDA()  # Create a new instance of the LDA class
        new_train = myLDA.fit_transform(x.PCA[:,:154],digits.train_Labels)  # It will fit based on x.PCA
        new_test = myLDA.transform([email protected](x.V[:154,:])) # get my transformed test dataset
        Knn_labels = local_kmeans_class(new_train,digits.train_Labels,new_test,10) # Run kNN on the new data
        toc()
        pickle.dump(Knn_labels,open('Loc_kmeans_fda_lab','wb'))

    fda = pickle.load(open('Loc_kmeans_fda_lab','rb'))
    labels_Full = pickle.load(open('KNN_Full','rb'))
    loc_full = pickle.load(open('Loc_kmeans_Full_lab','rb'))
    errors_fda,ind_fda = class_error_rate(np.transpose(fda),digits.test_labels)
    errors_near,ind_near = class_error_rate(labels_Full,digits.test_labels)
    errors_full,ind_full = class_error_rate(np.transpose(loc_full),digits.test_labels)
    labels_50 = pickle.load(open('KNN_50','rb'))
    errors_50,ind_50 = class_error_rate(labels_50,digits.test_labels)
    print(errors_full)
    plt.figure()
    plt.plot(np.arange(10)+1, errors_fda, color='Green', marker='o', markersize=10, label='fda Kmeans')  #plots the 82.5%
    plt.plot(np.arange(10)+1, errors_near, color='Blue', marker='o', markersize=10, label='kNN')
    plt.plot(np.arange(10)+1, errors_full, color='Yellow', marker='o', markersize=10, label='Full Kmeans')
    plt.plot(np.arange(10)+1, errors_50, color='Red', marker='o', markersize=10, label='kNN 50')
    axes = plt.gca()
    axes.set_ylim([0.015,0.12])
    plt.grid(1) # Turns the grid on
    plt.title('Plot of Local Kmeans with FDA Error rates')
    plt.legend(loc='upper right')  # Puts a legend on the plot
    plt.show()
    project_back(x,digits)
Пример #10
0
def main():
    digits = mnist() # Creates a class with our mnist images and labels
    if open('Training SVD Data','rb')._checkReadable() == 0: # Check if file exist create it if it doesn't
        print("im here")   # Just wanted to check if it was going in here
        x = center_matrix_SVD(digits.train_Images) # Creates a class with our svd and associated info
        pickle.dump(x,open('Training SVD Data','wb'))
    else:
        x = pickle.load(open('Training SVD Data','rb'))  # If we already have the file just load it
    if 0: # if this is zero skip
        test_Images_Center = np.subtract(digits.test_Images,np.repeat(x.centers,digits.test_Images.shape[0],0))
        tic()
        myLDA = LDA()  # Create a new instance of the LDA class
        new_train = myLDA.fit_transform(x.PCA[:,:154],digits.train_Labels)  # It will fit based on x.PCA
        new_test = myLDA.transform([email protected](x.V[:154,:])) # get my transformed test dataset
        Knn_labels, nearest = KNN(new_train,digits.train_Labels,new_test,10) # Run kNN on the new data
        toc()
        pickle.dump(Knn_labels,open('FDAKNN_Lables','wb'))
        pickle.dump(nearest,open('FDAKNN_neastest','wb'))
    fda = pickle.load(open('FDAKNN_Lables','rb'))
    labels_Full = pickle.load(open('KNN_Full','rb'))
    labels_50 = pickle.load(open('KNN_50','rb'))
    errors_fda,ind_fda = class_error_rate(fda,digits.test_labels)
    errors_near,ind_near = class_error_rate(labels_Full,digits.test_labels)
    errors_50,ind_50 = class_error_rate(labels_50,digits.test_labels)
    plt.figure()
    plt.plot(np.arange(10)+1, errors_fda, color='Green', marker='o', markersize=10, label='fda')  #plots the 82.5%
    plt.plot(np.arange(10)+1, errors_near, color='Blue', marker='o', markersize=10, label='kNN')
    plt.plot(np.arange(10)+1, errors_50, color='Yellow', marker='o', markersize=10, label='kNN 50')
    plt.grid(1) # Turns the grid on
    plt.title('Plot of Knn with FDA Error rates')
    plt.legend(loc='upper right')  # Puts a legend on the plot
    plt.show()
    print(confusion_matrix(digits.test_labels,labels_Full[5]))
    print(confusion_matrix(digits.test_labels,fda[5]))
    print(confusion_matrix(digits.test_labels,labels_50[5]))
    """
Пример #11
0
def KNN_Plots(x,digits):
    # KNN plots
    labels_50 = pickle.load(open('KNN_50','rb'))
    labels_154 = pickle.load(open('KNN_154','rb'))
    labels_Full = pickle.load(open('KNN_Full','rb'))
    nearest_50 = pickle.load(open('Knn_50_nearest','rb'))
    nearest_154 = pickle.load(open('Knn_154_nearest','rb'))
    nearest_Full = pickle.load(open('Knn_Full_nearest','rb'))
    error_50, error_50_index = class_error_rate(labels_50,digits.test_labels)
    error_154, error_154_index = class_error_rate(labels_154,digits.test_labels)
    error_Full, error_Full_index = class_error_rate(labels_Full,digits.test_labels)
    print(error_50)
    print(error_154)
    print(error_Full)
    plt.figure()
    plt.bar([0,1,2],[error_50[2],error_154[2],error_Full[2]])
    plt.grid(1)
    plt.title('Bar Plot of Error Rates')
    plt.legend(loc='upper right')
    plt.show()
    error_50_index = np.asarray(np.where(error_50_index[2]))
    error_154_index = np.asarray(np.where(error_154_index.astype(int)[2]))
    error_Full_index = np.asarray(np.where(error_Full_index.astype(int)[2]))
    error_in_50_Full = error_50_index[0,inboth_index(error_Full_index[0],error_50_index[0])]
    # This is a loop that looks through digits the 50 dim PCA got correct but the full didn't
    for i in range(error_in_50_Full.shape[0]):
        j = error_in_50_Full[i]
        test_Images_Center = np.subtract(digits.test_Images,np.repeat(x.centers,digits.test_Images.shape[0],0))
        y = [email protected](x.V[:50,:])
        weighted_y = y[:,:50]@x.V[:50,:] + x.centers
        plt.subplot(2, 3, 1)
        plt.imshow(weighted_y[j].reshape(28,28),cmap='gray',interpolation = 'none')
        plt.axis('off')
        plt.title("In 50 %d Truth %d " % (np.asscalar(labels_50[2,j]), np.asscalar(digits.test_labels[j])))
        y = [email protected](x.V[:154,:])
        weighted_y2 = y[:,:154]@x.V[:154,:] + x.centers
        plt.subplot(2, 3, 2)
        plt.imshow(weighted_y2[j].reshape(28,28),cmap='gray',interpolation = 'none')
        plt.axis('off')
        plt.title("in 150 %d Truth %d " % (np.asscalar(labels_154[2,j]), np.asscalar(digits.test_labels[j])))
        plt.subplot(2, 3, 3)
        plt.imshow(digits.test_Images[j].reshape(28,28),cmap='gray')
        plt.axis('off')
        plt.title("in Full %d Truth %d " % (np.asscalar(labels_Full[2,j]), np.asscalar(digits.test_labels[j])))
        plt.subplot(2, 3, 4)
        weighted_x = x.PCA[:,:50]@x.V[:50,:] + x.centers
        myimage = np.hstack((weighted_x[nearest_50[j,0]].reshape(28,28),
                             weighted_x[nearest_50[j,1]].reshape(28,28),weighted_x[nearest_50[j,2]].reshape(28,28)))
        plt.imshow(myimage,cmap='gray')
        plt.title(np.array_str(digits.train_Labels[nearest_50[j,:3].astype(int)]))
        plt.axis('off')
        plt.subplot(2, 3, 5)
        weighted_x = x.PCA[:,:154]@x.V[:154,:] + x.centers
        myimage = np.hstack((weighted_x[nearest_154[j,0]].reshape(28,28),
                             weighted_x[nearest_154[j,1]].reshape(28,28),weighted_x[nearest_154[j,2]].reshape(28,28)))
        plt.imshow(myimage,cmap='gray')
        plt.title(np.array_str(digits.train_Labels[nearest_154[j,:3].astype(int)]))
        plt.axis('off')
        plt.subplot(2, 3, 6)
        weighted_x = x.a_centered + x.centers
        myimage = np.hstack((weighted_x[nearest_Full[j,0]].reshape(28,28),
                             weighted_x[nearest_Full[j,1]].reshape(28,28),weighted_x[nearest_Full[j,2]].reshape(28,28)))
        plt.imshow(myimage,cmap='gray')
        plt.title(np.array_str(digits.train_Labels[nearest_Full[j,:3].astype(int)]))
        print(np.array_str(nearest_Full[j,:3].astype(int)))
        print(np.array_str(nearest_154[j,:3].astype(int)))
        print(np.array_str(nearest_50[j,:3].astype(int)))
        plt.axis('off')
        plt.show()