def find_optimal_C_for_AUC(xTrain, yTrain, xTest, yTest):
C_2d_range = [10.0 ** i for i in range(-3, 3)]
accuracy = np.array([])
auc_score = np.array([])
for Ctry in C_2d_range:
clf = SVC(C=Ctry, kernel="linear", probability=True)
clf.fit(xTrain, yTrain)
pred = clf.predict(xTest)
pred_proba = clf.predict_proba(xTest)
accuracy = np.append(accuracy, np.average(yTest == pred))
auc_score = np.append(auc_score,
roc_auc_score(yTest, pred_proba[:, 1]))
print "C: {}" .format(Ctry)
print "accuracy: {}" .format(accuracy[-1])
print "AUC: {}" .format(auc_score[-1])
# Extract the optimal parameters to train the final model
best_auc_idx = np.where(auc_score == max(auc_score))[0]
best_acc_idx = np.where(accuracy == max(accuracy[best_auc_idx]))[0]
best_C = C_2d_range[best_acc_idx[0]]
return best_C
评论列表
文章目录