user9875321__
user9875321__

Reputation: 357

scipy minimize ValueError: Index data must be 1-dimensional

I am trying to optimize the following function:

import numpy as np
import pandas as pd
def test(var,q,pts):
    eMin,eMax=var*(1-(1./q)**.5)**2,var*(1+(1./q)**.5)**2
    eVal=np.linspace(eMin,eMax,pts) # create space of eigenvalues
    pdf=q/(2*np.pi*var*eVal)*((eMax-eVal)*(eVal-eMin))**.5 # MP pdf based on eigenvalue space
    pdf=pd.Series(pdf,index=eVal)
    return pdf.sum()

The function works well: try in fact

test(0.5, 10, 1000)

But when I try to optimize it with scipy minimize:

from scipy.optimize import minimize
minimize(test, 0.5, (10,1000))

I get the following error message:

ValueError: Index data must be 1-dimensional

which in my understanding refers to the last line, i.e. pdf=pd.Series(pdf, index=eVal). However, I don't understand what is the problem here as the function works fine outside the optimization process.

Upvotes: 1

Views: 755

Answers (1)

Muhammad Mohsin Khan
Muhammad Mohsin Khan

Reputation: 1476

The problem lies in the eVal variable which is a 2D np.ndarray because var is passed as a 1D np.ndarray (for e.g., [0.5]) to the test function when minimize is used. Due to this, eMin and eMax are also 1D arrays and you end up getting eVal as a 2D np.ndarray. Index argument in pd.Series must be 1D. Therefore, you must flatten your eVal to 1D as follows:

eVal=np.linspace(eMin,eMax,pts).flatten()

which solves the above error.

Output:

>>> minimize(test, 0.5, (10,1000))
      fun: 0.06741138275083512
 hess_inv: array([[2.84420795e+08]])
      jac: array([-5.75464219e-06])
  message: 'Optimization terminated successfully.'
     nfev: 70
      nit: 34
     njev: 35
   status: 0
  success: True
        x: array([11715.27509055])

Upvotes: 2

Related Questions