📜  Python中的 numpy.unpackbits()

📅  最后修改于: 2022-05-13 01:55:15.674000             🧑  作者: Mango

Python中的 numpy.unpackbits()

numpy.unpackbits()是另一个在 numpy 中进行二进制操作的函数。它用于将 uint8 数组的元素解包为二进制值输出数组。

代码#1:

# Python program explaining
# numpy.unpackbits() function
  
# importing numpy
import numpy as geek
  
# creating input array using 
# array function
in_arr = geek.array([171,  16], dtype = geek.uint8)
print ("Input array : ", in_arr) 
  
# unpacking elements of an array
# using unpackbits() function
out_arr = geek.unpackbits(in_arr)
  
print ("Output unpacked array : ", out_arr)
输出 :
Input array :  [171  16]
Output unpacked array :  [1 0 1 0 1 0 1 1 0 0 0 1 0 0 0 0]

代码#2:

# Python program explaining
# numpy.unpackbits() function
  
# importing numpy
import numpy as geek
  
# creating input array using 
# array function
in_arr = geek.array([[ 64, 128], [ 17, 25]], dtype = geek.uint8)
print ("Input array : ", in_arr) 
  
# unpacking elements of an array
# using unpackbits() function
out_arr = geek.unpackbits(in_arr, axis = 0)
  
print ("Output unpacked array along axis 0 : ", out_arr) 
输出 :
Input array :  [[ 64 128]
 [ 17  25]]
Output unpacked array along axis 0 :  [[0 1]
 [1 0]
 [0 0]
 [0 0]
 [0 0]
 [0 0]
 [0 0]
 [0 0]
 [0 0]
 [0 0]
 [0 0]
 [1 1]
 [0 1]
 [0 0]
 [0 0]
 [1 1]]