在 NumPy 中生成範德蒙德矩陣


要生成範德蒙德矩陣,請在 Python NumPy 中使用 **np.ma.vander()** 方法。範德蒙德矩陣,以 Alexandre-Théophile Vandermonde 命名,是一個矩陣,其每一行都包含一個等比數列的項。

輸出矩陣的列是輸入向量的冪。冪的順序由遞增布林引數確定。具體來說,當 increasing 為 False 時,第 i 列輸出是輸入向量按元素提升到 N - i - 1 的冪。這樣一個在每一行都有等比數列的矩陣,是以 Alexandre-Théophile 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() 方法 -

print("
Result..
.", np.ma.vander(maskArr))

示例

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 print("
Result..
.", np.ma.vander(maskArr))

輸出

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..
. [[74805201 804357 8649 93 1]
[ 0 0 0 0 0]
[33362176 438976 5776 76 1]
[28398241 389017 5329 73 1]
[ 0 0 0 0 0]]

更新於: 2022年2月22日

166 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.