久久久久久久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

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

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中的感興趣區域周圍繪制一個矩形)
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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 五月婷婷激情网 | 羞羞视频在线网站观看 | caoporn免费在线视频 | 中文字幕av高清 | 久久久久国产一区二区三区 | 日韩在线免费 | av在线免费观看网站 | 日韩中文字幕网 | 一级免费在线视频 | 香蕉av免费 | 久草日韩 | 中文在线a在线 | 亚洲午夜视频 | 欧美亚洲视频 | 中文字幕国产一区 | 亚洲网址在线观看 | 91电影在线播放 | 日韩中文一区二区三区 | 亚洲h色 | 黄色av网站在线观看 | 天天操夜夜操 | 中文字幕一区在线 | 成人免费观看男女羞羞视频 | 日本高清中文字幕 | 国产亚洲精品久久久久动 | 九九伊人sl水蜜桃色推荐 | 91视频中文| 国产乱性| 久久久蜜桃一区二区人 | 国产精品久久久久久福利一牛影视 | 欧洲av一区| 欧美一区二区三区四区五区无卡码 | 国产高清在线 | 99av成人精品国语自产拍 | 午夜伦理影院 | 亚洲福利视频一区二区 | av毛片在线播放 | 91视频a | 国久久 | 国产精品成av人在线视午夜片 | 91精品国产综合久久久亚洲 |