I have the following code
trainX.append(x)
labelY.append([personData.isPerson,personData.isntPerson,personData.xmin,personData.ymin,personData.xmax,personData.ymax])
Where x is image loaded using
x = cv2.imread(PATH_TO_IMG + personData.path,3)
However i want both x and array passed to labelY to be stored as float32
I have tried following
trainX = np.array(trainX).asType("float32")
labelY = np.array(labelY).asType("float32")
or even
labelY.append([personData.isPerson,personData.isntPerson,personData.xmin,personData.ymin,personData.xmax,personData.ymax].asType("float32"))
neither of them works. What is the right way to convert the value into float?
Thanks
答案 0 :(得分:0)
列表有append
方法,numpy数组没有。
alist.append(1)
就地修改alist
,并且不返回任何内容(实际上None
)。 None
没有astype
方法。
In [19]: alist=[]
In [20]: x = alist.append(1)
In [21]: x # None
In [22]: alist
Out[22]: [1]
In [23]: alist.append(2).astype('float')
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-23-7123155bd568> in <module>()
----> 1 alist.append(2).astype('float')
AttributeError: 'NoneType' object has no attribute 'astype'
In [24]: alist
Out[24]: [1, 2]
列表没有astype
方法:
In [25]: alist.astype(float)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-25-d0fc5f951ddf> in <module>()
----> 1 alist.astype(float)
AttributeError: 'list' object has no attribute 'astype'
我可以将列表转换为数组:
In [26]: np.array(alist).astype(float)
Out[26]: array([1., 2.])
如果
trainX = np.array(trainX).asType("float32")
不起作用,那么你需要告诉我们错误。 DOES NOT WORK
不是描述问题的有用方法!
以下是astype
不起作用的数组示例:
In [27]: alist.append([3,4])
In [28]: alist
Out[28]: [1, 2, [3, 4]]
In [29]: np.array(alist)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-29-7512d762195a> in <module>()
----> 1 np.array(alist)
ValueError: setting an array element with a sequence.
In [30]: np.array(alist,object)
Out[30]: array([1, 2, list([3, 4])], dtype=object)
In [31]: _.astype(float)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-31-b12cc607d111> in <module>()
----> 1 _.astype(float)
ValueError: setting an array element with a sequence.
注意错误信息和dtype。