Update my_module.py
Browse files- 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 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
| 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 |
+
|