- 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 - 欄位訪問
NumPy 欄位訪問
NumPy 中的欄位訪問是指根據欄位名稱檢索或修改結構化陣列中的特定元素。它允許您處理陣列中每個記錄的各個屬性或特性。
NumPy 中的結構化陣列使您能夠定義包含多個欄位的記錄陣列,每個欄位都有自己的資料型別。可以單獨訪問結構化陣列中的欄位,從而可以操作資料。
按名稱訪問單個欄位
NumPy 中的結構化陣列允許您為每個元素內的不同欄位分配名稱。此命名約定使您可以輕鬆地使用這些名稱直接訪問特定欄位。
在 NumPy 中,當使用結構化陣列時,訪問單個欄位允許您與陣列中每個元素的特定元件或屬性進行互動。這在處理包含多種型別資料的陣列時非常重要。
示例
在以下示例中,我們訪問結構化陣列的“name”欄位以提取並檢索陣列中的所有名稱 -
import numpy as np
# Define a structured array with fields 'name' and 'age'
dtype = [('name', 'U10'), ('age', 'i4')]
data = [('Alice', 25), ('Bob', 30)]
structured_array = np.array(data, dtype=dtype)
# Access the 'name' field
names = structured_array['name']
print("Names:", names)
以下是獲得的輸出 -
Names: ['Alice' 'Bob']
多維陣列中的欄位訪問
要訪問多維結構化陣列中的特定欄位,您可以使用類似於一維和二維陣列中使用的索引技術,但應用於多個維度。
多維結構化陣列是一個數組,其中每個元素本身都是一個結構化陣列,這些元素按多個維度(例如,二維、三維陣列)組織。陣列中的每個元素都可以有多個欄位,類似於一個表,其中每一行都是一個具有多個屬性的記錄。
示例
在下面的示例中,我們訪問三維結構化陣列的第一層中的“name”欄位 -
import numpy as np
# Define a 3D structured array
dtype = [('name', 'U10'), ('age', 'i4'), ('height', 'f4')]
data = [[[('Alice', 25, 5.5), ('Bob', 30, 6.0)],
[('Charlie', 35, 5.8), ('David', 40, 6.2)]],
[[('Eve', 28, 5.7), ('Frank', 33, 6.1)],
[('Grace', 29, 5.6), ('Hank', 32, 6.3)]]]
structured_array_3d = np.array(data, dtype=dtype)
# Access the 'name' field from the first layer
names_layer_0 = structured_array_3d[0]['name']
print("Names in the first layer:\n", names_layer_0)
以下是上述程式碼的輸出 -
Names in the first layer: [['Alice' 'Bob'] ['Charlie' 'David']]
訪問特定切片中的欄位
訪問特定切片中的欄位意味著從結構化陣列中的特定子集或資料範圍檢索值。
當您切片結構化陣列的單個維度時,您可以訪問結果切片中的特定欄位。要訪問涉及多個維度的切片中的欄位,您需要跨維度應用切片,然後從結果子陣列中選擇欄位。
示例:切片一維和訪問欄位
在以下示例中,我們對結構化陣列進行切片以獲取行子集,具體為第 1 行和第 2 行。切片後,我們訪問並檢索此子集中的“name”和“age”欄位 -
import numpy as np
# Define a structured array with fields 'name' and 'age'
dtype = [('name', 'U10'), ('age', 'i4')]
data = [('Alice', 25), ('Bob', 30), ('Charlie', 35), ('David', 40)]
structured_array = np.array(data, dtype=dtype)
# Slice the array to get a subset of rows
sliced_array = structured_array[1:3] # Gets rows 1 and 2
# Access the 'name' field from the sliced array
names = sliced_array['name']
# Access the 'age' field from the sliced array
ages = sliced_array['age']
print("Sliced names:", names)
print("Sliced ages:", ages)
獲得的輸出如下所示 -
Sliced names: ['Bob' 'Charlie'] Sliced ages: [30 35]
示例:切片二維和訪問欄位
在這裡,我們對二維結構化陣列進行切片以提取行和列的子集。然後,我們訪問並檢索此陣列切片部分中的“name”和“age”欄位 -
import numpy as np
# Define a 2D array with structured data
dtype = [('name', 'U10'), ('age', 'i4')]
data = [[('Alice', 25), ('Bob', 30)],
[('Charlie', 35), ('David', 40)]]
structured_array = np.array(data, dtype=dtype).view(np.recarray)
# Slice the array to get a subset of rows and columns
sliced_array = structured_array[0:2, 0:2] # Gets all rows and columns
# Access the 'name' field from the sliced array
names = sliced_array['name']
# Access the 'age' field from the sliced array
ages = sliced_array['age']
print("Sliced names:", names)
print("Sliced ages:", ages)
執行上述程式碼後,我們得到以下輸出 -
Sliced names: [['Alice' 'Bob'] ['Charlie' 'David']] Sliced ages: [[25 30] [35 40]]
同時訪問多個欄位
同時訪問多個欄位意味著同時從結構化陣列中的多個欄位檢索資料,允許您一起處理欄位子集。
要在 NumPy 中同時訪問多個欄位,您可以使用以下方法 -
- 使用欄位名稱列表訪問:您可以指定欄位名稱列表以獲取僅包含這些欄位的結構化陣列。
- 使用結構化陣列的欄位索引:如果您需要按索引訪問欄位,則可以使用其位置選擇它們。
示例
在下面的示例中,我們透過指定欄位名稱或索引訪問結構化陣列的不同欄位,並列印結果。我們檢索特定欄位,如“name”和“age”,以及所有欄位 -
import numpy as np
# Define a structured array with fields 'name', 'age', and 'height'
dtype = [('name', 'U10'), ('age', 'i4'), ('height', 'f4')]
data = [('Alice', 25, 5.5), ('Bob', 30, 6.0), ('Charlie', 35, 5.8)]
structured_array = np.array(data, dtype=dtype)
# 1. Accessing multiple fields with a list of field names
selected_fields = structured_array[['name', 'age']]
print("Selected fields (name and age):")
print(selected_fields)
# 2. Accessing fields by index
names = structured_array['name']
ages = structured_array['age']
heights = structured_array['height']
print("\nNames:", names)
print("Ages:", ages)
print("Heights:", heights)
# Accessing all fields simultaneously
all_fields = structured_array[['name', 'age', 'height']]
print("\nAll fields:")
print(all_fields)
產生的結果如下 -
Selected fields (name and age):
[('Alice', 25) ('Bob', 30) ('Charlie', 35)]
Names: ['Alice' 'Bob' 'Charlie']
Ages: [25 30 35]
Heights: [5.5 6. 5.8]
All fields:
[('Alice', 25, 5.5) ('Bob', 30, 6. ) ('Charlie', 35, 5.8)]
將欄位訪問與布林索引結合使用
將欄位訪問與布林索引結合使用是指根據應用於陣列的條件或過濾器從結構化陣列中檢索特定欄位。
布林索引允許您選擇滿足給定條件的陣列元素。透過將布林掩碼(一個布林值陣列)應用於結構化陣列,您可以根據應用於一個或多個欄位的條件過濾陣列。
示例
在以下示例中,我們使用布林掩碼根據“age”欄位過濾結構化陣列。然後,我們選擇並列印“age”大於 30 的條目的“name”和“height”欄位 -
import numpy as np
# Define a structured array
dtype = [('name', 'U10'), ('age', 'i4'), ('height', 'f4')]
data = [('Alice', 25, 5.5), ('Bob', 30, 6.0), ('Charlie', 35, 5.8), ('David', 40, 6.2)]
structured_array = np.array(data, dtype=dtype)
# Create a boolean mask for filtering based on 'age'
mask = structured_array['age'] > 30
# Apply boolean indexing and select 'name' and 'height' fields
filtered_fields = structured_array[mask][['name', 'height']]
print("Filtered Fields (name and height) where age > 30:\n", filtered_fields)
我們得到如下所示的輸出 -
Filtered Fields (name and height) where age > 30:[('Charlie', 5.8) ('David', 6.2)]