Example #1
0
def LAG_Fit(slctd_Features_Train, trainLabel):
    #lag1 = LAG(encode='distance', num_clusters=[12,12,12,12,12,12,12,12,12,12], alpha=10, learner=myLLSR(onehot=False))
    #lag2 = LAG(encode='distance', num_clusters=[12,12,12,12,12,12,12,12,12,12], alpha=10, learner=myLLSR(onehot=False))
    #lag3 = LAG(encode='distance', num_clusters=[12,12,12,12,12,12,12,12,12,12], alpha=10, learner=myLLSR(onehot=False))
    lag1 = LAG(encode='distance',
               num_clusters=[5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
               alpha=10,
               learner=myLLSR(onehot=False))
    lag2 = LAG(encode='distance',
               num_clusters=[5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
               alpha=10,
               learner=myLLSR(onehot=False))
    lag3 = LAG(encode='distance',
               num_clusters=[5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
               alpha=10,
               learner=myLLSR(onehot=False))
    #Extract Training Features
    slctd_Features_Train1 = slctd_Features_Train[0]
    slctd_Features_Train2 = slctd_Features_Train[1]
    slctd_Features_Train3 = slctd_Features_Train[2]
    #Fit the three LAG units
    lag1.fit(slctd_Features_Train1, trainLabel)
    lag2.fit(slctd_Features_Train2, trainLabel)
    lag3.fit(slctd_Features_Train3, trainLabel)
    #Get the Transformed Training Features
    features_Train_Trans1 = lag1.transform(slctd_Features_Train1)
    features_Train_Trans2 = lag2.transform(slctd_Features_Train2)
    features_Train_Trans3 = lag3.transform(slctd_Features_Train3)
    #Cascade all the LAG features
    features_Train_LAG = np.concatenate(
        (features_Train_Trans1, features_Train_Trans2, features_Train_Trans3),
        axis=1)
    return features_Train_LAG, lag1, lag2, lag3
Example #2
0
def LAG_Fit(slctd_Features_Train, trainLabel):
    num1 = 10
    num2 = 10
    num3 = 10
    num4 = 10
    num5 = 10
    numCluster1 = [num1, num1, num1, num1, num1, num1, num1, num1, num1, num1]
    numCluster2 = [num2, num2, num2, num2, num2, num2, num2, num2, num2, num2]
    numCluster3 = [num3, num3, num3, num3, num3, num3, num3, num3, num3, num3]
    numCluster4 = [num4, num4, num4, num4, num4, num4, num4, num4, num4, num4]
    numCluster5 = [num5, num5, num5, num5, num5, num5, num5, num5, num5, num5]
    lag1 = LAG(encode='distance',
               num_clusters=numCluster1,
               alpha=10,
               learner=myLLSR(onehot=False))
    lag2 = LAG(encode='distance',
               num_clusters=numCluster2,
               alpha=10,
               learner=myLLSR(onehot=False))
    lag3 = LAG(encode='distance',
               num_clusters=numCluster3,
               alpha=10,
               learner=myLLSR(onehot=False))
    lag4 = LAG(encode='distance',
               num_clusters=numCluster4,
               alpha=10,
               learner=myLLSR(onehot=False))
    lag5 = LAG(encode='distance',
               num_clusters=numCluster5,
               alpha=10,
               learner=myLLSR(onehot=False))
    #Extract Training Features
    slctd_Features_Train1 = slctd_Features_Train[0]
    slctd_Features_Train2 = slctd_Features_Train[1]
    slctd_Features_Train3 = slctd_Features_Train[2]
    slctd_Features_Train4 = slctd_Features_Train[3]
    slctd_Features_Train5 = slctd_Features_Train[4]
    #Fit the three LAG units
    lag1.fit(slctd_Features_Train1, trainLabel)
    lag2.fit(slctd_Features_Train2, trainLabel)
    lag3.fit(slctd_Features_Train3, trainLabel)
    lag4.fit(slctd_Features_Train4, trainLabel)
    lag5.fit(slctd_Features_Train5, trainLabel)
    #Get the Transformed Training Features
    features_Train_Trans1 = lag1.transform(slctd_Features_Train1)
    features_Train_Trans2 = lag2.transform(slctd_Features_Train2)
    features_Train_Trans3 = lag3.transform(slctd_Features_Train3)
    features_Train_Trans4 = lag4.transform(slctd_Features_Train4)
    features_Train_Trans5 = lag5.transform(slctd_Features_Train5)
    #Cascade all the LAG features
    features_Train_LAG = np.concatenate(
        (features_Train_Trans1, features_Train_Trans2, features_Train_Trans3,
         features_Train_Trans4, features_Train_Trans5),
        axis=1)
    return features_Train_LAG, lag1, lag2, lag3, lag4, lag5
def lag_oper(feature_ce, test_ce, train_label):
    my_LAG = LAG(encode='distance', num_clusters=[5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
                 alpha=10, learner=myLLSR(onehot=False))

    my_LAG.fit(feature_ce, train_label)
    X_train_trans = my_LAG.transform(feature_ce)
    X_test_trans = my_LAG.transform(test_ce)

    # X_train_pred_prob = my_LAG.predict_proba(feature_ce)
    # print(" --> train acc: %s" % str(my_LAG.score(feature_ce, train_label)))

    return X_train_trans, X_test_trans
Example #4
0
def LAG_train_unit(x_1, x_2, x_3, Y):
    input_list =  [x_1, x_2, x_3]
    the_list = []
    lag_list = []
    for i in range(3):
        print("Currently doing LAG for hop {}".format(i+1))
        lag = LAG(encode='distance', num_clusters=[2,2,2,2,2,2,2,2,2,2], alpha=10, learner=myLLSR(onehot=False))
        lag.fit(input_list[i], Y)
        X_train_trans = lag.transform(input_list[i])
        the_list.append(X_train_trans)
        lag_list.append(lag)
    return the_list, lag_list
Example #5
0
def LAG_Transform(slctd_Features_Train, slctd_Features_Test, trainNum, testNum):
    lag1 = LAG(encode='distance', num_clusters=[12,12,12,12,12,12,12,12,12,12], alpha=10, learner=myLLSR(onehot=False))
    lag2 = LAG(encode='distance', num_clusters=[12,12,12,12,12,12,12,12,12,12], alpha=10, learner=myLLSR(onehot=False))
    lag3 = LAG(encode='distance', num_clusters=[12,12,12,12,12,12,12,12,12,12], alpha=10, learner=myLLSR(onehot=False))
    #lag1 = LAG(encode='distance', num_clusters=[5,5,5,5,5,5,5,5,5,5], alpha=10, learner=myLLSR(onehot=False))
    #lag2 = LAG(encode='distance', num_clusters=[5,5,5,5,5,5,5,5,5,5], alpha=10, learner=myLLSR(onehot=False))
    #lag3 = LAG(encode='distance', num_clusters=[5,5,5,5,5,5,5,5,5,5], alpha=10, learner=myLLSR(onehot=False))
    #Extract Training Features
    slctd_Features_Train1 = slctd_Features_Train[0].reshape(trainNum, -1)
    slctd_Features_Train2 = slctd_Features_Train[1].reshape(trainNum, -1)
    slctd_Features_Train3 = slctd_Features_Train[2].reshape(trainNum, -1)
    #Extract Testing Features
    slctd_Features_Test1 = slctd_Features_Test[0].reshape(testNum, -1)
    slctd_Features_Test2 = slctd_Features_Test[1].reshape(testNum, -1)
    slctd_Features_Test3 = slctd_Features_Test[2].reshape(testNum, -1)
    #Fit the three LAG units
    lag1.fit(slctd_Features_Train1, trainLabel)
    lag2.fit(slctd_Features_Train2, trainLabel)
    lag3.fit(slctd_Features_Train3, trainLabel)
    #Get the Transformed Training Features
    features_Train_Trans1 = lag1.transform(slctd_Features_Train1)
    features_Train_Trans2 = lag2.transform(slctd_Features_Train2)
    features_Train_Trans3 = lag3.transform(slctd_Features_Train3)
    #Get the Transformed Testing Features
    features_Test_Trans1 = lag1.transform(slctd_Features_Test1)
    features_Test_Trans2 = lag2.transform(slctd_Features_Test2)
    features_Test_Trans3 = lag3.transform(slctd_Features_Test3)
    #Cascade all the LAG features
    features_Test_LAG = np.concatenate((features_Test_Trans1,features_Test_Trans2,features_Test_Trans3), axis=1)
    features_Train_LAG = np.concatenate((features_Train_Trans1,features_Train_Trans2,features_Train_Trans3), axis=1)
    return features_Test_LAG, features_Train_LAG
reducedFeatureImgs_unit1_test = featureSelectorWithoutCrossEnt(
    pooled_module1Out1_test, selectedFeaturesIndeces_unit1_train,
    numOfNs_unit1_train)
print("feature selection is done for train and test::"
      )  #Elapsed time is 99.167789 seconds.
timerObj.toc()

#LAG Unit
lag_unit1 = LAG(encode='distance',
                num_clusters=[7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
                alpha=10,
                learner=myLLSR(onehot=False))
lag_unit1.fit(reducedFeatureImgs_unit1_train, subYTrain4)
timerObj.tic()
finalFeature_unit1_train = lag_unit1.transform(reducedFeatureImgs_unit1_train)
print("LAG is done for train set:")  #Elapsed time is 0.321958 seconds.
timerObj.toc()

timerObj.tic()
finalFeature_unit1_test = lag_unit1.transform(reducedFeatureImgs_unit1_test)
print("LAG is done for test set:")  #Elapsed time is 0.173667 seconds.
timerObj.toc()

# In[ ]:

##2nd unit(depth 2)
##Module 2
#Max-pooling
pooled_module1Out2_train = MaxpoolingOperator(module1Out2_train)
pooled_module1Out2_test = MaxpoolingOperator(module1Out2_test)


lag1 = LAG(encode='distance', num_clusters=[5,5,5,5,5,5,5,5,5,5], alpha=10, learner=myLLSR(onehot=False)) 
lag1.fit(X_reshape1[:,:,f_number1].reshape(X_reshape1.shape[0],-1), y_train[0:nti])
lag2 = LAG(encode='distance', num_clusters=[5,5,5,5,5,5,5,5,5,5], alpha=10, learner=myLLSR(onehot=False)) 
lag2.fit(X_reshape2[:,:,f_number2].reshape(X_reshape2.shape[0],-1), y_train[0:nti])

end = time.time()
print('Module 2:',(end-start))


"""Module 3"""

start = time.time()
lag_output_0 = lag.transform(X_reshape0[:,:,f_number0].reshape(X_reshape0.shape[0],-1))
lag_output_1 = lag1.transform(X_reshape1[:,:,f_number1].reshape(X_reshape1.shape[0],-1))
lag_output_2 = lag2.transform(X_reshape2[:,:,f_number2].reshape(X_reshape2.shape[0],-1))


lag_concat = np.concatenate((lag_output_0,lag_output_1,lag_output_2),axis=1)
lag_concat.shape



from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import StratifiedKFold
import pandas as pd

clf = RandomForestClassifier(bootstrap=False , criterion= 'entropy', random_state=2,n_jobs=-1)
from lag import LAG
idx1 = np.argsort(output1Feature)
slice1 = idx1[:4018]
output_1 = output_1.reshape(len(output_1), -1)
t1 = (np.transpose(output_1))
in_lag1 = t1[slice1]
in_lag1 = in_lag1.reshape(len(in_lag1), -1)
in_lag_1 = np.transpose(in_lag1)
print(" input feature shape before reshape: %s" % str(in_lag_1.shape))
print(" > This is a test example: ")
lag1 = LAG(encode='distance',
           num_clusters=[5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
           alpha=10,
           learner=myLLSR(onehot=False))
lag1.fit(in_lag_1, label_train)
X_train_trans1 = lag1.transform(in_lag_1)
X_train_predprob1 = lag1.predict_proba(in_lag_1)
print(X_train_trans1.shape)
print("------- DONE -------\n")

#2nd LAG Layer
from sklearn.model_selection import train_test_split
from llsr import LLSR as myLLSR
from lag import LAG
idx2 = np.argsort(output2Feature)
slice2 = idx2[:2763]
output_2 = output_2.reshape(len(output_2), -1)
t2 = (np.transpose(output_2))
in_lag2 = t2[slice2]
in_lag2 = in_lag2.reshape(len(in_lag2), -1)
in_lag_2 = np.transpose(in_lag2)
        num_clusters=[NoC, NoC, NoC, NoC, NoC, NoC, NoC, NoC, NoC, NoC],
        alpha=10,
        learner=myLLSR(onehot=False))
    lag_2 = LAG(
        encode='distance',
        num_clusters=[NoC, NoC, NoC, NoC, NoC, NoC, NoC, NoC, NoC, NoC],
        alpha=10,
        learner=myLLSR(onehot=False))
    lag_3 = LAG(
        encode='distance',
        num_clusters=[NoC, NoC, NoC, NoC, NoC, NoC, NoC, NoC, NoC, NoC],
        alpha=10,
        learner=myLLSR(onehot=False))

    lag_1.fit(output[0], y_train[0:train_num])
    output[0] = lag_1.transform(output[0])

    lag_2.fit(output[1], y_train[0:train_num])
    output[1] = lag_2.transform(output[1])

    lag_3.fit(output[2], y_train[0:train_num])
    output[2] = lag_3.transform(output[2])

    # ------------------ Module 3 ------------------#

    feature_vector = np.zeros([train_num, 3 * NoC * 10])
    feature_vector[:, 0:NoC * 10] = output[0]
    feature_vector[:, NoC * 10:2 * NoC * 10] = output[1]
    feature_vector[:, 2 * NoC * 10:3 * NoC * 10] = output[2]

    # ------------------ Random Forest ------------------#