<reponame>Syuukakou/DeepLook_into_Android-type_ELF
import sys
sys.path.append("/home/syuu/Project/Src/git_repo/iconip2021")
from sklearn.linear_model import LogisticRegression
from Linux_Android_Classification.Src.common_libs import *
from Linux_Android_Classification.Src.data_process import get_firstN_rows_opcodes, data_process_opcodes, get_col_name
import pandas as pd

# r"../Files/opcodes624_fcns98-malicious.csv"
# filepath = "/home/syuu/Project/Src/git_repo/iconip2021/Linux_Android_Classification/Files/opcodes624_fcns249-malicious.csv"
filepath = "Linux_Android_Classification/Files/fcns.csv"
data, label = data_process_opcodes(filepath)
# data, label = get_firstN_rows_opcodes(filepath)
print(data, label)
label = np.array(label)
# test_list = []
# with open("result1/test_list.txt", "w") as tlf:
#     for i in test_list:
#         tlf.write("%s\n" % i)
result_params = []
lr_results = []
lr_recall = []
lr_f1 = []
lr_precision = []
# f_importances = []
# col_names = get_col_name()
# f_importances.append(col_names)
skf = StratifiedKFold(n_splits=10)
# skf.get_n_splits(data, label)
for train_index, test_index in skf.split(data, label):
    results = {}
    data_train, data_test = data[train_index], data[test_index]
    label_train, label_test = label[train_index], label[test_index]
    print("TRAIN: ", train_index, "TEST: ", test_index)
    seed = 1

    models = ["LR"]
    clfs = [LogisticRegression()]

    params = {
        models[0]: {'penalty': ['l2'], 'C': [0.01, 1.0],
                    'solver': ['newton-cg', 'lbfgs', 'sag'],
                    'max_iter': [1000]}
    }

    for name, estimator in zip(models, clfs):
        print(name)
        clf = GridSearchCV(estimator, params[name], scoring='roc_auc', refit=True, n_jobs=3, cv=10, verbose=5)
        clf.fit(data_train, label_train)

        print("best params: " + str(clf.best_params_))
        print("best scores: " + str(clf.best_score_))
        results[name] = {'best params': str(clf.best_params_), 'best scores': str(clf.best_score_)}
        result_params.append(results)

    lr_params = ast.literal_eval(results["LR"]["best params"])
    clf_lr = LogisticRegression(penalty=lr_params["penalty"],
                                C=lr_params["C"],
                                solver=lr_params["solver"],
                                max_iter=lr_params["max_iter"])
    clf_lr.fit(data_train, label_train)
    # clf_rf_score = clf_rf.score(data_test, label_test)
    label_predict = clf_lr.predict(data_test)

    clf_lr_score = roc_auc_score(label_test, label_predict)
    lr_results.append(clf_lr_score)
    clf_lr_recall = recall_score(label_test, label_predict)
    lr_recall.append(clf_lr_recall)
    clf_lr_f1 = f1_score(label_test, label_predict)
    lr_f1.append(clf_lr_f1)
    clf_lr_precision = precision_score(label_test, label_predict)
    lr_precision.append(clf_lr_precision)

with open("Linux_Android_Classification/Src/GridSearchCV/LRC/results/fcns_Rf_best_params.json", "w") as f:
    json.dump(result_params, f)

with open("Linux_Android_Classification/Src/GridSearchCV/LRC/results/fcns_Rf_results.txt", "w") as rff:
    for r in lr_results:
        rff.write("%s\n" % r)

with open("Linux_Android_Classification/Src/GridSearchCV/LRC/results/fcns_Rf_recall.txt", "w") as rf:
    for r_f in lr_recall:
        rf.write("%s\n" % r_f)

with open("Linux_Android_Classification/Src/GridSearchCV/LRC/results/fcns_Rf_f1.txt", "w") as rf1:
    for f_1 in lr_f1:
        rf1.write("%s\n" % f_1)

with open("Linux_Android_Classification/Src/GridSearchCV/LRC/results/fcns_Rf_precision.txt", "w") as rfp:
    for p in lr_precision:
        rfp.write("%s\n" % p)

import sys
sys.path.append("/home/syuu/Project/Src/git_repo/iconip2021")
from sklearn.linear_model import LogisticRegression
from Linux_Android_Classification.Src.common_libs import *
from Linux_Android_Classification.Src.data_process import get_firstN_rows_opcodes, data_process_opcodes, get_col_name
import pandas as pd

# r"../Files/opcodes624_fcns98-malicious.csv"
# filepath = "/home/syuu/Project/Src/git_repo/iconip2021/Linux_Android_Classification/Files/opcodes624_fcns249-malicious.csv"
filepath = "Linux_Android_Classification/Files/fcns.csv"
data, label = data_process_opcodes(filepath)
# data, label = get_firstN_rows_opcodes(filepath)
print(data, label)
label = np.array(label)
# test_list = []
# with open("result1/test_list.txt", "w") as tlf:
#     for i in test_list:
#         tlf.write("%s\n" % i)
result_params = []
lr_results = []
lr_recall = []
lr_f1 = []
lr_precision = []
# f_importances = []
# col_names = get_col_name()
# f_importances.append(col_names)
skf = StratifiedKFold(n_splits=10)
# skf.get_n_splits(data, label)
for train_index, test_index in skf.split(data, label):
    results = {}
    data_train, data_test = data[train_index], data[test_index]
    label_train, label_test = label[train_index], label[test_index]
    print("TRAIN: ", train_index, "TEST: ", test_index)
    seed = 1

    models = ["LR"]
    clfs = [LogisticRegression()]

    params = {
        models[0]: {'penalty': ['l2'], 'C': [0.01, 1.0],
                    'solver': ['newton-cg', 'lbfgs', 'sag'],
                    'max_iter': [1000]}
    }

    for name, estimator in zip(models, clfs):
        print(name)
        clf = GridSearchCV(estimator, params[name], scoring='roc_auc', refit=True, n_jobs=3, cv=10, verbose=5)
        clf.fit(data_train, label_train)

        print("best params: " + str(clf.best_params_))
        print("best scores: " + str(clf.best_score_))
        results[name] = {'best params': str(clf.best_params_), 'best scores': str(clf.best_score_)}
        result_params.append(results)

    lr_params = ast.literal_eval(results["LR"]["best params"])
    clf_lr = LogisticRegression(penalty=lr_params["penalty"],
                                C=lr_params["C"],
                                solver=lr_params["solver"],
                                max_iter=lr_params["max_iter"])
    clf_lr.fit(data_train, label_train)
    # clf_rf_score = clf_rf.score(data_test, label_test)
    label_predict = clf_lr.predict(data_test)

    clf_lr_score = roc_auc_score(label_test, label_predict)
    lr_results.append(clf_lr_score)
    clf_lr_recall = recall_score(label_test, label_predict)
    lr_recall.append(clf_lr_recall)
    clf_lr_f1 = f1_score(label_test, label_predict)
    lr_f1.append(clf_lr_f1)
    clf_lr_precision = precision_score(label_test, label_predict)
    lr_precision.append(clf_lr_precision)

with open("Linux_Android_Classification/Src/GridSearchCV/LRC/results/fcns_Rf_best_params.json", "w") as f:
    json.dump(result_params, f)

with open("Linux_Android_Classification/Src/GridSearchCV/LRC/results/fcns_Rf_results.txt", "w") as rff:
    for r in lr_results:
        rff.write("%s\n" % r)

with open("Linux_Android_Classification/Src/GridSearchCV/LRC/results/fcns_Rf_recall.txt", "w") as rf:
    for r_f in lr_recall:
        rf.write("%s\n" % r_f)

with open("Linux_Android_Classification/Src/GridSearchCV/LRC/results/fcns_Rf_f1.txt", "w") as rf1:
    for f_1 in lr_f1:
        rf1.write("%s\n" % f_1)

with open("Linux_Android_Classification/Src/GridSearchCV/LRC/results/fcns_Rf_precision.txt", "w") as rfp:
    for p in lr_precision:
        rfp.write("%s\n" % p)
# r"../Files/opcodes624_fcns98-malicious.csv" # filepath = "/home/syuu/Project/Src/git_repo/iconip2021/Linux_Android_Classification/Files/opcodes624_fcns249-malicious.csv" # data, label = get_firstN_rows_opcodes(filepath) # test_list = [] # with open("result1/test_list.txt", "w") as tlf: #     for i in test_list: #         tlf.write("%s\n" % i) # f_importances = [] # col_names = get_col_name() # f_importances.append(col_names) # skf.get_n_splits(data, label) # clf_rf_score = clf_rf.score(data_test, label_test)