• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
41 views

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))
plt.show()

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

Further Readings:

https://github.com/tyiannak/pyAudioAnalysis/issues/190

by (21.9k points)  
edited by
...