1.lr.predict_proba(under_text_x) 獲得的是正負的概率值
在sklearn邏輯回歸的計算過程中,使用的是大於0.5的是正值,小於0.5的是負值,我們使用使用不同的概率結果判定來研究概率閾值對結果的影響
從圖中我們可以看出,閾值越小,被判為正的越多,即大於閾值的就是為正,但是存在一個很明顯的問題就是很多負的也被判為正值。
當閾值很小時,數據的召回率很大,但是整體數據的准確率很小
因此我們需要根據召回率和准確率的綜合考慮選擇一個合適的閾值
lr = LogisticRegression(C=best_c, penalty='l1') lr.fit(under_train_x, under_train_y) pred_array = np.array(lr.predict_proba(under_text_x)) thresholds = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9] j = 1 for threshold in thresholds: pred_y_new = np.zeros([len(under_text_x), 1]) pred_y_new[pred_array[:, 1] > threshold] = 1 # 獲得矩陣 plt.subplot(3, 3, j) conf = confusion_matrix(under_test_y, pred_y_new) # 畫圖 plot_matrix(conf, classes=[0, 1], title='threshod is {}'.format(threshold)) accurracy = (conf[0, 0] + conf[1, 1]) / (conf[0, 0] + conf[0, 1] + conf[1, 0] + conf[1, 1]) # 召回率 recall = conf[1, 1] / (conf[1, 0] + conf[1, 1]) j = j + 1 plt.show()