Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Getting ['AxesSubplot' object is not subscriptable] when trying to use skopt.plots with result object as returned by Optimizer.tell() #740

@frederikfaye

Description

@frederikfaye

I am using skopt.optimize.Optimizer, as I need the ask() and tell() functionality, and I would like to use the plotting functions in skopt.plots on the results of my optimization.

As far as I can understand, what the plotting functions need is the scipy.optimize.OptimizeResult object, which is exactly what the tell() method returns, so I'm trying to pass that:

import numpy as np
import matplotlib.pyplot as plt
from skopt import Optimizer
from skopt.plots import plot_objective

# Define objective
def objective(x, noise_level=0.1):
    return np.sin(5 * x[0]) * (1 - np.tanh(x[0] ** 2)) + np.random.randn() * noise_level

# Initialize Optimizer
opt = Optimizer([(-2.0, 2.0)], n_initial_points=1)

# Optimize
for i in range(2):
    next_x = opt.ask()
    f_val = objective(next_x)
    res = opt.tell(next_x, f_val)

# Plot results
ax = plot_objective(res)

However, this results in the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-d718026f55fc> in <module>
     20 
     21 # Plot results
---> 22 ax = plot_objective(res)

~/miniconda3/envs/py3/lib/python3.6/site-packages/skopt/plots.py in plot_objective(result, levels, n_points, n_samples, size, zscale, dimensions)
    425                                             n_points=n_points)
    426 
--> 427                 ax[i, i].plot(xi, yi)
    428                 ax[i, i].axvline(result.x[i], linestyle="--", color="r", lw=1)
    429 

TypeError: 'AxesSubplot' object is not subscriptable

It seems the only difference between what skopt.optimizer.base.base_minimize() and tell() returns is that the result object from tell() has attribute specs set to None.

I can see in the source code for base_minimize() that specs there is set to

{"args": copy.copy(inspect.currentframe().f_locals),
"function": inspect.currentframe().f_code.co_name}

Is this difference the reason why the above does not work? If so, what would be a nice way to do this? Also, isn't "function" always just "base_minimize"? Why is this needed?

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions