0

I am trying to create an estimated distribution function from the data in data.dat using Gaussian process regression and set it as the objective function of pso.but I keep getting the error below. I would like to create a function that returns LD when I pass x, but it doesn't work. In the case of pso, x is passed at the same time, so I split it into columns and return LD. I tried to define a function with the result in an empty "result".Might be wrong to put in empty data.

Code

import numpy as np
import pandas as pd
import pyswarms as ps
import GPy  
  
# define the random seed to fix
np.random.seed(0)  

# optimization conditions  
n_particles = 5
n = n_particles
iters = 10  
bounds = (np.array([10,6,2,12,38,0,3.6,4,8,0]),np.array([18,13,7,18,42,9,9,10,18,7.5]))#(min,max)  

# Determine hyperparameter
options = {"c1": 0.5, "c2": 0.3, "w":0.9}  

# Input the dimensions of design parameters
ndim = 10
Ndim = np.arange(ndim)  

# import the data
data = pd.read_csv('data.dat', header=None, sep=" ")                
data.columns = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"]
print(data.shape) # (96,12)
N = len(data)  

# import the input values to X and the output values to LD
X = data.iloc[:, 1:11]
fn = data.loc[:, "12"]  

# change the data type from 'list' to 'numpy.ndarray'
x = np.array(X, dtype=float)
Y = np.array(fn, dtype=float)  

# reshape the dimension od output from
y = Y.reshape(N, 1)  

# choose the kernel to regression
kernel = GPy.kern.RBF(ndim, ARD=True)  

# create a model from the data by Gaussian Progress Regression with a kernel
m = GPy.models.GPRegression(x, y, kernel)  

#define an objective function
def func2(x):
    print(x.shape) #(5,10)
    for i in range(n):
        xi = x[i,:]
        xi = xi.reshape(1,len(xi))
        print(xi.shape) #(1,10)
        LD = m.predict(xi, include_likelihood = False)
        # print('LD =',LD)#tuple
        # print('LD0 =',LD[0])#list

    result = np.empty((n,1))
    result[i, 0] = LD[0]
    print(result.shape) #(5,1)

    return result  

# start optimization
optimizer = ps.single.GlobalBestPSO(n_particles = n, dimensions = ndim, options = options, bounds = bounds)
cost, pos = optimizer.optimize (objective_func = func2 , iters = iters)  

Error

Traceback (most recent call last):
  File "e:\PSO\approx4pso.py", line 132, in <module>
    cost, pos = optimizer.optimize (objective_func = func2 , iters = iters)
  File "C:\Users\taku_\anaconda3\lib\site-packages\pyswarms\single\global_best.py", line 210, in optimize
    self.swarm.pbest_pos, self.swarm.pbest_cost = compute_pbest(self.swarm)
  File "C:\Users\taku_\anaconda3\lib\site-packages\pyswarms\backend\operators.py", line 69, in compute_pbest
    new_pbest_pos = np.where(~mask_pos, swarm.pbest_pos, swarm.position)
  File "<__array_function__ internals>", line 180, in where
ValueError: operands could not be broadcast together with shapes (5,10,5) (5,10) (5,10)  

How do I define a function?I don't have to stick to this approach, so if this code doesn't work, please let me know of other ways to define the function using Gaussian process regression.

stackman
  • 1
  • 1

0 Answers0