久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將二進制(0 | 1)numpy轉換為整數或二進制字符串?

Convert binary (0|1) numpy to integer or binary-string?(將二進制(0 | 1)numpy轉換為整數或二進制字符串?)
本文介紹了將二進制(0 | 1)numpy轉換為整數或二進制字符串?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

是否有將二進制 (0|1) numpy 數組轉換為整數或二進制字符串的快捷方式?F.e.

Is there a shortcut to Convert binary (0|1) numpy array to integer or binary-string ? F.e.

b = np.array([0,0,0,0,0,1,0,1])   
  => b is 5

np.packbits(b)

有效,但僅適用于 8 位值..如果 numpy 是 9 個或更多元素,它會生成 2 個或更多 8 位值.另一種選擇是返回一個字符串 0|1 ...

works but only for 8 bit values ..if the numpy is 9 or more elements it generates 2 or more 8bit values. Another option would be to return a string of 0|1 ...

我目前做的是:

    ba = bitarray()
    ba.pack(b.astype(np.bool).tostring())
    #convert from bitarray 0|1 to integer
    result = int( ba.to01(), 2 )

太丑了!!!

推薦答案

一種方法是使用 dot-product2-powered 范圍數組 -

One way would be using dot-product with 2-powered range array -

b.dot(2**np.arange(b.size)[::-1])

示例運行 -

In [95]: b = np.array([1,0,1,0,0,0,0,0,1,0,1])

In [96]: b.dot(2**np.arange(b.size)[::-1])
Out[96]: 1285

或者,我們可以使用按位左移運算符來創(chuàng)建范圍數組,從而獲得所需的輸出,就像這樣 -

Alternatively, we could use bitwise left-shift operator to create the range array and thus get the desired output, like so -

b.dot(1 << np.arange(b.size)[::-1])

如果對時間感興趣 -

In [148]: b = np.random.randint(0,2,(50))

In [149]: %timeit b.dot(2**np.arange(b.size)[::-1])
100000 loops, best of 3: 13.1 μs per loop

In [150]: %timeit b.dot(1 << np.arange(b.size)[::-1])
100000 loops, best of 3: 7.92 μs per loop

<小時>

逆向處理

要檢索二進制數組,請使用 np.binary_repr 以及 np.fromstring -

To retrieve back the binary array, use np.binary_repr alongwith np.fromstring -

In [96]: b = np.array([1,0,1,0,0,0,0,0,1,0,1])

In [97]: num = b.dot(2**np.arange(b.size)[::-1]) # integer

In [98]: np.fromstring(np.binary_repr(num), dtype='S1').astype(int)
Out[98]: array([1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1])

這篇關于將二進制(0 | 1)numpy轉換為整數或二進制字符串?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 免费一级黄色录像 | 国产精品久久久久久久7777 | 日本午夜一区二区三区 | 日韩视频在线一区 | 麻豆精品国产91久久久久久 | 一区在线免费视频 | 中文字幕免费 | 精品国产一区二区三区观看不卡 | 9久久精品| 91九色视频 | 日本中出视频 | 国产一区精品 | 久久爆操 | 一级特黄a大片 | 国产精品久久久久久久久久 | 国产精品福利网站 | 欧美日韩在线免费观看 | 精品视频一区二区三区在线观看 | 97精品一区二区 | 特级做a爰片毛片免费看108 | 老司机狠狠爱 | 国产视频黄色 | 伦理午夜电影免费观看 | 在线视频一区二区 | 精品婷婷 | 国产成人午夜高潮毛片 | 亚洲精品在线国产 | 日韩精品一区二区三区在线观看 | 久久精品久久久 | 成人精品一区二区三区 | 中文字幕一区二区三区不卡 | 欧美精品一区二区免费视频 | 少妇无套高潮一二三区 | 欧美精品第三页 | 三级在线观看 | 操久久| 精品国产一区二区三区久久久蜜月 | 欧美三区 | 激情欧美日韩一区二区 | 久久久久久久一区 | 在线婷婷 |