• Register
0 votes
3.1k 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

Please log in or register to answer this question.

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 12 views
12 views
Problem: help me to fix this : typeerror: only length-1 arrays can be converted to python scalars.
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 393 views
393 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, 2020 LizzyM 6.1k points
0 votes
1 answer 26 views
26 views
Problem: f=np.loadtxt('Single Small Angle 1.txt',unpack=True,skiprows=2) g=np.loadtxt('Single Small Angle 5.txt',unpack=True,skiprows=2) x = f-g[:,:11944] t=range(len(x)) m=math.log10(abs(x)) np.polyfit(t,m) plt.plot(t,abs(x)) plt.show() I'm just not sure on how to fix my issue. It keeps saying: m=math.log10(abs(x)) TypeError: only length-1 arrays can be converted to Python scalars
asked Jan 7 Mashhoodch 13k points
1 vote
1 answer 90 views
90 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, 2020 LizzyM 6.1k points
0 votes
1 answer 7 views
7 views
Problem: I want to write a function that randomly picks elements from a training set, based on the bin probabilities provided. I divide the set indices to 11 bins, then create custom probabilities for them. bin_probs = [0.5, 0.3, 0.15, 0.04, 0.0025, 0.0025, 0.001, 0.001, ... It is 1-D, it is integer, and it is scalar. What am I missing? Note : I tried to pass indices with astype(int). Same error.
asked May 2 anika11 32.2k points
0 votes
1 answer 45 views
45 views
Problem: i will be thankful to the person who could help me in solve this problem? only one element tensors can be converted to python scalars.
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 3K views
3K 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
0 votes
1 answer 43 views
43 views
Problem: I have such Python code: 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() And such error: TypeError: only length-1 arrays can be converted to Python scalars How can I fix it?
asked Jan 10 Mashhoodch 13k points