【问题标题】:How to create a 3d graph of grid seach如何创建网格搜索的 3d 图
【发布时间】:2021-07-28 03:17:55
【问题描述】:

我正在研究一个随机森林回归器。 我使用了 GridSearch 并调整了超参数(max_features、n_estimators、max_depth)。 我从属性 cv_results_ 创建了结果数据框

`**results = pd.DataFrame(grid_search.cv_results_)

结果 = 结果[['param_max_features', 'param_n_estimators', 'param_max_depth', 'mean_test_score']]

results.columns = ['max_features', 'n_estimators', 'max_depth', 'mean_test_score']**`

这是 DataFrame 的样子: 现在我将创建一个交互式曲面图(如果可能的话,使用 plotly)来解释模型性能在参数变化时如何变化,但我不知道如何做到这一点(df 中有 4 列,但在图中需要 3 列)。

谢谢

【问题讨论】:

  • 欢迎来到stackoverflow。您能否展示results DataFrame 的示例,以及您尝试在 plotly 中创建曲面图的任何代码?这将有助于使您的问题更具重现性,以便人们可以帮助您找出答案
  • 我试图显示图形和数据框的屏幕截图。我希望你能看到图片
  • 请在您的问题中包含格式正确的代码。您的笔记本的屏幕截图是可见的,但人们必须逐行输入您的代码才能重现您的问题,并且能够更容易地复制和粘贴您所做的事情,以便我们诊断您的问题。您可以阅读更多关于创建最小可重现示例here

标签: plotly grid-search hyperparameters


【解决方案1】:

我认为您的问题没有明确定义,因为不清楚如何根据数据创建曲面图。如果您对两个变量执行网格搜索,那么您可以使用 mean_test_score 作为您的 z 值创建一个曲面图,因为网格搜索将在您的 x 和 y 值上进行。但是,由于您正在对三个变量执行网格搜索,如果您将 z 值设为这些变量中的任何一个,则将有多个 z 值对应于每个 x 和 y 值,从而形成一个封闭的矩形棱柱。

您可以做的是制作散点图,其中标记根据mean_test_score 值更改颜色。颜色比例尺将允许用户查看哪些超参数组合使平均测试分数最大化。

我重新创建了一些数据,我认为这些数据看起来像您的 results gridsearch DataFrame,其中每个超参数列具有离散值,并且平均测试分数范围与您的相似。在附图中,光标悬停在与最佳平均测试分数之一相对应的超参数组合上。

import numpy as np
import pandas as pd
import plotly.express as px

## recreate a DataFrame that looks how a gridsearch would look
features = list(range(1,7))
estimators = [25,50,75,100,125,150,175,200]
depth = list(range(1,6))
grid = []
for x in features:
    for y in estimators:
        for z in depth:
            grid.append([x,y,z])
results = pd.DataFrame(data=np.array(grid), columns=["max_features","n_estimators","max_depth"])

## let's assume your mean_test_score gradually increases with some noise 
np.random.seed(42)
mean_scores = np.linspace(0.17, 0.42, len(results)) + np.random.normal(0, 0.01, len(results))
results["mean_scores"] = mean_scores

fig = px.scatter_3d(results, x='max_features', y='n_estimators', z='max_depth', color='mean_scores')
fig.update_layout(
    title="Hyperparameter tuning",
    autosize=True, width=700, height=700,
    margin = dict(l=65, r=50, b=65, t=90))
fig.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 2017-05-07
    • 1970-01-01
    • 2019-12-23
    • 2016-08-30
    • 1970-01-01
    • 2018-02-24
    相关资源
    最近更新 更多