0
votes

I am trying to use to minimize functionality in scipy.optimize. I am trying to varying a matrix (10 by 6) of weights as the input parameter of the function. However, when I attempt to perform the minimization, the matrix changes into a 1-dimensional vector preventing the matrices in the function from aligning. As shown in the Error msg below, my 10 by 6 matrix is being converted to a vector.

Has anyone had similar issues with scipy.optimize in the past? Any input would be much appreciated.

ValueError: shapes (2364,10) and (60,) not aligned: 10 (dim 1) != 60 (dim 0)

1
can you show us your code? - Andre
In general optimization routines take vectors as inputs, not multidimensional arrays. The usual way around this is to add some reshaping before and after the optimization calls. - Julien

1 Answers

1
votes

Similar to @Julien Brenu said above, in general optimization routines take vectors as inputs. So you should write your function to take a vector as an input, to quickly construct it into a 10 x 6 matrix.

def cool_func(parameter_guess):
  parameter_matrix = parameter_guess.reshape(10,6)
  return parameter_matrix

You said that the matrix are weights. You can't have negative weights, right? The optimizer does not explicitly know this - you have two ways around this:

  1. Use "COBYLA" method - http://docs.scipy.org/doc/scipy/reference/optimize.minimize-cobyla.html which allows you declare constraints.

  2. Do not input the vector parameter_guess into cool_func(). Instead enter np.log(parameter_guess) into cool_func() and immediately np.exp(parameter_guess) to recover it. You can do the same thing by raising it to a power, but this can drive the optimizer to consider insanely high values.