DecisionTreeClassifier

DecisionTreeClassifier

我正在使用来自scikit-learn的DecisionTreeClassifier对一些数据进行分类。我还使用其他算法,为了进行比较,我使用了精确调用指标下的面积。问题是DecisionTreeClassifier的AUPRC的形状是正方形,而不是您期望该度量标准的通常形状。

这是我如何为DecisionTreeClassifier计算AUPRC。我在计算时遇到了一些麻烦,因为DecisionTreeClassifer不像其他分类器(如LogisticRegression)一样没有decision_function()

这些是我为SVM的AUPRC,Logistic回归和DecisionTreeClassifier获得的结果

这是我如何计算DecisionTreeClassifier的AUPRC

def execute(X_train, y_train, X_test, y_test):
    tree = DecisionTreeClassifier(class_weight='balanced')
    tree_y_score = tree.fit(X_train, y_train).predict(X_test)

    tree_ap_score = average_precision_score(y_test, tree_y_score)

    precision, recall, _ = precision_recall_curve(y_test, tree_y_score)
    values = {'ap_score': tree_ap_score, 'precision': precision, 'recall': recall}
    return values


这是我如何计算SVM的AUPRC:

def execute(X_train, y_train, X_test, y_test):
    svm = SVC(class_weight='balanced')
    svm.fit(X_train, y_train.values.ravel())
    svm_y_score = svm.decision_function(X_test)

    svm_ap_score = average_precision_score(y_test, svm_y_score)

    precision, recall, _ = precision_recall_curve(y_test, svm_y_score)
    values = {'ap_score': svm_ap_score, 'precision': precision, 'recall': recall}
    return values


这是我如何计算LogisticRegression的AUPRC:

def execute(X_train, y_train, X_test, y_test):
    lr = LogisticRegression(class_weight='balanced')
    lr.fit(X_train, y_train.values.ravel())
    lr_y_score = lr.decision_function(X_test)

    lr_ap_score = average_precision_score(y_test, lr_y_score)

    precision, recall, _ = precision_recall_curve(y_test, lr_y_score)
    values = {'ap_score': lr_ap_score, 'precision': precision, 'recall': recall}
    return values


然后,我将它们称为方法,并绘制如下结果:

import LogReg_AP_Harness as lrApTest
import SVM_AP_Harness as svmApTest
import DecTree_AP_Harness as dtApTest
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import label_binarize
import matplotlib.pyplot as plt


def do_work(df):
    X = df.ix[:, df.columns != 'Class']
    y = df.ix[:, df.columns == 'Class']

    y_binarized = label_binarize(y, classes=[0, 1])
    n_classes = y_binarized.shape[1]

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.3, random_state=0)

    _, _, y_train_binarized, y_test_binarized = train_test_split(X, y_binarized, test_size=.3, random_state=0)

    print('Executing Logistic Regression')
    lr_values = lrApTest.execute(X_train, y_train, X_test, y_test)
    print('Executing Decision Tree')
    dt_values = dtApTest.execute(X_train, y_train_binarized, X_test, y_test_binarized)
    print('Executing SVM')
    svm_values = svmApTest.execute(X_train, y_train, X_test, y_test)

    plot_aupr_curves(lr_values, svm_values, dt_values)


def plot_aupr_curves(lr_values, svm_values, dt_values):
    lr_ap_score = lr_values['ap_score']
    lr_precision = lr_values['precision']
    lr_recall = lr_values['recall']

    svm_ap_score = svm_values['ap_score']
    svm_precision = svm_values['precision']
    svm_recall = svm_values['recall']

    dt_ap_score = dt_values['ap_score']
    dt_precision = dt_values['precision']
    dt_recall = dt_values['recall']

    plt.step(svm_recall, svm_precision, color='g', alpha=0.2,where='post')
    plt.fill_between(svm_recall, svm_precision, step='post', alpha=0.2, color='g')

    plt.step(lr_recall, lr_precision, color='b', alpha=0.2, where='post')
    plt.fill_between(lr_recall, lr_precision, step='post', alpha=0.2, color='b')

    plt.step(dt_recall, dt_precision, color='r', alpha=0.2, where='post')
    plt.fill_between(dt_recall, dt_precision, step='post', alpha=0.2, color='r')

    plt.xlabel('Recall')
    plt.ylabel('Precision')
    plt.ylim([0.0, 1.05])
    plt.xlim([0.0, 1.0])
    plt.title('SVM (Green): Precision-Recall curve: AP={0:0.2f}'.format(svm_ap_score) + '\n' +
              'Logistic Regression (Blue): Precision-Recall curve: AP={0:0.2f}'.format(lr_ap_score) + '\n' +
              'Decision Tree (Red): Precision-Recall curve: AP={0:0.2f}'.format(dt_ap_score))
    plt.show()


do_work()方法中,我必须对y进行二值化处理,因为DecisionTreeClassifier没有descision_function()。我有here的方法。

这是情节:

python - DecisionTreeClassifier的精确调用曲线下的面积是一个正方形-LMLPHP

我想这可以归结为我不正确地为DecisionTreeClassifier计算AUPRC。

最佳答案

对于DecisionTreeClassifier,将predict替换为pred_proba;后者的作用与decision_function相同。

关于python - DecisionTreeClassifier的精确调用曲线下的面积是一个正方形,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49632828/

10-12 17:50