• Register
0 votes

Problem :

I am getting below error:

TypeError: only length-1 arrays can be converted to Python scalars

How can I fix it?

by (6.9k points)  

1 Answer

0 votes

Solution :

This 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.

PFB code will solve your issue

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))

You can skip the definition of f(x) and just pass np.int to the vectorize function: f2 = np.vectorize(np.int).

Further Readings:


by (36.1k points)  
edited by