【问题标题】:Print Estimator Name in SkLearn在 SkLearn 中打印估算器名称
【发布时间】:2019-01-05 06:30:44
【问题描述】:

在 Sklearn 中,有没有办法打印出估算器的类名?

我曾尝试使用 name 属性,但不起作用。

from sklearn.linear_model import LogisticRegression  

def print_estimator_name(estimator):
    print(estimator.__name__)

#Expected Outcome:
print_estimator_name(LogisticRegression())

我希望这会打印出上面的分类器名称

【问题讨论】:

  • type(estimator)?
  • 你的名字是什么意思?你是说类名吗?在这种情况下 type() 应该可以解决问题。您是指分配给实例化对象的内存地址吗?另外,也许您只想要对象上的 __str__() 方法?

标签: python model scikit-learn


【解决方案1】:

我认为您正在寻找 estimator.__class__.__name__ 即:

from sklearn.linear_model import LogisticRegression

def print_estimator_name(estimator):
    print(estimator.__class__.__name__)

#Expected Outcome:
print_estimator_name(LogisticRegression())

【讨论】:

    【解决方案2】:

    我有另一种方法。获取对象名称,转换为 str,使用split(".") 获取最重要的子类,最后去除不需要的字符

    str(type(clf)).split(".")[-1][:-2]
    

    这对我来说适用于 SKLearn、XGBoost 和 LightGBM

    print(f'Acc: {pred:0.5f} for the {str(type(clf)).split(".")[-1][:-2])}')
    
    Acc: 0.7159443 : DecisionTreeClassifier
    Acc: 0.7572368 : RandomForestClassifier
    Acc: 0.7548593 : ExtraTreesClassifier
    Acc: 0.7416970 : XGBClassifier
    Acc: 0.7582540 : LGBMClassifier
    

    【讨论】:

      猜你喜欢
      • 2017-08-08
      • 2020-09-13
      • 2020-09-05
      • 2020-11-03
      • 1970-01-01
      • 1970-01-01
      • 2016-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多