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

Update my_module.py

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