Typeerror: Only Length-1 Arrays Can Be Converted to Python Scalars While Plot Showing

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

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.

np.int was an alias for the built-in int, which is deprecated in numpy v1.20. The argument for int should be a scalar and it does not accept array-like objects. In general, if you want to apply a function to each element of the array, you can use np.vectorize:

import numpy as np
import matplotlib.pyplot as plt

def f(x):
return 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 the function int to the vectorize function: f2 = np.vectorize(int).

Note that np.vectorize is just a convenience function and basically a for loop. That will be inefficient over large arrays. Whenever you have the possibility, use truly vectorized functions or methods (like astype(int) as @FFT suggests).

TypeError: only size-1 arrays can be converted to Python scalars when trying to plot a function

I think the x value in the formula only applies for one value of x, and since you have multiple x in a form of a list, you have to iterate through each of them (for example, using for xval in x:), perform the calculation and append the calculated value to the values list

from math import cos, sin, pi
import numpy as np
import matplotlib.pyplot as plt

def f(x):
values = []
for xval in x:
s = 0
for n in range(1, 6, 1):
s += -((2/(n*pi))*(((cos((n*pi)/2))-1)*(sin((n/2)*xval))))
values.append(s * -1)

return values

x = np.linspace(-2*pi, 6*pi, 500)
plt.plot(f(x))
plt.show()

only length-1 arrays can be converted to Python scalars' error

The problem is that you are using math.exp that expects a Python scalar, for example:

>>> import numpy as np
>>> import math
>>> math.exp(np.arange(3))

Traceback (most recent call last):
File "path", line 3331, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-10-3ac3b9478cd5>", line 1, in <module>
math.exp(np.arange(3))
TypeError: only size-1 arrays can be converted to Python scalars

Use np.exp instead:

def func1(x):
f1 = np.exp(x - 2) + x ** 3 - x
return f1

The difference between np.exp and math.exp is that math.exp works with Python's numbers (floats and integers) while np.exp can work with numpy arrays. In your code the argument x is a numpy array, hence the error.

getting 'only length-1 arrays can be converted to Python scalars' error

Just change
plt.plot(x,sin(x**2))
to
plt.plot(x,np.sin(x**2))

TypeError: only size-1 arrays can be converted to Python scalars in simple python code

Did you check the shape and dtype of arr_data and selected_pixel_data? Tell us! Rather than searching the web for "like sounding errors", focus on understanding your data. If necessary, construct a simpler case. The fact that your code is "simple" does not reduce the chance that you'll get an error!.

I can reproduce your error message with

In [14]: res = np.zeros((2,3))
In [15]: res[0,0] = np.arange(2)
TypeError: only size-1 arrays can be converted to Python scalars

The above exception was the direct cause of the following exception:
Traceback (most recent call last):
Input In [15] in <cell line: 1>
res[0,0] = np.arange(2)
ValueError: setting an array element with a sequence.

res[0,0] is the slot for one number. np.arange(2) is 2 numbers, a "sequence". Do you see the mismatch?

edit

With i,n,m scalars and

shape = (597616, 2)
shape = (1064, 590)

single value slot: result_arr[m,n]

 selected_pixels_list[i] # (2,) shape
arr[_] # (2,590) shape

You can't put that 2d array in a single number slot.

You need to rethink the indexing and assignment.

edit

What does the flattening do?

shape = (597616*2,)
shape = (1064*590,)

single value slot: result_arr[m,n]

 selected_pixels_list[i] # 1 element
arr[_] # 1 element

I works, but is that right? What's the significance of the size 2 shape? I suspect that is wrong

Another possibility is to interpret the 2 columns of selected_pixels_list as i,j indices of arr:

k, l = selected_pixels_list[i]   # 2 numbers
arr[k,l] # 1 element

How should I fix it? Only size-1 arrays can be converted to Python scalars

I have fixed the error with from scipy.special import factorial


import matplotlib.pyplot as plt
import numpy as np
from scipy.special import factorial

def g(x):
return np.exp(-10)*pow(10,x)/factorial(x)

x=np.linspace(0,100)
plt.plot(x,g(x))
plt.show()

The Stirling's Approximation is not necessary. Thank you.

Distribution



Related Topics



Leave a reply



Submit