-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModel.py
174 lines (134 loc) · 4.92 KB
/
Model.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
import numpy as np
import lightgbm as lgb
import time
import pickle
import argparse
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import confusion_matrix,accuracy_score
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split,GridSearchCV
from sklearn.utils.class_weight import compute_class_weight
def get_config():
parser=argparse.ArgumentParser()
parser.add_argument("--pkl_file",type=str,
default="./log/array.pkl",
help="the path of pickle file")
parser.add_argument("--test_ratio",type=float,
default=0.2,
help="the ratio of test size")
parser.add_argument("--n_jobs",type=int,
default=4,
help="the number threads for training")
parser.add_argument("--learning_rate",type=float,
default=0.1,
help="the learning ratio for training")
parser.add_argument("--boosting_type",type=str,
default="gbdt",
choices=["gbdt","dart","rf","goss"],
help="which boosting type")
parser.add_argument("--num_leaves",type=int,
default=31)
parser.add_argument("--n_estimators",type=int,
default=100)
parser.add_argument("--objective",type=str,
default="multiclass")
parser.add_argument("--subsample",type=float,
default=0.9,
help="the ratio of smaple for row,must < 1.0")
parser.add_argument("--subsample_freq",type=int,
default=100)
parser.add_argument("--colsample_bytree",type=float,
default=0.9)
parser.add_argument("--max_depth",type=int,
default=5,
help="Max depths for building tree")
parser.add_argument("--num_iter",type=int,
default=10000,
help="number boost rounds to train")
parser.add_argument("--save_dir",type=str,
default="./model",
help="the path for saving model")
args=parser.parse_args()
return args
def show_time(diff):
m, s = divmod(diff, 60)
h, m = divmod(m, 60)
s,m,h = int(round(s, 0)), int(round(m, 0)), int(round(h, 0))
print("Execution Time: " + "{0:02d}:{1:02d}:{2:02d}".format(h, m, s))
def GridSearch(clf, params, X, y, X_predict, y_predict):
# Train
start = time.time()
model = GridSearchCV(clf, params, scoring='accuracy', n_jobs=5, cv=5).fit(X,y).best_estimator_
end = time.time()
print('Training time: ')
show_time(end - start)
# Predict
start = time.time()
scores=accuracy_score(y_predict, model.predict(X_predict))
print(scores)
end = time.time()
print('Prediction time: ')
show_time(end - start)
return model
def Build_Model(args,class_weight,for_cv=False):
boosting_type=args.boosting_type
num_leaves=args.num_leaves
max_depth=args.max_depth
learning_rate=args.learning_rate
n_estimators=args.n_estimators
objective=args.objective
subsample=args.subsample
subsample_freq=args.subsample_freq if hasattr(args,"subsample_freq") else 0
colsample_bytree=args.colsample_bytree
n_jobs=args.n_jobs
random_state =7777
model=lgb.LGBMClassifier(boosting_type=boosting_type,
num_leaves=num_leaves,
max_depth=max_depth,
learning_rate=learning_rate,
n_estimators=n_estimators,
objective=objective,
class_weight=class_weight,
subsample=subsample,
subsample_freq=subsample_freq,
colsample_bytree=colsample_bytree,
n_jobs=n_jobs,
random_state=random_state)
if for_cv:
model=lgb.LGBMClassifier(objective=objective,
n_jobs=n_jobs,
random_state=random_state,
class_weight=class_weight)
return model
def predictor(model_file,data_file):
with open(model_file,"rb") as fin:
model=pickle.load(fin)
with open(data_file,"rb") as fp:
data=pickle.load(fp)
fp.close()
X=data["array"]
y=data["cluster"]
features=data["genes"]
feature_to_key={key:gene for key,gene in enumerate(features)}
print("There are : {} features in data".format(len(features)))
print('Feature importances:')
for key in list(model.feature_importances_)[:20]:
print("No.{} important feature is {}".format(key,feature_to_key[key]))
print("Encoder cluster -> label ")
encoder=LabelEncoder()
encoder.fit(y)
label=encoder.transform(y)
num_classes=len(np.unique(label))
batch_size=1024
n_batch=X.shape[0]//batch_size
i=1
for count in range(n_batch):
batch_x=X[i*batch_size:batch_size*(i+1)]
batch_y=label[i*batch_size:batch_size*(i+1)]
prob=model.predict(batch_x)
prediction=np.argmax(prob,1)
report=classification_report(batch_y,prediction)
print("No.{} report is : ".format(count+1))
print(report)
i+=1