- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Pack the elements of a binary-valued Numpy array into bits in a uint8 array over negative axis

To pack the elements of a binary-valued array into bits in a uint8 array, use the **numpy.packbits()** method in Python Numpy. The result is padded to full bytes by inserting zero bits at the end. The axis is set using the axis parameter. The axis is the dimension over which bit-packing is done. We have set negative axis.

The axis is the dimension over which bit-packing is done. None implies packing the flattened array. The bitorder is the order of the input bits. 'big' will mimic bin(val), [0, 0, 0, 0, 0, 0, 1, 1] ⇒ 3 = 0b00000011, 'little' will reverse the order so [1, 1, 0, 0, 0, 0, 0, 0] ⇒ 3. Defaults to 'big'.

The function packbits() returns the array of type uint8 whose elements represent bits corresponding to the logical (0 or nonzero) value of the input elements. The shape of packed has the same number of dimensions as the input.

## Steps

At first, import the required library −

import numpy as np

Create a 3d array −

arr = np.array([[ [1,0,1], [0,1,0]], [ [1,1,0], [0,0,1]], [ [1, 1, 0],[0, 0, 1] ]])

Displaying our array −

print("Array...

",arr)

Get the datatype −

print("

Array datatype...

",arr.dtype)

Get the dimensions of the Array −

print("

Array Dimensions...

",arr.ndim)

Get the shape of the Array −

print("

Our Array Shape...

",arr.shape)

Get the number of elements of the Array −

print("

Elements in the Array...

",arr.size)

To pack the elements of a binary-valued array into bits in a uint8 array, use the numpy.packbits() method. The result is padded to full bytes by inserting zero bits at the end −

res = np.packbits(arr, axis = -1) print("

Result...

",res)

## Example

import numpy as np # Create a 3d array arr = np.array([[ [1,0,1], [0,1,0]],[ [1,1,0], [0,0,1]],[[1, 1, 0],[0, 0, 1] ]]) # Displaying our array print("Array...

",arr) # Get the datatype print("

Array datatype...

",arr.dtype) # Get the dimensions of the Array print("

Array Dimensions...

",arr.ndim) # Get the shape of the Array print("

Our Array Shape...

",arr.shape) # Get the number of elements of the Array print("

Elements in the Array...

",arr.size) # To pack the elements of a binary-valued array into bits in a uint8 array, use the numpy.packbits() method in Python Numpy # The result is padded to full bytes by inserting zero bits at the end # The axis is set using the axis parameter # The axis is the dimension over which bit-packing is done. res = np.packbits(arr, axis = -1) print("

Result...

",res)

## Output

Array... [[[1 0 1] [0 1 0]] [[1 1 0] [0 0 1]] [[1 1 0] [0 0 1]]] Array datatype... int64 Array Dimensions... 3 Our Array Shape... (3, 2, 3) Elements in the Array... 18 Result... [[[160] [ 64]] [[192] [ 32]] [[192] [ 32]]]

- Related Articles
- Pack the elements of a binary-valued Numpy array into bits in a uint8 array over axis 1
- Pack the elements of a binary-valued array into bits in a uint8 array over specific axis in Numpy
- Pack the elements of a binary-valued Numpy array into bits in a uint8 array
- Unpack elements of a uint8 array into a binary-valued output array over specific axis in Numpy
- Unpack elements of a uint8 array into a binary-valued output array over axis 0 in Numpy
- Unpack elements of a uint8 array into a binary-valued output array in Numpy
- Unpack elements of a uint8 array and only unpack some bits in Numpy
- Unpack elements of a uint8 array and trim off that many bits from the end in Numpy
- Reduce a multi-dimensional array and add elements along negative axis in Numpy
- Return the average of the masked array elements over axis 0 in Numpy
- Compute the maximum of the masked array elements over axis 0 in Numpy
- Compute the maximum of the masked array elements over axis 1 in Numpy
- Compute the minimum of the masked array elements over axis 0 in Numpy
- Compute the minimum of the masked array elements over axis 1 in Numpy
- Reduce a multi-dimensional array along negative axis in Numpy