OnevsrestClassifier和随机森林

ele*_*ora 2 python scikit-learn

我试图在http://scikit-learn.org/stable/auto_examples/model_selection/plot_roc.html重现该示例,但使用RandomForestClassifer.

我看不出如何转换这部分代码

# Learn to predict each class against the other
classifier = OneVsRestClassifier(svm.SVC(kernel='linear', probability=True,
                                 random_state=random_state))
y_score = classifier.fit(X_train, y_train).decision_function(X_test)
Run Code Online (Sandbox Code Playgroud)

我试过了

# Learn to predict each class against the other
classifier = OneVsRestClassifier(RandomForestClassifier())
y_score = classifier.fit(X_train, y_train).decision_function(X_test)
Run Code Online (Sandbox Code Playgroud)

但我明白了

# Learn to predict each class against the other
classifier = OneVsRestClassifier(svm.SVC(kernel='linear', probability=True,
                                 random_state=random_state))
y_score = classifier.fit(X_train, y_train).decision_function(X_test)
Run Code Online (Sandbox Code Playgroud)

有解决方法吗?

has*_*e55 7

那你应该知道用的是什么decision_function.它只与SVM分类器一起使用,因为它给出了数据点与分隔数据的超平面的距离,而当你用RandomForestClassifier它做分数时没有任何意义.您可以使用RFC支持的其他方法.predict_proba如果您想获得分类数据点的概率,可以使用.

以下是支持的功能的参考

只是提到RFC do支持oob_decision_function,这是你的训练集的估计值.

所以只需更换你的线路 -

y_score = classifier.fit(X_train, y_train).predict_proba(X_test)
Run Code Online (Sandbox Code Playgroud)

要么

y_score = classifier.fit(X_train, y_train).predict(X_test)
Run Code Online (Sandbox Code Playgroud)