Ejemplo n.º 1
0
def dim_reduction_mRMR(df, k):
    import pymrmr
    s = datetime.now()
    reduced = pymrmr.mRMR(df, 'MIQ', k)
    func.execution_time(s)
    # finish Beep sound
    func.beeep()
    # below is how we can prepare the data to use mRMR
    #ordinal_df = pd.DataFrame(pd.np.column_stack([y,X_pad]))
    #ordinal_df.rename(columns={0:'class'}, inplace=True)
    #for i in range(1,1001):
    #    ordinal_df.rename(columns={i:'Feat'+str(i)}, inplace=True)
    #reduced_ordf = prp.dim_reduction_mRMR(ordinal_df)
    return reduced
Ejemplo n.º 2
0
def lstm_eval(X, y, runtype):
    s = datetime.now()

    es = EarlyStopping(monitor='val_loss', patience=3, verbose=1)

    if (runtype == "10_fold_regular"):
        print("Regular 10-fold cross-validation Bi-LSTM")

        # 10-fold cross-validation
        lstm = neuralNet_10fold(X, y, ep=50, batch=256, netType='lstm', es=es)

        print(f"Testing Accuracy: {round(lstm[0],3)}%")
        print(f"F-score(macro): {round(lstm[1],3)}")
        print(f"F-score(micro): {round(lstm[4],3)}")
        print(f"Precision: {round(lstm[5],3)}")
        print(f"Recall: {round(lstm[6],3)}")

    elif (runtype == "10_fold_grand_mean"):
        print("Grand mean 10-fold cross-validation Bi-LSTM")
        # grand mean 10-fold cross-validation

        acc = 0
        fc1 = 0
        fc2 = 0
        perc = 0
        rec = 0
        for i in range(0, 30):
            print(f"Iteration {i+1}/30")
            lstm = neuralNet_10fold(X,
                                    y,
                                    ep=50,
                                    batch=256,
                                    netType='lstm',
                                    es=es)
            acc = acc + lstm[0]
            fc1 = fc1 + lstm[1]
            fc2 = fc2 + lstm[4]
            perc = perc + lstm[5]
            rec = rec + lstm[6]

        print("Grand mean of 10-fold cross-validation BiLSTM (30 runs)")
        print(f"Grand mean Accuracy: {round(acc/30,3)}%")
        print(f"Grand mean F-score(macro): {round(fc1/30,3)}")
        print(f"Grand mean F-score(micro): {round(fc2/30,3)}")
        print(f"Grand mean Precision: {round(perc/30,3)}")
        print(f"Grand mean Recall: {round(rec/30,3)}")

    func.execution_time(s)
Ejemplo n.º 3
0
def SVM_tuning(X, y):

    s = datetime.now()
    from sklearn.multiclass import OneVsRestClassifier
    clf = svm.SVC(verbose=2)

    # Choose some parameter combinations to try
    parameters = [{
        'kernel': ['rbf'],
        'gamma': [1e-2, 1e-3, 1e-4, 1e-5],
        'C': [0.001, 0.10, 0.1, 10, 25, 50, 100, 1000]
    }, {
        'kernel': ['sigmoid'],
        'gamma': [1e-2, 1e-3, 1e-4, 1e-5],
        'C': [0.001, 0.10, 0.1, 10, 25, 50, 100, 1000]
    }, {
        'kernel': ['linear'],
        'C': [0.001, 0.10, 0.1, 10, 25, 50, 100, 1000]
    }]

    acc_scorer = make_scorer(f1_score, average='macro')
    #    acc_scorer = make_scorer(average_precision_score)

    from sklearn import preprocessing
    #    y = preprocessing.label_binarize(y, classes=[1, 2, 3,4,5,6,7,8,9])

    #grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)
    grid_obj = GridSearchCV(clf,
                            parameters,
                            cv=3,
                            scoring=acc_scorer,
                            n_jobs=4)
    grid_obj = grid_obj.fit(X, y)

    # Set the clf to the best combination of parameters
    clf = grid_obj.best_estimator_
    print(grid_obj.best_params_)

    func.execution_time(s)
    # finish Beep sound
    func.beeep()

    return grid_obj.best_params_
Ejemplo n.º 4
0
def svm_eval(X, y, params, runtype):

    s = datetime.now()

    svm_params = params

    if (runtype == "10_fold_regular"):
        print("Regular 10-fold cross-validation SVM")
        svm = svm_10fold(X, y, svm_params)

        print(f"Testing Accuracy: {round(svm[2],3)}%")
        print(f"F-score(macro): {round(svm[3],3)}")
        print(f"F-score(micro): {round(svm[6],3)}")
        print(f"Precision: {round(svm[7],3)}")
        print(f"Recall: {round(svm[8],3)}")

    elif (runtype == "10_fold_grand_mean"):

        print("Grand mean 10-fold cross-validation SVM")
        #   grand mean

        acc = 0
        fc1 = 0
        fc2 = 0
        perc = 0
        rec = 0
        for i in range(0, 30):
            print(f"Iteration {i+1}/30")
            svm = svm_10fold(X, y, svm_params)
            acc = acc + svm[2]
            fc1 = fc1 + svm[3]
            fc2 = fc2 + svm[6]
            perc = perc + svm[7]
            rec = rec + svm[8]

        print("Grand mean of 10-fold cross-validation SVM (30 runs)")
        print(f"Grand mean Accuracy: {round(acc/30,3)}%")
        print(f"Grand mean F-score(macro): {round(fc1/30,3)}")
        print(f"Grand mean F-score(micro): {round(fc2/30,3)}")
        print(f"Grand mean Precision: {round(perc/30,3)}")
        print(f"Grand mean Recall: {round(rec/30,3)}")

    func.execution_time(s)
Ejemplo n.º 5
0
def dim_reduction_ForwardSelection(X, y, k):
    from sklearn import svm
    from mlxtend.feature_selection import SequentialFeatureSelector as SFS
    from sklearn.metrics import make_scorer
    from sklearn.metrics import f1_score
    s = datetime.now()
    acc_scorer = make_scorer(f1_score, average='macro')
    sfs = SFS(svm.SVC(),
              k_features=k,
              forward=True,
              floating=False,
              verbose=2,
              scoring=acc_scorer,
              cv=0)
    # Perform SFS
    sfs = sfs.fit(X, y)
    sfs.k_feature_names_
    feat_cols = list(sfs.k_feature_idx_)
    print(feat_cols)
    func.execution_time(s)
    # finish Beep sound
    func.beeep()
    return feat_cols