## Example 1.

import numpy as np import matplotlib.pyplot as plt def f(x): return np.int(x) x = np.arange(1, 15.1, 0.1) plt.plot(x, f(x)) plt.show()

Answer 1:

The error “only length-1 arrays can be converted to Python scalars” is raised when the function expects a single value but you pass an array instead.

If you look at the call signature of **np.int,** you’ll see that it accepts a single value, not an array. In general, if you want to apply a function that accepts a single element to every element in an array, you can use **np.vectorize:**

import numpy as np import matplotlib.pyplot as plt def f(x): return np.int(x) f2 = np.vectorize(f) x = np.arange(1, 15.1, 0.1) plt.plot(x, f2(x)) plt.show()