pyarrow.fixed_shape_tensor#
- pyarrow.fixed_shape_tensor(DataType value_type, shape, dim_names=None, permutation=None)#
创建固定形状张量扩展类型的实例,包含形状以及可选的张量维度名称和所需维度逻辑顺序的索引。
- 参数:
- 返回:
示例
创建固定形状张量扩展类型的实例
>>> 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]