生成範德蒙矩陣並在 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))

示例

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))

輸出

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 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.