返回一个包含相同数据但具有新形状的掩码数组,在 NumPy 中将其视为列主序


要返回一个包含相同数据但具有新形状的掩码数组,请在 NumPy 中使用 **ma.MaskedArray.reshape()** 方法。在不更改数据的情况下为数组赋予新的形状。使用“**order**”参数设置顺序。'F' 顺序确定数组数据是否应视为 FORTRAN 格式,即 F(列主序)。

新形状应与原始形状兼容。如果提供整数,则结果将是长度为该整数的一维数组。

order 参数确定数组数据是否应视为 C(行主序)或 FORTRAN(列主序)顺序。返回一个包含相同数据但具有新形状的掩码数组。结果是原始数组的视图;如果无法实现,则会引发 ValueError。

步骤

首先,导入所需的库 -

import numpy as np
import numpy.ma as ma

使用 numpy.array() 方法创建一个包含整数元素的数组 -

arr = np.array([[49, 85, 45], [67, 33, 59]])
print("Array...
", arr) print("
Array type...
", arr.dtype)

获取数组的维度 -

print("Array Dimensions...
",arr.ndim)

创建一个掩码数组并将其中的某些元素标记为无效 -

maskArr = ma.masked_array(arr, mask =[[0, 0, 1], [ 0, 1, 0]])
print("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype)

获取掩码数组的维度 -

print("
Our Masked Array Dimensions...
",maskArr.ndim)

获取掩码数组的形状 -

print("
Our Masked Array Shape...
",maskArr.shape)

获取掩码数组的元素数量 -

print("
Elements in the Masked Array...
",maskArr.size)

返回一个包含相同数据但具有新形状的掩码数组,使用 ma.MaskedArray.reshape() 方法。在不更改数据的情况下为数组赋予新的形状。掩码数组的新形状设置为 6x1 作为参数。新形状应与原始形状兼容。如果提供整数,则结果将是长度为该整数的一维数组。使用“order”参数设置顺序。'F' 顺序确定数组数据是否应视为 FORTRAN 格式,即 F(列主序) -

print("
Result...
",maskArr.reshape((6,1),order='F'))

示例

# Python ma.MaskedArray - Return a masked array containing the same data but with a new shape
# viewed as column-major order

import numpy as np
import numpy.ma as ma

# Create an array with int elements using the numpy.array() method
arr = np.array([[78, 85, 51], [56, 33, 97]])
print("Array...
", arr) print("
Array type...
", arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) # Create a masked array and mask some of them as invalid # The masked array is 1x6 maskArr = ma.masked_array(arr, mask =[[0, 1, 0, 0, 0, 1]]) print("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype) # Get the dimensions of the Masked Array print("
Our Masked Array Dimensions...
",maskArr.ndim) # Get the shape of the Masked Array print("
Our Masked Array Shape...
",maskArr.shape) # Get the number of elements of the Masked Array print("
Elements in the Masked Array...
",maskArr.size) # To return a masked array containing the same data, but with a new shape, use the ma.MaskedArray.reshape() method in Numpy # Give a new shape to the array without changing its data # The new shape of the masked array is set to 6x1 as a parameter # The new shape should be compatible with the original shape. # If an integer is supplied, then the result will be a 1-D array of that length # The order is set using the "order" parameter # The 'F' order determines whether the array data should be viewed as in FORTRAN i.e F (column-major) print("
Result...
",maskArr.reshape((6,1),order='F'))

输出

Array...
[[78 85 51]
[56 33 97]]

Array type...
int64

Array Dimensions...
2

Our Masked Array
[[78 -- 51]
[56 33 --]]

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]
[--]]

更新于: 2022年2月4日

100 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告