首页 > 解决方案 > 如何仅获取 SciKitLearn 中模型对象的名称?

问题描述

我有一个愚蠢的问题。

我在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

谢谢!

标签: pythonscikit-learn

解决方案


你可以这样做:

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

推荐阅读