pyarrow.fixed_shape_tensor#

pyarrow.fixed_shape_tensor(DataType value_type, shape, dim_names=None, permutation=None)#

创建固定形状张量扩展类型的实例,包含形状以及可选的张量维度名称和所需维度逻辑顺序的索引。

参数:
value_typeDataType

单个张量元素的数据类型。

shapetuplelist of integers

所包含张量的物理形状。

dim_namestuplelist of strings, 默认 None

张量维度的显式名称。

permutationtuplelist integers, 默认 None

原始维度所需顺序的索引。这些索引包含 [0, 1, .., N-1] 的排列,其中 N 是维度数量。该排列表示逻辑布局的哪个维度对应于物理张量的哪个维度。关于此参数的更多信息,请参阅 Fixed shape tensor

返回:
typeFixedShapeTensorType

示例

创建固定形状张量扩展类型的实例

>>> import pyarrow as pa
>>> tensor_type = pa.fixed_shape_tensor(pa.int32(), [2, 2])
>>> tensor_type
FixedShapeTensorType(extension<arrow.fixed_shape_tensor[value_type=int32, shape=[2,2]]>)

检查数据类型

>>> tensor_type.value_type
DataType(int32)
>>> tensor_type.shape
[2, 2]

创建包含固定形状张量扩展数组的表格

>>> arr = [[1, 2, 3, 4], [10, 20, 30, 40], [100, 200, 300, 400]]
>>> storage = pa.array(arr, pa.list_(pa.int32(), 4))
>>> tensor = pa.ExtensionArray.from_storage(tensor_type, storage)
>>> pa.table([tensor], names=["tensor_array"])
pyarrow.Table
tensor_array: extension<arrow.fixed_shape_tensor[value_type=int32, shape=[2,2]]>
----
tensor_array: [[[1,2,3,4],[10,20,30,40],[100,200,300,400]]]

创建带有张量维度名称的固定形状张量扩展类型实例

>>> tensor_type = pa.fixed_shape_tensor(pa.int8(), (2, 2, 3),
...                                     dim_names=['C', 'H', 'W'])
>>> tensor_type.dim_names
['C', 'H', 'W']

创建带有排列(permutation)的固定形状张量扩展类型实例

>>> tensor_type = pa.fixed_shape_tensor(pa.int8(), (2, 2, 3),
...                                     permutation=[0, 2, 1])
>>> tensor_type.permutation
[0, 2, 1]