返回掩码数组的视图,其中轴在 NumPy 中沿给定轴转置
要返回在 Python 中转置轴的数组视图,请使用ma. MaskedArray.transpose()Numpy 中的方法。对于一维数组,这没有影响,因为转置向量只是相同的向量。要将一维数组转换为二维列向量,必须添加一个额外的维度。np.atleast2 d(a).T 实现了这一点,a[:, np.newaxis] 也是如此。对于二维数组,这是标准矩阵转置。
轴可以是,
无或无参数 - 反转轴的顺序。
元组的整数 - 元组中第 j 个位置的 i 意味着 a 的第 i 个轴成为的第 j 个轴。a.transpose()
n ints - 与相同整数的 n 元组相同
脚步
首先,导入所需的库 -
import numpy as npimportnumpy.maas ma
使用该方法创建一个包含 int 元素的数组-numpy.array()
arr = np.array([[49, 85, 45], [67, 33, 59]])print("Array...\n", arr)
print("\nArray type...\n", arr.dtype)
获取数组的维度 -
print("Array Dimensions...\n",arr.ndim)
创建一个屏蔽数组并将其中一些屏蔽为无效 -
maskArr = ma.masked_array(arr, mask =[[0, 0, 1], [ 0, 1, 0]])print("\nOur Masked Array\n", maskArr)
print("\nOur Masked Array type...\n", maskArr.dtype)
获取 Masked Array 的尺寸 -
print("\nOur Masked Array Dimensions...\n",maskArr.ndim)
获取蒙面阵列的形状 -
print("\nOur Masked Array Shape...\n",maskArr.shape)
获取 Masked Array 的元素数量 -
print("\nElements in the Masked Array...\n",maskArr.size)
返回转置轴的数组视图,使用 ma. -MaskedArray.transpose()
print("\nResult...\n",maskArr.transpose((1, 0)))
示例
# Pythonma.MaskedArray- Return a view of the array with axes transposed along given axis输出结果import numpy as np
importnumpy.maas ma
# Create an array with int elements using the numpy.array() method
arr = np.array([[78, 85, 51], [56, 33, 97]])
print("Array...\n", arr)
print("\nArray type...\n", arr.dtype)
# Get the dimensions of the Array
print("\nArray Dimensions...\n",arr.ndim)
# Create a masked array and mask some of them as invalid
maskArr = ma.masked_array(arr, mask =[[0, 1, 0], [ 0, 0, 0]])
print("\nOur Masked Array\n", maskArr)
print("\nOur Masked Array type...\n", maskArr.dtype)
# Get the dimensions of the Masked Array
print("\nOur Masked Array Dimensions...\n",maskArr.ndim)
# Get the shape of the Masked Array
print("\nOur Masked Array Shape...\n",maskArr.shape)
# Get the number of elements of the Masked Array
print("\nElements in the Masked Array...\n",maskArr.size)
# To return a view of the array with axes transposed, use the ma.MaskedArray.transpose() method in Numpy
print("\nResult...\n",maskArr.transpose((1, 0)))
Array...[[78 85 51]
[56 33 97]]
Array type...
int64
Array Dimensions...
2
Our Masked Array
[[78 -- 51]
[56 33 97]]
Our Masked Array type...
int64
Our Masked Array Dimensions...
2
Our Masked Array Shape...
(2, 3)
Elements in the Masked Array...
6
Result...
[[78 56]
[-- 33]
[51 97]]
以上是 返回掩码数组的视图,其中轴在 NumPy 中沿给定轴转置 的全部内容, 来源链接: utcz.com/z/297083.html