def over(C,solver,class_weight): clf = LogisticRegression(C=C, solver=solver, class_weight=class_weight) clf.fit(log['train_feature'], log['train_category']) train_sc=ai_def.estimation(clf, log['train_feature']) valid_sc=ai_def.estimation(clf, log['valid_feature']) test_sc=ai_def.estimation(clf, log['test_feature']) train_accuracy = accuracy_score(log['train_category'],train_sc) valid_accuracy = accuracy_score(log['valid_category'],valid_sc) test_accuracy = accuracy_score(log['test_category'],test_sc) result=[] result.append([C,train_accuracy, valid_accuracy, test_accuracy]) return result
""" 55. 混同行列の作成 52で学習したロジスティック回帰モデルの混同行列 (confusion matrix)を, 学習データおよび評価データ上で作成せよ """ from sklearn.metrics import confusion_matrix import ai_def log=ai_def.Lg() clf=ai_def.load_model() train_score=ai_def.estimation(clf,log['train_feature']) train_matrix = confusion_matrix(log['train_category'],train_score) print(train_matrix) test_score=ai_def.estimation(clf,log['test_feature']) test_matrix = confusion_matrix(log['test_category'],test_score) print(test_matrix)
""" import ai_def import numpy as np from tqdm import tqdm from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score from matplotlib import pyplot as plt log=ai_def.Lg() result=[] for C in tqdm(np.logspace(-5,4,10,base=10)): lg=LogisticRegression(random_state=123, max_iter=10000, C=C) clf=lg.fit(log['train_feature'], log['train_category']) train_sc=ai_def.estimation(clf, log['train_feature']) valid_sc=ai_def.estimation(clf, log['valid_feature']) test_sc=ai_def.estimation(clf, log['test_feature']) train_accuracy = accuracy_score(log['train_category'],train_sc) valid_accuracy = accuracy_score(log['valid_category'],valid_sc) test_accuracy = accuracy_score(log['test_category'],test_sc) result.append([C,train_accuracy, valid_accuracy, test_accuracy]) result=np.array(result).T plt.plot(result[0],result[1], label='train') plt.plot(result[0],result[2], label='valid') plt.plot(result[0],result[3], label='test') plt.ylim(0,1.1) plt.ylabel('Accuracy')