0

I've defined a function that references an array and broadcasts variables across that array. When the function is run, it works fine. However, when I attempt to use scipy.minimize to minimize the function I get the following error:

operands could not be broadcast together with shapes (75,) (2,)

Any help would be much appreciated!

import numpy as np
from scipy.optimize import minimize

a = [2, 4, 6, 5, 2]
b = 3
c = 4

def mult(x):
    target = np.array(np.array(a) * x) + x    
    mean = np.mean(target)
    sd = np.std(target)
    mean_ch = np.abs(mean / b - 1)
    sd_ch = np.abs(sd / c - 1)
    total_ch = mean_ch + sd_ch
    return total_ch

x0 = [2, 4]
minimize(mult, x0)
  • 1
    Could you modify your code snippet to be a fully self-contained example that we can copy-paste and run as is? E.g., specify all imports and make some dummy data for `wt`? https://stackoverflow.com/help/minimal-reproducible-example – Ahmed Fasih Nov 26 '19 at 01:14
  • In your example, `np.array(a)` is a 5-long vector while your `x0` is a 2-long vector. What exactly do you want to accomplish with `np.array(a) * x`? Note how `mult(x0)` doesn't even work because Numpy can't multiply a 5-vector with a 2-vector. `mult(np.array(x0)[:, np.newaxis])` does work but that's because Numpy multiplies a 5-vector with a 2x1 array yielding a 5x2 array—none of these things is what you want. Fix your `mult` function so it operates on `x0` and the rest should follow. – Ahmed Fasih Nov 27 '19 at 01:18

0 Answers0