NumPy - 建立結構化陣列



結構化陣列簡介

NumPy 中的結構化陣列允許在單個數組中表示不同型別和大小的資料。結構化陣列中的每個元素都可以是具有多個欄位的記錄,每個欄位都有自己的資料型別。

這類似於擁有一個表格,其中每一行代表一個具有各種屬性的記錄。主要要點如下:

  • 結構化陣列中的每個元素(記錄)可以有多個欄位。
  • 欄位可以具有不同的資料型別(例如,整數、浮點數、字串)。
  • 結構化陣列可用於表示複雜的資料結構。

定義結構化陣列

建立結構化陣列的第一步是為結構化陣列定義資料型別。這使用 NumPy 的dtype物件完成,該物件指定陣列中欄位的名稱和型別,如下例所示:

import numpy as np

# Define the data type for the structured array
dtype = np.dtype([('name', 'U10'), ('age', 'i4'), ('height', 'f4')])

這裡,dtype是一個結構化資料型別,具有三個欄位:

  • name: 最多 10 個字元的字串 ('U10')
  • age: 一個整數 ('i4')
  • height: 一個浮點數 ('f4')

建立結構化陣列

定義好“dtype”後,可以透過將 dtype 傳遞給 np.array() 函式來建立結構化陣列。以下是語法:

numpy.array(
   object, 
   dtype=None, 
   copy=True, 
   order='K', 
   subok=False, 
   ndmin=0
)

其中,

  • object: 這是您要轉換為 NumPy 陣列的輸入資料。它可以是列表、元組或任何其他類似序列的結構。
  • dtype (可選): 指定陣列所需的資料型別。如果未提供,NumPy 將從輸入資料推斷資料型別。例如,您可以使用 'int32'、'float64'、'str' 等。
  • copy (可選): 如果為 True(預設值),則建立一個新陣列。如果為 False,則僅在必要時建立新陣列(即,如果輸入物件本身不是陣列)。如果為 False,則 np.array 可能在可能的情況下返回原始陣列的檢視。
  • order (可選): 指定記憶體佈局順序。它可以是 'C' 表示行主序(C 樣式)順序,'F' 表示列主序(Fortran 樣式)順序,或 'K' 表示輸入中找到的順序。預設值為 'K'。
  • subok (可選): 如果為 True,則如果輸入是子類,則將使用 ndarray 的子類。預設值為 False,這意味著返回的陣列將始終是 ndarray 的例項。
  • ndmin (可選): 指定結果陣列應具有的最小維度數。例如,將 ndmin 設定為 2 可確保結果至少為二維陣列。

示例

在下面的示例中,我們正在定義一個結構化陣列,其指定的 dtype 包括“name”、“age”和“height”的欄位。然後我們使用與該結構匹配的資料建立陣列,並列印結果結構化陣列:

import numpy as np

# Define the dtype with field names and data types
dtype = [('name', 'U10'), ('age', 'i4'), ('height', 'f4')]

# Create data consistent with the dtype
data = [('Alice', 30, 5.6), ('Bob', 25, 5.8), ('Charlie', 35, 5.9)]

# Create the structured array
structured_array = np.array(data, dtype=dtype)
print("Structured Array:\n", structured_array)

獲得以下輸出:

Structured Array:
[('Alice', 30, 5.6) ('Bob', 25, 5.8) ('Charlie', 35, 5.9)]

使用不同的資料型別建立結構化陣列

您可以根據應用程式的需求建立具有各種資料型別(包括字串、整數和浮點數)的結構化陣列。

在結構化陣列的上下文中:

  • 字串: 您可以在結構化陣列中儲存文字資料。例如,名稱欄位可以使用字串資料型別。
  • 整數: 數值資料(如年齡或計數)可以儲存為整數。這可能包括年齡或數量等資料,它們是整數。
  • 浮點數: 對於十進位制數或實數,您可以使用浮點資料型別。這對於測量或任何需要精度的值(例如身高或體重)很有用。

示例

在下面的示例中,我們正在建立一個結構化陣列,其資料型別 (dtype) 包括混合資料型別:整數表示 ID、字串表示名稱以及浮點數表示分數:

import numpy as np

# Define a dtype with mixed data types
dtype = [('id', 'i4'), ('name', 'U15'), ('score', 'f8')]
data = [(1, 'Alice', 88.5), (2, 'Bob', 91.2), (3, 'Charlie', 85.4)]

# Create the structured array
structured_array = np.array(data, dtype=dtype)
print("Structured Array with Mixed Data Types:\n", structured_array)

這將產生以下結果:

Structured Array with Mixed Data Types:[(1, 'Alice', 88.5) (2, 'Bob', 91.2) (3, 'Charlie', 85.4)]

使用元組列表建立結構化陣列

您可以使用元組列表定義 dtype 並建立結構化陣列,其中每個元組表示一個欄位。每個元組包含兩個元素:第一個元素是欄位的名稱,第二個元素是該欄位的資料型別。

示例

在下面的示例中,我們正在使用指定的 dtype 定義一個具有“name”、“age”和“height”欄位的結構化陣列。然後我們使用相應的資料建立此陣列:

import numpy as np

# Define the dtype
dtype = [('name', 'U10'), ('age', 'i4'), ('height', 'f4')]

# Define the data
data = [('Alice', 30, 5.6), ('Bob', 25, 5.8), ('Charlie', 35, 5.9)]

# Create the structured array
structured_array = np.array(data, dtype=dtype)

print("Structured Array:\n", structured_array)

獲得以下輸出:

Structured Array:
[('Alice', 30, 5.6) ('Bob', 25, 5.8) ('Charlie', 35, 5.9)]

使用字典建立結構化陣列

或者,您可以使用字典定義資料和 dtype 以清楚地指定欄位的名稱和型別。字典中的每個鍵都代表一個欄位名稱,與每個鍵關聯的值定義該欄位的資料型別。

示例

在此示例中,我們使用字典格式定義結構化陣列的 dtype 以指定“name”、“age”和“height”的欄位。然後我們建立並顯示此結構化陣列及其對應的資料,將其組織成支援每個記錄中多種資料型別的格式:

import numpy as np

# Define the dtype using a dictionary
dtype = np.dtype([('name', 'U10'), ('age', 'i4'), ('height', 'f4')])

# Define the data
data = [('Alice', 30, 5.6), ('Bob', 25, 5.8), ('Charlie', 35, 5.9)]

# Create the structured array
structured_array = np.array(data, dtype=dtype)

print("Structured Array from Dictionary:\n", structured_array)

這將產生以下結果:

Structured Array from Dictionary:
[('Alice', 30, 5.6) ('Bob', 25, 5.8) ('Charlie', 35, 5.9)]
廣告

© . All rights reserved.