Yinxing commited on
Commit
fc8c1e7
·
verified ·
1 Parent(s): b633880

Upload my_module.py

Browse files
Files changed (1) hide show
  1. my_module.py +44 -0
my_module.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sklearn.model_selection import GridSearchCV
2
+ from sklearn.metrics import confusion_matrix, precision_score, recall_score, accuracy_score, f1_score
3
+
4
+ def my_evaluation(y, pred):
5
+ # 正解と予測値を入れると自動で評価してくれます。
6
+ print('混同行列:')
7
+ print(confusion_matrix(y, pred))
8
+ accuracy = accuracy_score(y, pred)
9
+ precision = precision_score(y, pred)
10
+ recall = recall_score(y, pred)
11
+ f1 = f1_score(y, pred)
12
+
13
+ print("正解率: %.3f" % accuracy)
14
+ print("精度: %.3f" % precision)
15
+ print("再現率: %.3f" % recall)
16
+ print("F1スコア: %.3f" % f1)
17
+
18
+
19
+ # 万能関数
20
+ def my_model(X_train, X_test, y_train, y_test, model, name=''):
21
+ # モデル訓練
22
+ model.fit(X_train, y_train)
23
+
24
+ pred = model.predict(X_test)# 予測
25
+ # モデル評価
26
+ print(f'モデル名:{name}')
27
+ my_evaluation(y_test, pred)
28
+ return model
29
+
30
+ def my_best_model(X_train, X_test, y_train, y_test, model,params, name=''):
31
+ # モデル訓練
32
+ # 基礎モデル、パラメーター集合
33
+ models = GridSearchCV(model, params)
34
+ models.fit(X_train, y_train)
35
+
36
+ best_model = models.best_estimator_
37
+ pred = best_model.predict(X_test)# 予測
38
+ # モデル評価
39
+ print(f'モデル名:{name}')
40
+ my_evaluation(y_test, pred)
41
+ return best_model
42
+
43
+
44
+