Yellowbrick旨在与scikit-learn一起使用,并使用sklearn的类型检查系统来检测模型是否适合特定类别的机器学习问题。如果neupy PNN
模型实现了scikit-learn估计器API(例如fit()
和predict()
),则可以直接使用该模型并通过使用{{ 1}}参数如下:
force_model=True
但是,快速浏览neupy documentation后,由于neupy方法被命名为visualizer = ClassificationReport(model,support=True,force_model=True)
而不是train
,并且由于PNN模型确实被命名,因此这似乎不一定有效不实现fit
方法,也不支持后缀为score()
的学习参数。
解决方案是创建一个围绕_
模型的轻量级包装,将其作为sklearn估计器公开。在Yellowbrick数据集上进行测试,看来可行:
PNN
尽管Yellowbrick目前不支持neupy,但是如果您有兴趣-可能值得submitting an issue建议将neupy添加到contrib中,类似于在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,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,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()
的方式。
本文链接:https://www.f2er.com/3124049.html