Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用numpy.clip将正值和负值转换为位字符串_Python_Arrays_Numpy_Clip - Fatal编程技术网

Python 使用numpy.clip将正值和负值转换为位字符串

Python 使用numpy.clip将正值和负值转换为位字符串,python,arrays,numpy,clip,Python,Arrays,Numpy,Clip,我想有效地将列表(或numpy数组)中的值转换为一个numpy位数组:在新数组中,负值应该变成0,在新数组中,正值应该变成1 例如: >> import numpy as np >> np.clip([1,2,3,-1,-2], a_min=0, a_max=1) array([1, 1, 1, 0, 0]) 但是,如果列表中有浮点数,此方法将保持其原样: >> np.clip([1,0.45,3,-1,-2], a_min=0, a_max=1) ar

我想有效地将列表(或numpy数组)中的值转换为一个numpy位数组:在新数组中,负值应该变成0,在新数组中,正值应该变成1

例如:

>> import numpy as np
>> np.clip([1,2,3,-1,-2],  a_min=0, a_max=1)
array([1, 1, 1, 0, 0])
但是,如果列表中有浮点数,此方法将保持其原样:

>> np.clip([1,0.45,3,-1,-2],  a_min=0, a_max=1)
array([ 1.  ,  0.45,  1.  ,  0.  ,  0.  ])

有没有规避这种行为的好方法?一种方法是对这些值进行四舍五入。但我希望所有正的都被赋值为1。如果我使用
np.around()
,这将是0.45->0。

要将大于0的所有内容映射到1(以及小于0的所有内容),可以使用:

请注意,
np.其中
返回一个带有dtype
int32(4字节整数)的数组,而
astype('i1')
返回一个带有dtype
int8(1字节整数)的数组

如果希望将这些二进制值打包到uint8中,可以使用:

或者,作为字符串:

In [60]: np.packbits((x > 0).astype('i1')).tostring()
Out[60]: '\xe0'

In [62]: bin(0xe0)
Out[62]: '0b11100000'

要将大于0的所有内容映射到1(以及小于0的所有内容),可以使用:

请注意,
np.其中
返回一个带有dtype
int32(4字节整数)的数组,而
astype('i1')
返回一个带有dtype
int8(1字节整数)的数组

如果希望将这些二进制值打包到uint8中,可以使用:

或者,作为字符串:

In [60]: np.packbits((x > 0).astype('i1')).tostring()
Out[60]: '\xe0'

In [62]: bin(0xe0)
Out[62]: '0b11100000'

很好,谢谢!当StackOverflow允许我的时候,我会接受这个答案!注意,如果性能是一个问题,我得到的是
(a>0)。astype('i1')
np快得多。其中(a>0,1,0)
。阵列越大,差异越大。对于100长度的阵列,我的速度快了2倍,对于10k长度的阵列,我的速度快了10倍。感谢@RogerFanNice的留言,谢谢!当StackOverflow允许我的时候,我会接受这个答案!注意,如果性能是一个问题,我得到的是
(a>0)。astype('i1')
np快得多。其中(a>0,1,0)
。阵列越大,差异越大。对于100长度的阵列,我的速度快了2倍,对于10k长度的阵列,我的速度快了10倍。感谢@RogerFan的注释
In [48]: x = np.array([1,0.45,3,-1,-2])

In [49]: np.packbits((x > 0).astype('i1'))
Out[49]: array([224], dtype=uint8)

In [50]: bin(224)
Out[50]: '0b11100000'
In [60]: np.packbits((x > 0).astype('i1')).tostring()
Out[60]: '\xe0'

In [62]: bin(0xe0)
Out[62]: '0b11100000'
In [21]: arr = np.array([1,0.45,3,-1,-2])

In [22]: np.ceil(arr.clip(0, 1))
Out[22]: array([ 1.,  1.,  1.,  0.,  0.])