I have large numpy array and trying to create logical (1, 0) array from it. For example,
a=np.array((2.0,2.0,2.0,3.0,4.0,5.0,6.0,2.0))
a==2.0
array([ True, True, True, False, False, False, False, True], dtype=bool)
I want 1.0
(float/double) for all 2.0
s in a
. a==2.0
gives array of bool
, but not 1.0
float. How can I do it?
>>> (a == 2).astype(float)
array([1., 1., 1., 0., 0., 0., 0., 1.])
Try
(a==2.0) * 1
True multiplied by number will be equal to that number and False multiplied by number will be equal to 0
只需执行a = float(a)
,您也可以使用numpy
来转换类型
你可以试试这个
[1.0 if i else False for i in r]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.