简体   繁体   English

如何检查分类器是否属于 sklearn.tree?

[英]How to check if a classifier belongs to sklearn.tree?

Suppose, I have a trained model, and I would like to check whether the model is a tree-based classifier.假设,我有一个训练有素的 model,我想检查 model 是否是基于树的分类器。 What is the best way to determine it?确定它的最佳方法是什么?

eg I'm looking for something following:例如,我正在寻找以下内容:

import sklearn
from imaginarypackage import listmodules
if type(clf).__name__ in listmodules(sklearn.tree)

I have tried:我努力了:

>>> import pkgutil
>>> 'DecisionTreeClassifier' in pkgutil.iter_modules(["sklearn.tree"])
>>> False

I understand not all the tree-based models (eg RandomForest) are under skelarn.tree.我了解并非所有基于树的模型(例如 RandomForest)都在 skelarn.tree 下。 Hence, having a generic solution will be of very much help.因此,拥有一个通用的解决方案将非常有帮助。

Thanks in advance!提前致谢!

Check type of tree model检查树的类型 model

As @Alexander Santos suggests, you can use the method from this answer to check which module your class belongs to.正如@Alexander Santos 建议的那样,您可以使用此答案中的方法来检查您的 class 属于哪个模块。 As far as I can tell, the tree based models are either a part of sklearn.tree or sklearn.ensemble._tree modules.据我所知,基于树的模型是sklearn.treesklearn.ensemble._tree模块的一部分。

# Method 1: check if object type has __module__ attribute
module = getattr(clf, '__module__', '')

if module.startswith('sklearn.tree') or module.startswith('sklearn.ensemble._tree'):
    print("clf is a tree model")

Alternatively, a less python-esque method is to convert the type to a string and perform the same comparison.或者,一种不太像 python 的方法是将type转换为字符串并执行相同的比较。

# Method 2: convert type to string
type_ = str(type(clf))

if "sklearn.tree" in type_ or "sklearn.ensemble._tree" in type_:
    print("Clf is probably a tree model")

You can obviously rewrite this more efficiently if you need to test against many more than just two modules.如果您需要针对两个以上的模块进行测试,您显然可以更有效地重写它。

Alternative 'hack'替代“黑客”

By inspecting the methods of DecisionTree , RandomForest and ExtraTrees regressor and classifiers using dir(clf) , it appears all the models you want to test for have methods such as:通过使用dir(clf)检查DecisionTreeRandomForestExtraTrees回归器和分类器的方法,似乎您要测试的所有模型都具有以下方法:

  • min_samples_leaf
  • min_weight_fraction_leaf
  • max_leaf_nodes

So if you really needed one check to validate your model type, you can inspect the model's methods:因此,如果您真的需要一项检查来验证您的 model 类型,您可以检查模型的方法:

attributes = dir(clf)

check_for_list = ['min_samples_leaf', 'min_weight_fraction_leaf', 'max_leaf_nodes']

verdict = False
for check in check_for_list:
    if check in attributes:
        verdict = True
        break

if verdict:
    print("clf is probably a tree-based model.")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python-如何从 sklearn.tree 导入决策树分类器 - Python- How to import DecisionTree Classifier from sklearn.tree 如何使用sklearn.tree输出进行预测的节点? - How can I output the node making a prediction using sklearn.tree? 如何解决 ImportError: cannot import name 'DesicionTreeClassifier' from 'sklearn.tree' in python? - How to resolve the ImportError: cannot import name 'DesicionTreeClassifier' from 'sklearn.tree' in python? 导入错误:无法从“sklearn.tree”导入名称“tree” - Import Error: cannot import name 'tree' from 'sklearn.tree' 如何检查 sklearn model 是分类器还是回归器 - How to check if sklearn model is classifier or regressor 决策树 AttributeError:模块“sklearn.tree”在 Jupyter Notebook 中没有属性“plot_tree”错误 - Decision Tree AttributeError: module 'sklearn.tree' has no attribute 'plot_tree' Error in Jupyter Notebook 导入错误:无法从“sklearn.tree”导入名称“plot_tree” - ImportError: cannot import name 'plot_tree' from 'sklearn.tree' 当使用来自 sklearn.tree 的 DecisionTreeRegressor 时,model.predict(x.head) 真正预测的是什么? - What is model.predict(x.head) really predicting when using DecisionTreeRegressor from sklearn.tree? Python sklearn决策树分类器具有多个功能? - Python sklearn decision tree classifier with multiple features? Sklearn 决策树分类器 - 动物猜谜游戏 - Sklearn Decision Tree Classifier - Animal Guessing game
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM