• Register
0 votes
2k views

Problem :

I am getting below error:

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

How can I fix it?

6 5 3
6,930 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

9 7 4
38,600 points

Related questions

0 votes
1 answer 254 views
254 views
I was trying to write a programm using numpy and matplotlib. I am new to python programming. The code I wrote is: import numpy import matplotlib.pyplot def ThisFunction(a): return numpy.int(a) a = numpy.arange(3, 13.5, 0.7) matplotlib.pyplot.plot(a, ... 4, in ThisFunction return numpy.int(a) TypeError: only size-1 arrays can be converted to Python scalars how can I solve this problem?
asked Mar 15 LizzyM 6.1k points
1 vote
1 answer 35 views
35 views
Problem: I am doing a online course on python. I got an assginement from there. So I was trying to write a programm using numpy and matplotlib. I am new to python programming. The code I wrote is: import numpy import matplotlib.pyplot def ThisFunction(a): return ... .int(a) TypeError: only size-1 arrays can be converted to Python scalars Do anyone know why this error is happening, please help.
asked Apr 3 LizzyM 6.1k points
0 votes
1 answer 1.7K views
1.7K views
Problem : I am trying to convert my list into an array using Python, But I got below error ValueError: only one element tensors can be converted to Python scalars.
asked Oct 22, 2019 peterlaw 6.9k points