【问题标题】:classification report using YellowBrick使用 YellowBrick 的分类报告
【发布时间】:2020-03-07 04:14:00
【问题描述】:

我最近在 iris 数据集上实现了概率神经网络。我试图使用 YellowBrick 分类器打印分类报告,但是当我运行此代码时出现错误。如下。

from neupy import algorithms
model = algorithms.PNN(std=0.1, verbose=True, batch_size = 500)
model.train(X_train, Y_train)
predictions = model.predict(X_test)


from yellowbrick.classifier import ClassificationReport
visualizer = ClassificationReport(model, support=True)

visualizer.fit(X_train, Y_train)  # Fit the visualizer and the model
visualizer.score(X_test, Y_test)  # Evaluate the model on the test data
visualizer.show()  

此代码返回此错误。

YellowbrickTypeError: This estimator is not a classifier; try a regression or clustering score visualizer instead!

当我为其他分类模型尝试相同的分类报告代码时,它可以工作。我不知道。为什么会这样?谁能帮我解决这个问题?

【问题讨论】:

  • 请显示你的model到底是什么。
  • 嗨@desertnaut ..我已经添加了它。很抱歉没有早点添加它

标签: python machine-learning neural-network yellowbrick


【解决方案1】:

Yellowbrick 旨在与 scikit-learn 一起使用,并使用 sklearn 的类型检查系统来检测模型是否适合特定类别的机器学习问题。如果 neupy PNN 模型实现了 scikit-learn 估计器 API(例如 fit() 和 predict()) - 可以直接使用模型并通过使用 force_model=True 参数绕过类型检查,如下所示:

visualizer = ClassificationReport(model, support=True, force_model=True)

但是,在快速浏览 neupy documentation 之后,似乎这不一定有效,因为 neupy 方法被命名为 train 而不是 fit 并且因为 PNN 模型没有实现 score()方法也不支持_后缀的学习参数。

解决方案是创建一个轻量级包装器,围绕 PNN 模型将其公开为 sklearn 估算器。在 Yellowbrick 数据集上进行测试,这似乎可行:

from sklearn import metrics
from neupy import algorithms
from sklearn.base import BaseEstimator
from yellowbrick.datasets import load_occupancy
from yellowbrick.classifier import ClassificationReport
from sklearn.model_selection import train_test_split


class PNNWrapper(algorithms.PNN, BaseEstimator):
    """
    The PNN wrapper implements BaseEstimator and allows the classification
    report to score the model and understand the learned classes.
    """

    @property
    def classes_(self):
        return self.classes

    def score(self, X_test, y_test):
        y_hat = self.predict(X_test)
        return metrics.accuracy_score(y_test, y_hat)


# Load the binary classification dataset 
X, y = load_occupancy()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Create and train the PNN model using the sklearn wrapper
model = PNNWrapper(std=0.1, verbose=True, batch_size=500)
model.train(X_train, y_train)

# Create the classification report
viz = ClassificationReport(
    model, 
    support=True, 
    classes=["not occupied", "occupied"], 
    is_fitted=True, 
    force_model=True, 
    title="PNN"
)

# Score the report and show it
viz.score(X_test, y_test)
viz.show()

虽然 Yellowbrick 目前不支持 neupy,但如果您有兴趣 - 可能值得 submitting an issue 建议将 neupy 添加到 contrib,类似于在 Yellowbrick 中实现 statsmodels 的方式。

【讨论】:

  • 您能否指导我如何将 Yellowbrick 用于 xgb.train() 模型?我对这个模块相当陌生,无法让它适用于我的 XGB 模型
猜你喜欢
  • 2021-09-23
  • 2020-04-11
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 2019-01-26
  • 1970-01-01
  • 2020-02-14
  • 2012-11-03
相关资源
最近更新 更多