python - 如何仅获取 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
谢谢!
解决方案
你可以这样做:
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
推荐阅读
- php - 如何防止服务器端多次添加 - Laravel?
- sql-server - 无法捕获绕过代码中 try..catch 的 SQL Server SP 错误
- git - 如何配置 commitlint 以忽略某些提交消息,例如包含字符串“WIP”的任何提交消息?
- python - 如何修复“'float'对象不可下标”
- javascript - 通过 package.json 名称导入当前包导出
- javascript - 反应:常量对象没有输出
- excel - 从我刚刚打开 VBA 的工作簿中删除所有行
- python - 在 Python 中解析给定特定格式的文件名
- autotools - autoconf 没有在 AC_CONFIG_COMMANDS 中定义前缀
- python-3.x - 如何使用 scikit learn 确保测试集和训练集具有相同的功能?