0

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!

2
  • What is ML ???? Commented Mar 13, 2021 at 18:05
  • @pippo1980 Most likely machine learning. Commented Mar 13, 2021 at 18:07

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.