【问题标题】:How to obtain only the name of a model's object in SciKitLearn?如何在 SciKit Learn 中仅获取模型对象的名称?
【发布时间】:2019-03-16 17:29:25
【问题描述】:

我有一个愚蠢的问题。

我在scikit learn中做过交叉验证,想用我为每个模型得到的值做一个更直观的信息。

但是,我不能只访问要插入数据框的模板名称。始终与参数一起提供。是否有一些创建对象的方法只访问模型的名称,而不访问它的参数。还是我必须创建一个带有名称的外部列表?

我用:

for model in models:
   scores = cross_val_score(model, X, y, cv=5)
   print(f'Name model: {model} , Mean score: {scores.mean()}')

但是我通过参数获取名称:

Name model: LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False), Mean score: 0.8066782865537986

其实我想这样获取信息:

Name Model: LinearRegression, Mean Score: 0.8066782865537986

谢谢!

【问题讨论】:

    标签: python scikit-learn


    【解决方案1】:

    你可以这样做:

    model_name = type(model).__name__
    

    Python 3.5.5 | packaged by conda-forge | (default, Jul 23 2018, 23:45:11) 
    [GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from sklearn.linear_model import LinearRegression
    >>> model = LinearRegression()
    >>> model_name = type(model).__name__
    >>> print(model_name)
    LinearRegression
    

    【讨论】:

    • 你也可以这样做:model_name = model.__class__.__name__
    • 注意:模型必须是实例,而不是类,即 LinearRegression() 而不是 LinearRegression。否则你会得到父类的名字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 2017-09-06
    • 2019-08-18
    • 2012-08-30
    • 1970-01-01
    • 2015-11-21
    • 2012-12-08
    相关资源
    最近更新 更多