-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLBGM_Parametr_tuning.py
More file actions
153 lines (122 loc) · 5.25 KB
/
LBGM_Parametr_tuning.py
File metadata and controls
153 lines (122 loc) · 5.25 KB
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
import pandas as pd
from pandas import Series, DataFrame
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
from sklearn.metrics import roc_auc_score
from lightgbm import LGBMClassifier
from imblearn.over_sampling import SMOTE
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning) # FutureWarning 제거
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV, ShuffleSplit, learning_curve
import matplotlib.pyplot as plt
from sklearn.model_selection import GridSearchCV
import numpy as np
pricision_list = list()
recall_list = list ()
f1_score_list = list()
#모델링
def modeling(model,x_train,x_test,y_train,y_test):
model.fit(x_train,y_train)
pred = model.predict(x_test)
metrics(y_test,pred)
# print("1 " , sum(pred == 1))
# print("0 " , sum(pred == 0))
#평가 지표
def metrics(y_test,pred):
accuracy = accuracy_score(y_test,pred)
precision = precision_score(y_test,pred)
recall = recall_score(y_test,pred)
f1 = f1_score(y_test,pred)
print('accuracy : ', accuracy , ' precision : ', precision , ' recall : ', recall , 'f1 score ',f1)
pricision_list.append(precision)
recall_list.append(recall)
f1_score_list.append(f1)
def plot_learning_curve(estimator, x, y, ylim=None, cv=None, n_jobs=-1, train_sizes=np.linspace(.1, 1.0, 5), s=None):
if ylim is not None:
plt.ylim(*ylim)
# train_sizes : (392 + 392)의 80% 를 0.1, 0.325, 0.55, 0.775, 1의 비율로 학습시긴다.
train_sizes, train_scores, test_scores = learning_curve(estimator, x, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes, scoring=s)
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
test_scores_mean = np.mean(test_scores, axis=1)
test_scores_std = np.std(test_scores, axis=1)
# 평균에 표준 편차를 +-해준 영역을 색칠한다.
plt.fill_between(train_sizes, train_scores_mean - train_scores_std, train_scores_mean + train_scores_std, alpha=0.1, color="#ff9124")
plt.fill_between(train_sizes, test_scores_mean - test_scores_std, test_scores_mean + test_scores_std, alpha=0.1, color="#2492ff")
plt.plot(train_sizes, train_scores_mean, 'o-', color="#ff9124", label="Training score")
plt.plot(train_sizes, test_scores_mean, 'o-', color="#2492ff", label="Cross-validation score")
plt.xlabel('Training size')
plt.ylabel('neg_mean_squared_error')
# 그림에 선 표시
plt.grid(True)
# 범례 표시: best - 자동으로 최적의 위치에
plt.legend(loc="best")
plt.show()
df = pd.read_csv('creditcard.csv')
df = df.sample(frac=1)
# df.Class.value_counts(normalize=True).plot(kind='bar')
# print(df.Class.value_counts(normalize=True)*100)
X = df.iloc[:,:-1]
y = df.iloc[:,-1]
depth = [20, 30, 40, 50, -1]
n_estimators = [100,200,300,400,500,600,700,800,900,1000]
leaves = range(2,64)
ratio = list()
ratio_train = list()
for i in range(1, 11) :
ratio.append(i*0.1)
ratio_train.append(i*0.1)
# min_data_list = range(100, 1000, 10)
# min_child_samples_list = range (100, 5000, 100)
# for rT in ratio_train:
# round(rT,2)
# for r in ratio:
# round(r,2)
# X_train, X_test, y_train, y_test = train_test_split(X,y,test_size= 1-rT, random_state=10)
# smote = SMOTE( sampling_strategy= r , random_state=0 )
# X_train_over,y_train_over = smote.fit_resample(X_train,y_train)
# lgb = LGBMClassifier( n_estimators=400,n_jobs=-1,max_depth = 30 , num_leaves = 20, device = 'gpu', boost_from_average=False, min_data_in_leaf = 320 )
# print("Over Sampling Ratio : ", r , end="")
# print("Train, Test Ratio : ", rT , end="")
# # print("leaf number : ", leaf , end=" ")
# modeling(lgb,X_train_over,X_test,y_train_over,y_test)
# contest = ["precision", "recall", "f1"]
# plt.figure()
# plt.title('Test Ratio '+str(rT), fontsize=14)
# plt.plot(ratio, pricision_list, 'o-', color="#ff7473", label= "precision")
# plt.plot(ratio, recall_list, 'o-', color="#ffc952", label= "recall")
# plt.plot(ratio, f1_score_list, 'o-', color="#47b8e0", label= "f1")
# plt.xlabel('OverSampling Ratio' )
# plt.ylabel("Score")
# # 그림에 선 표시
# plt.grid(True)
# # 범례 표시: best - 자동으로 최적의 위치에
# plt.legend(loc="best")
# # plt.show()
# plt.savefig('Train - Test Ratio '+str(rT)+".png")
# pricision_list = list()
# recall_list = list ()
# f1_score_list = list()
# plt.figure()
# plt.plot(leaves, recall_list, 'o-', color="#ff9124", label= "recall")
# plt.xlabel('number of leaves')
# plt.ylabel("recall")
# # 그림에 선 표시
# plt.grid(True)
# # 범례 표시: best - 자동으로 최적의 위치에
# plt.legend(loc="best")
# plt.show()
# plt.figure()
# plt.plot(leaves, f1_score_list, 'o-', color="#ff9124", label= "f1")
# plt.xlabel('number of leaves')
# plt.ylabel("f1")
# # 그림에 선 표시
# plt.grid(True)
# # 범례 표시: best - 자동으로 최적의 위치에
# plt.legend(loc="best")
# plt.show()