生成范德蒙矩阵并在 Numpy 中设置输出的列数


要生成范德蒙矩阵,请在 Python Numpy 中使用 **np.ma.vander()** 方法。使用 N 参数设置输出中的列数。如果未指定 N,则返回一个方阵(N = len(x))。

输出矩阵的列是输入向量的幂。幂的顺序由 increasing 布尔参数确定。具体来说,当 increasing 为 False 时,第 i 列输出是输入向量按元素提升到 N - i - 1 的幂。这样一个每一行都具有几何级数的矩阵以 Alexandre-Theophile Vandermonde 的名字命名。

步骤

首先,导入所需的库 -

import numpy as np

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

arr = np.array([93, 33, 76, 73, 88]) print("Array...", arr)

创建一个掩码数组并掩盖其中一些无效的值 -

maskArr = ma.masked_array(arr, mask =[0, 1, 0, 0, 1]) 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("Number of elements in the Masked Array...",maskArr.size)

要生成范德蒙矩阵,请在 Python Numpy 中使用 np.ma.vander() 方法。使用 N 参数设置输出中的列数。如果未指定 N,则返回一个方阵(N = len(x)) -

N = 4 print("Result...", np.ma.vander(maskArr, N))

示例

Open Compiler
import numpy as np import numpy.ma as ma # Create an array with int elements using the numpy.array() method arr = np.array([93, 33, 76, 73, 88]) print("Array...", arr) # Create a masked array and mask some of them as invalid maskArr = ma.masked_array(arr, mask =[0, 1, 0, 0, 1]) print("Our Masked Array...", maskArr) # Get the type of the masked array 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("Number of elements in the Masked Array...",maskArr.size) # To generate a Vandermonde matrix, use the np.ma.vander() method in Python Numpy # Set the number of columns in the output using the N parameter. If N is not specified, a square array is returned (N = len(x)) N = 4 print("Result...", np.ma.vander(maskArr, N))

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输出

Array...
[93 33 76 73 88]

Our Masked Array...
[93 -- 76 73 --]

Our Masked Array type...
int64

Our Masked Array Dimensions...
1

Our Masked Array Shape...
(5,)

Number of elements in the Masked Array...
5

Result..
. [[804357 8649 93 1]
[ 0 0 0 0]
[438976 5776 76 1]
[389017 5329 73 1]
[ 0 0 0 0]]

更新于: 2022年2月22日

246 次查看

启动您的 职业生涯

通过完成课程获得认证

开始学习
广告