Here is the code in question:
arr = ['-0.944', '0.472', '0.472']
charges = [np.float64(i) for i in arr] # [-0.944, 0.472, 0.472]
charges = np.ndarray(charges)
The error is thrown on the last step, where the list is casted to an ndarray. Assigning dtype=np.float64 in ndarray does not change the error. What is wrong with this snippet of code?
Numpy 1.14, Python 3.6.1
The first argument to np.ndarray is the shape, which is usually a tuple of integers.
You should not use the low-level constructor np.ndarray. Correct interface is np.array, and take it directly from the strings without a list comprehension first:
>>> arr = ['-0.944', '0.472', '0.472']
>>> np.array(arr, dtype=np.float64)
array([-0.944, 0.472, 0.472])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With