Issue
I have a list of strings like the following:
['0.20115899', '0.111678', '0.10674', '0.05564842', '-0.09271969', '-0.02292056', '-0.04057575', '0.2019901', '-0.05368654', '-0.1708179']
['-2.17182860e-01', '-1.04081273e-01', '7.75325894e-02', '7.51972795e-02', '-7.11168349e-02', '-4.75254208e-02', '-2.94160955e-02']
etc.
etc.
List's name is data_det
. I did the following to find the types:
for item in data_det:
print(type(item))
for it in item:
print(type(it))
I got
<class 'list'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
etc.
I tried to convert it into ndarray
.
data_det = numpy.asarray(data_det, dtype=np.float)
But got the error:
return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.
Ideally I want each value to be converted to float. How can this be accomplished?
Solution
Try this :
import numpy as np
l1 = ['0.20115899', '0.111678', '0.10674', '0.05564842', '-0.09271969', '-0.02292056', '-0.04057575', '0.2019901', '-0.05368654', '-0.1708179']
l2 = ['-2.17182860e-01', '-1.04081273e-01', '7.75325894e-02', '7.51972795e-02', '-7.11168349e-02', '-4.75254208e-02', '-2.94160955e-02']
l1 = np.array([float(i) for i in l1])
l2 = np.array([float(i) for i in l2])
print(l1.dtype)
Output :
float64
Answered By - Arkistarvh Kltzuonstev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.