
- NumPy 教程
- NumPy - 首頁
- NumPy - 簡介
- NumPy - 環境
- NumPy 陣列
- NumPy - Ndarray 物件
- NumPy - 資料型別
- NumPy 建立和運算元組
- NumPy - 陣列建立例程
- NumPy - 陣列操作
- NumPy - 從現有資料建立陣列
- NumPy - 從數值範圍建立陣列
- NumPy - 遍歷陣列
- NumPy - 陣列重塑
- NumPy - 連線陣列
- NumPy - 堆疊陣列
- NumPy - 分割陣列
- NumPy - 扁平化陣列
- NumPy - 轉置陣列
- NumPy 索引和切片
- NumPy - 索引和切片
- NumPy - 高階索引
- NumPy 陣列屬性和操作
- NumPy - 陣列屬性
- NumPy - 陣列形狀
- NumPy - 陣列大小
- NumPy - 陣列步長
- NumPy - 陣列元素大小
- NumPy - 廣播
- NumPy - 算術運算
- NumPy - 陣列加法
- NumPy - 陣列減法
- NumPy - 陣列乘法
- NumPy - 陣列除法
- NumPy 高階陣列操作
- NumPy - 交換陣列的軸
- NumPy - 位元組交換
- NumPy - 副本和檢視
- NumPy - 元素級陣列比較
- NumPy - 過濾陣列
- NumPy - 連線陣列
- NumPy - 排序、搜尋和計數函式
- NumPy - 搜尋陣列
- NumPy - 陣列的並集
- NumPy - 查詢唯一行
- NumPy - 建立日期時間陣列
- NumPy - 二元運算子
- NumPy - 字串函式
- NumPy - 數學函式
- NumPy - 統計函式
- NumPy - 矩陣庫
- NumPy - 線性代數
- NumPy - Matplotlib
- NumPy - 使用 Matplotlib 繪製直方圖
- NumPy - NumPy 的 I/O 操作
- NumPy 排序和高階操作
- NumPy - 陣列排序
- NumPy - 沿軸排序
- NumPy - 使用花式索引排序
- NumPy - 結構化陣列
- NumPy - 建立結構化陣列
- NumPy - 操作結構化陣列
- NumPy - 欄位訪問
- NumPy - 記錄陣列
- NumPy - 載入陣列
- NumPy - 儲存陣列
- NumPy - 向陣列追加值
- NumPy - 交換陣列的列
- NumPy - 向陣列插入軸
- NumPy 處理缺失資料
- NumPy - 處理缺失資料
- NumPy - 識別缺失值
- NumPy - 刪除缺失資料
- NumPy - 填充缺失資料
- NumPy 效能最佳化
- NumPy - 使用陣列進行效能最佳化
- NumPy - 使用陣列進行向量化
- NumPy - 陣列的記憶體佈局
- NumPy 線性代數
- NumPy - 線性代數
- NumPy - 矩陣庫
- NumPy - 矩陣加法
- NumPy - 矩陣減法
- NumPy - 矩陣乘法
- NumPy - 元素級矩陣運算
- NumPy - 點積
- NumPy - 矩陣求逆
- NumPy - 行列式計算
- NumPy - 特徵值
- NumPy - 特徵向量
- NumPy - 奇異值分解
- NumPy - 求解線性方程組
- NumPy - 矩陣範數
- NumPy 元素級矩陣運算
- NumPy - 求和
- NumPy - 求平均值
- NumPy - 求中位數
- NumPy - 求最小值
- NumPy - 求最大值
- NumPy 集合運算
- NumPy - 唯一元素
- NumPy - 交集
- NumPy - 並集
- NumPy - 差集
- NumPy 有用資源
- NumPy 編譯器
- NumPy - 快速指南
- NumPy - 有用資源
- NumPy - 討論
NumPy packbits() 函式
NumPy 的packbits()函式用於將二進位制值陣列的元素打包到 uint8 陣列中的位中。此函式將二進位制值的陣列(即 0 和 1)壓縮成一個 8 位無符號整數陣列,其中結果 uint8 陣列中的每個位都表示輸入陣列中的一個元素。
在此函式中,我們可以沿指定軸或在扁平化的輸入陣列上進行操作。在處理大型二進位制資料陣列時,它對於減少記憶體使用特別有用。
語法
以下是 Numpy packbits() 函式的語法:
numpy.packbits(a, /, axis=None, bitorder='big')
引數
以下是 Numpy packbits() 函式的引數:
- a(array_like): 輸入的二進位制值陣列。
- axis: 此引數是要打包位的軸。如果為 None,則陣列會被展平。
- bitorder: 打包表示中的位順序,可以是“big”或“little”。
返回值
此函式返回一個包含打包位元組的uint8型別陣列。
示例 1
以下是 Numpy packbits() 函式的基本示例,其中將 1D 位陣列打包到位元組陣列中:
import numpy as np # Define the input array of bits (0s and 1s) bits = np.array([1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1], dtype=np.uint8) # Pack the bits into bytes packed = np.packbits(bits) print(packed)
以下是packbits()函式的輸出:
[178 229]
示例 2
在 NumPy 中沿預設軸打包 2D 陣列涉及將陣列每行中的二進位制值 0 和 1 轉換為更緊湊的表示形式,例如位元組。
packbits()函式的預設軸為 -1,它指的是最後一個軸,對於 2D 陣列通常是列。以下是一個示例:
import numpy as np # Define a 2D array of bits bit_array = np.array([[0, 1, 1, 0, 0, 1, 1, 1], [1, 0, 1, 1, 0, 0, 0, 0]], dtype=np.uint8) # Pack the bits into bytes packed_array = np.packbits(bit_array) print("Original bit array:\n", bit_array) print("Packed array:", packed_array)
以下是上述示例的輸出:
Original bit array: [[0 1 1 0 0 1 1 1] [1 0 1 1 0 0 0 0]] Packed array: [103 176]
示例 3
以下示例顯示了packbits()函式如何在 3D 陣列上工作:
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...") print(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 res = np.packbits(arr) print("Result...", res)
以下是上述示例的輸出:
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... [171 28 64]
numpy_binary_operators.htm
廣告