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)]
廣告