0

I am trying to optimize my Random forest regression model using a particle swarm optimizer to minimize the prediction error. But getting this error:UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U33'), dtype('<U33')) -> None

Can anyone please help me with this. I really appreciate any help you can provide.

My dataset contains 24 independent variables (X) and one dependent variable (y).

CODE:

 import numpy as np
 import pandas as pd
 import re
 import os 
 import random

 import seaborn as sns
 import matplotlib.pyplot as plt
 from matplotlib import animation, rc
%matplotlib inline

from matplotlib.animation import FuncAnimation


from sklearn.model_selection import cross_val_predict, train_test_split
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score,  mean_absolute_percentage_error


from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20,  random_state=1)

from sklearn.ensemble import RandomForestRegressor
RFR = RandomForestRegressor()
RFR.fit(X_train, y_train)

######################### Particle swarm optimization ##################################

#### error rate
#### The objective is to find minimum error 
def fitness_function(xtest, ytest):

# Prediction
    ypred   = RFR.predict(X_test)
    error   = mean_squared_error(y_test, ypred)
    #r2 = r2_score(ytest, ypred)
    return f'The error is: {error}'

from matplotlib import animation
import random

####### velocity #######################################
def update_velocity(particle, velocity, pbest, gbest, w_min=0.5, max=1.0, c=0.1):
  # Initialise new velocity array
  num_particle = len(particle)
  new_velocity = np.array([0.2 for i in range(num_particle)])
  # Randomly generate r1, r2 and inertia weight from normal distribution
  r1 = random.uniform(0,max)
  r2 = random.uniform(0,max)
  w = random.uniform(w_min,max)
  c1 = c
  c2 = c
  
  # Calculate new velocity
  for i in range(num_particle):
    new_velocity[i] = w*velocity[i] + c1*r1*(pbest[i]-particle[i])+c2*r2*(gbest[i]-particle[i])
  return new_velocity

############## update position ##############
def update_position(particle, velocity):
  # Move particles by adding velocity
  new_particle = particle + velocity
  return new_particle

######################################### PSO Main function  ###################################

def pso_2d(population, dimension, position_min, position_max, generation, fitness_criterion):
  # Initialization
  # Population
  particles = [[np.random.uniform(position_min[0:24], position_max[0 :24]) for j in range(population)] for i in range(dimension)] # generating random particle position
  # Particle's best position



pbest_position = particles   # personal best position

  # Fitness
  pbest_fitness = [fitness_function(p[0:24],p[1]) for p in particles]  # personal best fitness
  # Index of the best particle
  # np.reshape(np.ravel(p[0]), (2, 31))
  gbest_index = np.argmin(pbest_fitness)
  # Global best particle position



gbest_position = pbest_position[gbest_index]

  
  # Velocity (starting from 0 speed)
  velocity = [[0.0 for j in range(dimension)] for i in range(population)]




# Loop for the number of generation
  for t in range(generation):
    # Stop if the average fitness value reached a predefined success criterion
    if np.average(pbest_fitness) <= fitness_criterion:
      break
    else:
      for n in range(population):
        # Update the velocity of each particle
        velocity[n] = update_velocity(particles[n], velocity[n], pbest_position[n], gbest_position)
        # Move the particles to new position
        particles[n] = update_position(particles[n], velocity[n])

    # Calculate the fitness value
    pbest_fitness = [fitness_function(p[0:24],p[1]) for p in particles]

    # Find the index of the best particle
    gbest_index = np.argmin(pbest_fitness)
    
    # Update the position of the best particle
    gbest_position = pbest_position[gbest_index]  

    # Print the results
  print('Global Best Position: ', gbest_position)
  print('Best Fitness Value: ', min(pbest_fitness))
  print('Average Particle Best Fitness Value: ', np.average(pbest_fitness))
  print('Number of Generation: ', t)  

position_min = [-0.44306155, -0.52971118, -0.10311188, -0.60053201, -0.78198029,
        -0.37737778, -0.14371436, -0.01623235, -0.88660182, -0.06182274,
        -0.30084403, -0.98080838, -0.11787062, -0.84172055, -0.709991  ,
         -0.9841236 , -0.32976052, -0.26586302, -0.87641669, -0.23728611,
         -0.08874495, -0.03091284, -0.29987714, -0.96795309]
        
position_max = [0.44306155, 0.52971118, 0.10311188, 0.60053201, 0.78198029,
         0.37737778, 0.14371436, 0.01623235, 0.88660182, 0.06182274,
         0.30084403, 0.98080838, 0.11787062, 0.84172055, 0.709991  ,
         0.9841236 , 0.32976052, 0.26586302, 0.87641669, 0.23728611,
         0.08874495, 0.03091284, 0.29987714, 0.96795309]

pso_2d(100, 24, position_min, position_max, 400, 10e-4)

Output: UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U33'), dtype('<U33')) -> None

    
    
  • Hi Swati, any reason to stick to PSO? This answer might guide you https://stackoverflow.com/a/63548159/4551984 – quest Sep 01 '22 at 11:42
  • Thanks for your suggestions, I have gone through this code, but couldn't find it helpful to apply in my case. It would be helpful if you suggest me where I am doing wrong. I want to reduce the error between the predicted value and actual value, to avoid the local minima and to obtain the best minima. – Swati Singh Sep 02 '22 at 05:01

0 Answers0