| | from sklearn.model_selection import GridSearchCV |
| | from sklearn.metrics import confusion_matrix, precision_score, recall_score, accuracy_score, f1_score |
| | import numpy as np |
| | def my_evaluation(y, pred): |
| | |
| | print('混同行列:') |
| | print(confusion_matrix(y, pred)) |
| | accuracy = accuracy_score(y, pred) |
| |
|
| | print("正解率: %.3f" % accuracy) |
| |
|
| | if len(np.unique(y))==2: |
| | precision = precision_score(y, pred) |
| | recall = recall_score(y, pred) |
| | f1 = f1_score(y, pred) |
| | print("精度: %.3f" % precision) |
| | print("再現率: %.3f" % recall) |
| | print("F1スコア: %.3f" % f1) |
| |
|
| |
|
| | |
| | def my_model(X_train, X_test, y_train, y_test, model, name=''): |
| | |
| | model.fit(X_train, y_train) |
| |
|
| | pred = model.predict(X_test) |
| | |
| | print(f'モデル名:{name}') |
| | my_evaluation(y_test, pred) |
| | return model |
| |
|
| | def my_best_model(X_train, X_test, y_train, y_test, model,params, name=''): |
| | |
| | |
| | models = GridSearchCV(model, params) |
| | models.fit(X_train, y_train) |
| |
|
| | best_model = models.best_estimator_ |
| | pred = best_model.predict(X_test) |
| | |
| | print(f'モデル名:{name}') |
| | my_evaluation(y_test, pred) |
| | return best_model |
| |
|
| |
|
| |
|
| |
|