ValueError when using Bayesian Optimization over a scikit GP model

330 Views Asked by At

I am utilizing a Gaussian process regression with 4 inputs and one output. The goal is to find the optimal X by performing a bayesian optimization over the fitted model.

I fit the model with the follow:

kernel = C(1.0, (1e-4, 1e4))*RBF(1.0,(1e-3,1e3))
model = GaussianProcessRegressor(kernel = kernel, n_restarts_optimizer = 10,optimizer='fmin_l_bfgs_b')           
model.fit(X,Y)

This so far has been what I am using to perform the optimization:

bayesian_optimization = gp_minimize(lambda x: -model.predict(np.array([x])[0]),   
                        [(0.0, 7.0), (0.0, 7.0),(0.0, 7.0),(0.0, 7.0)],                                        
                        acq_func="EI",                                         
                        n_calls=15,                                            
                        n_random_starts=5,                                     
                        random_state=1234)

I keep getting the following error:

ValueError: Expected 2D array, got 1D array instead:
array=[3.48364567 5.72486909 4.28478326 5.39951943].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

Not sure if it is possible at all to fix the problem, I already tried:


bayesian_optimization = gp_minimize(lambda x: -model.predict(np.array([x]).reshape(1,-1)[0]),   
                        [(0.0, 7.0), (0.0, 7.0),(0.0, 7.0),(0.0, 7.0)],                                        
                        acq_func="EI",                                         
                        n_calls=15,                                            
                        n_random_starts=5,                                     
                        random_state=1234)                                     

and

bayesian_optimization = gp_minimize(lambda x: -model.predict(np.array([x]).reshape(-1, 1)[0]),   
                        [(0.0, 7.0), (0.0, 7.0),(0.0, 7.0),(0.0, 7.0)],                                        
                        acq_func="EI",                                         
                        n_calls=15,                                            
                        n_random_starts=5,                                     
                        random_state=1234)     

but had no success. Any ideas on how to overcome such problem?

1

There are 1 best solutions below

0
On

In your first option you called model.predict(np.array([x])[0]), which doesn't work as you pass a 1D array to the predict method. Instead you need model.predict(np.array([x]))[0].

Might have been easier to spot this error by using a non-anonymous objective function, e.g.

def objective(x):
    x = np.array([x])
    return - model.predict(x)[0]