-
Notifications
You must be signed in to change notification settings - Fork 0
/
N-BaIoT-Optimized_GBDT.py
78 lines (61 loc) · 2.02 KB
/
N-BaIoT-Optimized_GBDT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 29 14:05:18 2020
@author: 1804499
"""
from sklearn.model_selection import train_test_split
import numpy as np
import lightgbm as lgb
from sklearn.model_selection import GridSearchCV, ParameterGrid
import time
from memory_profiler import profile
from sklearn.metrics import auc, accuracy_score, roc_auc_score, roc_curve
import os
import psutil
def data():
benign = np.loadtxt("benign_train.csv", delimiter = ",")
benscan = np.loadtxt("ben_mir_gaf.csv", delimiter = ",")
alldata = np.concatenate((benign, benscan))
j = len(benscan[0])
data = alldata[:, 1:j]
benlabel = alldata[:, 0]
bendata = (data - data.min()) / (data.max() - data.min())
bendata, benmir, benlabel, benslabel = train_test_split(bendata, benlabel, test_size = 0.3)
return bendata, benmir, benlabel, benslabel
param = { 'task': 'train',
'num_leaves': 2,
#'n_estimators': 2,
'boosting_type': 'gbdt',
'bagging_freq': 2,
'bagging_fraction': 0.4,
'feature_fraction': 0.4,
'learning_rate': 0.0001,
'objective': 'binary',
'reg_alpha': 0.0001,
'verbose': 0
#'min_data_in_leaf': 0,
#'max_depth':-1
}
train, test, trainlabel, testl = data()
def traind(train, trainlabel, param):
lgb_train =lgb.Dataset(train, trainlabel)
gbm = lgb.train(param, lgb_train)
return gbm
mdl = traind(train, trainlabel, param)
def predict_clf(gbm, test):
ypred = gbm.predict(test)
acc = roc_auc_score(testl, ypred)
return acc
def eval():
mi = psutil.Process(os.getpid())
mit = mi.memory_info()
sti = mit.rss
start_test_time = time.time()
acc = predict_clf(mdl, test)
end_test_time = time.time()
algtime = end_test_time - start_test_time
mp = psutil.Process(os.getpid())
mpt = mp.memory_info()
stm = mpt.rss - sti
return acc, algtime, stm
ac, algt, stm = eval()