简体   繁体   中英

Error 'numpy.ndarray' object is not callable

I'm a student learning ML with Python. I have the following code which generates a np.array, uses it as a parameter of the function and creates its plot:

import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize as opt

def _y_(x):
    return np.sin(x/5)*np.exp(x/10) + 5*np.exp(-x/2)

x = np.arange(1, 30.01, 0.01)
y = _y_(x)
plt.plot(x, y)
plt.show()

min1 = opt.minimize(y, x0=2)
print(res1)
print()

min2 = opt.minimize(y, x0=15)
print(min2)
print()

min3 = opt.minimize(y, x0=30)
print(min3)
print()

But when I try to find the minimum of the function for three different points, I get an error:

'numpy.ndarray' object is not callable

It occurs on this line:

min1 = opt.minimize(y, x0=2)

Please, help me fix it!

The minimize function of scipy.optimize expects a callable argument (a function) as its first argument, as per its documentation.

You are providing the array y as input argument. Instead, you should provide the function that calculates y from x , so you should provide _y_ . The code min1 = opt.minimize(_y_, x0=2) works without issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM