. Advertisement .
..3..
. Advertisement .
..4..
Hello everyone, I need your help. I am attempting to write a python that returns error. I don’t know what I’m doing wrong but the “TypeError: Only Size-1 Arrays Can Be Converted To Python Scalars” shows up. My detail code is:
import numpy as np
x = np.array([1, 2, 3, 4])
x = np.float(x)
TypeError: only size-1 arrays can be converted to Python scalars
Please guide me how to solve this problem. Thank you so much!
The cause: Because you’re utilizing Single Conversion Functions. These Numpy functions take a single Numpy element as input and alter its datatype internally. Such methods will throw an error if you try to send an integer array from Numpy to a parameter.
Solution: To fix the error, you should use Numpy Vectorize Function. Between the algorithm and the methods, you can put
numpy.vectorize()
. This technique handles a Numpy array like a python map function, avoids any type errors, and transforms all the values tofloat
.Output: