Subspace-Feature-Representations / Discriminant Analysis (DA) / Multiclass_DA.py
Multiclass_DA.py
Raw
import pandas as pd
import numpy as np
from sklearn.neighbors import KNeighborsClassifier as knn
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score


kk = [1,5,10,15]
results_orginal = []
results_DA = []
results_SVD = []
for i in range(10):
    train_data=np.load("DATA_paper/BM_Pre_Train_Fea_{}.npy".format(i),allow_pickle=True)
    test_data=np.load("DATA_paper/BM_Pre_Test_Fea_{}.npy".format(i),allow_pickle=True)
    train_labels=np.load("DATA_paper/BM_Train_label_{}.npy".format(i),allow_pickle=True)
    test_labels=np.load("DATA_paper/BM_Test_label_{}.npy".format(i),allow_pickle=True)
    print(i)
    print("train_data",train_data.shape)
    print("test_data",test_data.shape)
    print("train_labels",train_labels.shape)
    print("test_labels",test_labels.shape)
    print("---")
       
    for q in kk:
        model_knn = knn(n_neighbors=q, weights='uniform', algorithm='auto')
        model_knn.fit(train_data,train_labels)
        result_or=model_knn.score(test_data,test_labels)
        results_orginal.append(result_or)
        
    lda = LDA(solver='eigen',n_components=7, shrinkage='auto')
    lda.fit(train_data, train_labels)
    project_da_train = lda.transform(train_data)
    project_da_test = lda.transform(test_data)
        
    for n in kk:
        #for j in range(1,8):
            model_knn_dv = knn(n_neighbors=n, weights='uniform', algorithm='auto')
            model_knn_dv.fit(project_da_train,train_labels)
            result_da=model_knn_dv.score(project_da_test,test_labels)
            results_DA.append(result_da)
            
    U2, s2, V2 = np.linalg.svd(train_data.T, full_matrices=False)
    project_svd_train = np.dot(U2.T[:7],train_data.T).T
    project_svd_test = np.dot(U2.T[:7],test_data.T).T
    
    for m in kk:
        #for d in range(1,8):
            model_knn_svd = knn(n_neighbors=m, weights='uniform', algorithm='auto')
            model_knn_svd.fit(project_svd_train,train_labels)
            result_svd=model_knn_svd.score(project_svd_test,test_labels)
            results_SVD.append(result_svd)