scipy.optimize.minimize ValueError: setting an array element with a sequence

时间:2018-02-01 18:38:00

标签: python numpy scipy

I'm trying to minimize a function with scipy.optimize.minimize. I have a list of lists as input

X = numpy.array(features) #Convert to 2D numpy array
X = numpy.insert(X, 0, 1, axis=1)
y = numpy.transpose( numpy.matrix(values) )
w = numpy.random.random( (X.shape[0], 1) )

fun = lambda w: sum(numpy.power( y-numpy.matmul( numpy.transpose(w) , X), 2))
w = scipy.optimize.minimize(fun, w) 

But I get the following error on the last line:

ValueError: setting an array element with a sequence.

This means there is something wrong with my conversions from list (values) to 1 x N matrix (y), and from list of list (features) to matrix (X). Below are 10 entries for each.

features =  [[1.865e+01 1.760e+01 1.237e+02 1.076e+03 1.099e-01 1.686e-01 1.974e-01
  1.009e-01 1.907e-01 6.049e-02 6.289e-01 6.633e-01 4.293e+00 7.156e+01
  6.294e-03 3.994e-02 5.554e-02 1.695e-02 2.428e-02 3.535e-03 2.282e+01
  2.132e+01 1.506e+02 1.567e+03 1.679e-01 5.090e-01 7.345e-01 2.378e-01
  3.799e-01 9.185e-02]
 [8.196e+00 1.684e+01 5.171e+01 2.019e+02 8.600e-02 5.943e-02 1.588e-02
  5.917e-03 1.769e-01 6.503e-02 1.563e-01 9.567e-01 1.094e+00 8.205e+00
  8.968e-03 1.646e-02 1.588e-02 5.917e-03 2.574e-02 2.582e-03 8.964e+00
  2.196e+01 5.726e+01 2.422e+02 1.297e-01 1.357e-01 6.880e-02 2.564e-02
  3.105e-01 7.409e-02]
 [1.317e+01 1.866e+01 8.598e+01 5.346e+02 1.158e-01 1.231e-01 1.226e-01
  7.340e-02 2.128e-01 6.777e-02 2.871e-01 8.937e-01 1.897e+00 2.425e+01
  6.532e-03 2.336e-02 2.905e-02 1.215e-02 1.743e-02 3.643e-03 1.567e+01
  2.795e+01 1.028e+02 7.594e+02 1.786e-01 4.166e-01 5.006e-01 2.088e-01
  3.900e-01 1.179e-01]
 [1.205e+01 1.463e+01 7.804e+01 4.493e+02 1.031e-01 9.092e-02 6.592e-02
  2.749e-02 1.675e-01 6.043e-02 2.636e-01 7.294e-01 1.848e+00 1.987e+01
  5.488e-03 1.427e-02 2.322e-02 5.660e-03 1.428e-02 2.422e-03 1.376e+01
  2.070e+01 8.988e+01 5.826e+02 1.494e-01 2.156e-01 3.050e-01 6.548e-02
  2.747e-01 8.301e-02]
 [1.349e+01 2.230e+01 8.691e+01 5.610e+02 8.752e-02 7.698e-02 4.751e-02
  3.384e-02 1.809e-01 5.718e-02 2.338e-01 1.353e+00 1.735e+00 2.020e+01
  4.455e-03 1.382e-02 2.095e-02 1.184e-02 1.641e-02 1.956e-03 1.515e+01
  3.182e+01 9.900e+01 6.988e+02 1.162e-01 1.711e-01 2.282e-01 1.282e-01
  2.871e-01 6.917e-02]
 [1.176e+01 2.160e+01 7.472e+01 4.279e+02 8.637e-02 4.966e-02 1.657e-02
  1.115e-02 1.495e-01 5.888e-02 4.062e-01 1.210e+00 2.635e+00 2.847e+01
  5.857e-03 9.758e-03 1.168e-02 7.445e-03 2.406e-02 1.769e-03 1.298e+01
  2.572e+01 8.298e+01 5.165e+02 1.085e-01 8.615e-02 5.523e-02 3.715e-02
  2.433e-01 6.563e-02]
 [1.364e+01 1.634e+01 8.721e+01 5.718e+02 7.685e-02 6.059e-02 1.857e-02
  1.723e-02 1.353e-01 5.953e-02 1.872e-01 9.234e-01 1.449e+00 1.455e+01
  4.477e-03 1.177e-02 1.079e-02 7.956e-03 1.325e-02 2.551e-03 1.467e+01
  2.319e+01 9.608e+01 6.567e+02 1.089e-01 1.582e-01 1.050e-01 8.586e-02
  2.346e-01 8.025e-02]
 [1.194e+01 1.824e+01 7.571e+01 4.376e+02 8.261e-02 4.751e-02 1.972e-02
  1.349e-02 1.868e-01 6.110e-02 2.273e-01 6.329e-01 1.520e+00 1.747e+01
  7.210e-03 8.380e-03 1.311e-02 8.000e-03 1.996e-02 2.635e-03 1.310e+01
  2.133e+01 8.367e+01 5.272e+02 1.144e-01 8.906e-02 9.203e-02 6.296e-02
  2.785e-01 7.408e-02]
 [1.822e+01 1.870e+01 1.203e+02 1.033e+03 1.148e-01 1.485e-01 1.772e-01
  1.060e-01 2.092e-01 6.310e-02 8.337e-01 1.593e+00 4.877e+00 9.881e+01
  3.899e-03 2.961e-02 2.817e-02 9.222e-03 2.674e-02 5.126e-03 2.060e+01
  2.413e+01 1.351e+02 1.321e+03 1.280e-01 2.297e-01 2.623e-01 1.325e-01
  3.021e-01 7.987e-02]
 [1.510e+01 2.202e+01 9.726e+01 7.128e+02 9.056e-02 7.081e-02 5.253e-02
  3.334e-02 1.616e-01 5.684e-02 3.105e-01 8.339e-01 2.097e+00 2.991e+01
  4.675e-03 1.030e-02 1.603e-02 9.222e-03 1.095e-02 1.629e-03 1.810e+01
  3.169e+01 1.177e+02 1.030e+03 1.389e-01 2.057e-01 2.712e-01 1.530e-01
  2.675e-01 7.873e-02]]


X =  [[1.000e+00 1.865e+01 1.760e+01 1.237e+02 1.076e+03 1.099e-01 1.686e-01
      1.974e-01 1.009e-01 1.907e-01 6.049e-02 6.289e-01 6.633e-01 4.293e+00
      7.156e+01 6.294e-03 3.994e-02 5.554e-02 1.695e-02 2.428e-02 3.535e-03
      2.282e+01 2.132e+01 1.506e+02 1.567e+03 1.679e-01 5.090e-01 7.345e-01
      2.378e-01 3.799e-01 9.185e-02]
     [1.000e+00 8.196e+00 1.684e+01 5.171e+01 2.019e+02 8.600e-02 5.943e-02
      1.588e-02 5.917e-03 1.769e-01 6.503e-02 1.563e-01 9.567e-01 1.094e+00
      8.205e+00 8.968e-03 1.646e-02 1.588e-02 5.917e-03 2.574e-02 2.582e-03
      8.964e+00 2.196e+01 5.726e+01 2.422e+02 1.297e-01 1.357e-01 6.880e-02
      2.564e-02 3.105e-01 7.409e-02]
     [1.000e+00 1.317e+01 1.866e+01 8.598e+01 5.346e+02 1.158e-01 1.231e-01
      1.226e-01 7.340e-02 2.128e-01 6.777e-02 2.871e-01 8.937e-01 1.897e+00
      2.425e+01 6.532e-03 2.336e-02 2.905e-02 1.215e-02 1.743e-02 3.643e-03
      1.567e+01 2.795e+01 1.028e+02 7.594e+02 1.786e-01 4.166e-01 5.006e-01
      2.088e-01 3.900e-01 1.179e-01]
     [1.000e+00 1.205e+01 1.463e+01 7.804e+01 4.493e+02 1.031e-01 9.092e-02
      6.592e-02 2.749e-02 1.675e-01 6.043e-02 2.636e-01 7.294e-01 1.848e+00
      1.987e+01 5.488e-03 1.427e-02 2.322e-02 5.660e-03 1.428e-02 2.422e-03
      1.376e+01 2.070e+01 8.988e+01 5.826e+02 1.494e-01 2.156e-01 3.050e-01
      6.548e-02 2.747e-01 8.301e-02]
     [1.000e+00 1.349e+01 2.230e+01 8.691e+01 5.610e+02 8.752e-02 7.698e-02
      4.751e-02 3.384e-02 1.809e-01 5.718e-02 2.338e-01 1.353e+00 1.735e+00
      2.020e+01 4.455e-03 1.382e-02 2.095e-02 1.184e-02 1.641e-02 1.956e-03
      1.515e+01 3.182e+01 9.900e+01 6.988e+02 1.162e-01 1.711e-01 2.282e-01
      1.282e-01 2.871e-01 6.917e-02]
     [1.000e+00 1.176e+01 2.160e+01 7.472e+01 4.279e+02 8.637e-02 4.966e-02
      1.657e-02 1.115e-02 1.495e-01 5.888e-02 4.062e-01 1.210e+00 2.635e+00
      2.847e+01 5.857e-03 9.758e-03 1.168e-02 7.445e-03 2.406e-02 1.769e-03
      1.298e+01 2.572e+01 8.298e+01 5.165e+02 1.085e-01 8.615e-02 5.523e-02
      3.715e-02 2.433e-01 6.563e-02]
     [1.000e+00 1.364e+01 1.634e+01 8.721e+01 5.718e+02 7.685e-02 6.059e-02
      1.857e-02 1.723e-02 1.353e-01 5.953e-02 1.872e-01 9.234e-01 1.449e+00
      1.455e+01 4.477e-03 1.177e-02 1.079e-02 7.956e-03 1.325e-02 2.551e-03
      1.467e+01 2.319e+01 9.608e+01 6.567e+02 1.089e-01 1.582e-01 1.050e-01
      8.586e-02 2.346e-01 8.025e-02]
     [1.000e+00 1.194e+01 1.824e+01 7.571e+01 4.376e+02 8.261e-02 4.751e-02
      1.972e-02 1.349e-02 1.868e-01 6.110e-02 2.273e-01 6.329e-01 1.520e+00
      1.747e+01 7.210e-03 8.380e-03 1.311e-02 8.000e-03 1.996e-02 2.635e-03
      1.310e+01 2.133e+01 8.367e+01 5.272e+02 1.144e-01 8.906e-02 9.203e-02
      6.296e-02 2.785e-01 7.408e-02]
     [1.000e+00 1.822e+01 1.870e+01 1.203e+02 1.033e+03 1.148e-01 1.485e-01
      1.772e-01 1.060e-01 2.092e-01 6.310e-02 8.337e-01 1.593e+00 4.877e+00
      9.881e+01 3.899e-03 2.961e-02 2.817e-02 9.222e-03 2.674e-02 5.126e-03
      2.060e+01 2.413e+01 1.351e+02 1.321e+03 1.280e-01 2.297e-01 2.623e-01
      1.325e-01 3.021e-01 7.987e-02]
     [1.000e+00 1.510e+01 2.202e+01 9.726e+01 7.128e+02 9.056e-02 7.081e-02
      5.253e-02 3.334e-02 1.616e-01 5.684e-02 3.105e-01 8.339e-01 2.097e+00
      2.991e+01 4.675e-03 1.030e-02 1.603e-02 9.222e-03 1.095e-02 1.629e-03
      1.810e+01 3.169e+01 1.177e+02 1.030e+03 1.389e-01 2.057e-01 2.712e-01
      1.530e-01 2.675e-01 7.873e-02]]

values =  [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]

y = [[0]
     [0]
     [0]
     [0]
     [0]
     [1]
     [1]
     [1]
     [1]
     [1]] 

Any ideas on how this can be fixed? I have read other answers that get the same exception, but they are generated from other libraries (like cv2).

1 个答案:

答案 0 :(得分:1)

在numpy矩阵的python中使用sum将仅沿轴相加并返回另一个矩阵。您应该使用numpy.sum将矩阵中的所有元素加到标量中。

fun = lambda w: numpy.sum(numpy.power( y-numpy.matmul( numpy.transpose(w) , X), 2))