在 Python 中獲取四維陣列的逆矩陣


要計算四維陣列的逆矩陣,可以使用 Python 中的 numpy.linalg.tensorinv() 方法。結果是相對於 tensordot 操作 tensordot(a, b, ind) 的 a 的逆矩陣,即在浮點精度範圍內,tensordot(tensorinv(a), a, ind) 是 tensordot 操作的“單位”張量。

該方法返回 a 的 tensordot 逆矩陣,形狀為 a.shape[ind:] + a.shape[:ind]。第一個引數是 a,要“反轉”的張量。其形狀必須是“正方形”,即 prod(a.shape[:ind]) == prod(a.shape[ind:])。第二個引數是 ind,參與逆和運算的前幾個索引的個數。必須是正整數,預設為 2。

步驟

首先,匯入所需的庫 -

import numpy as np
from numpy.linalg import inv

建立一個數組。numpy.eye() 返回一個二維陣列,對角線上的元素為 1,其他位置的元素為 0 -

arr = np.eye(4*6)

更改上面建立的陣列的形狀 -

arr.shape = (4, 6, 8, 3)

顯示陣列 -

print("Our Array...\n",arr)

檢查維度 -

print("\nDimensions of our Array...\n",arr.ndim)

獲取資料型別 -

print("\nDatatype of our Array object...\n",arr.dtype)

獲取形狀 -

print("\nShape of our Array object...\n",arr.shape)

要計算四維陣列的逆矩陣,可以使用 Python 中的 numpy.linalg.tensorinv() 方法 -

print("\nResult...\n",np.linalg.tensorinv(arr))

示例

import numpy as np
from numpy.linalg import inv

# Create an array

# The numpy.eye() returns a 2-D array with ones on the diagonal and zeros elsewhere
arr = np.eye(4*6)

# Changing the shape of the array created above
arr.shape = (4, 6, 8, 3)

# Display the array
print("Our Array...\n",arr)

# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)

# Get the Shape
print("\nShape of our Array object...\n",arr.shape)

# To compute the inverse of a Four-Dimensional array, use the numpy.linalg.tensorinv() method in Python.
print("\nResult...\n",np.linalg.tensorinv(arr))

輸出

Our Array...
   [[[[1. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]

   [[0. 1. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]

   [[0. 0. 1.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]

   [[0. 0. 0.]
   [1. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]

   [[0. 0. 0.]
   [0. 1. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]

   [[0. 0. 0.]
   [0. 0. 1.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]]


   [[[0. 0. 0.]
   [0. 0. 0.]
   [1. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]

   [[0. 0. 0.]
   [0. 0. 0.]
   [0. 1. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]

   [[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 1.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]

   [[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [1. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]

   [[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 1. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]

   [[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]]


   [[[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [1. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]

   [[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 1. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]

   [[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 1.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]

   [[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [1. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]

   [[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 1. 0.]
   [0. 0. 0.]
   [0. 0. 0.]]

   [[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 1.]
   [0. 0. 0.]
   [0. 0. 0.]]]


   [[[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [1. 0. 0.]
   [0. 0. 0.]]

   [[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 1. 0.]
   [0. 0. 0.]]

   [[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 1.]
   [0. 0. 0.]]

   [[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [1. 0. 0.]]

   [[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 1. 0.]]

   [[0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 0.]
   [0. 0. 1.]]]]

Dimensions of our Array...
4

Datatype of our Array object...
float64

Shape of our Array object...
(4, 6, 8, 3)

Result...
   [[[[1. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]]

   [[0. 1. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]]

   [[0. 0. 1. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]]]


   [[[0. 0. 0. 1. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]]

   [[0. 0. 0. 0. 1. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]]

   [[0. 0. 0. 0. 0. 1.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]]]


   [[[0. 0. 0. 0. 0. 0.]
   [1. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]]

   [[0. 0. 0. 0. 0. 0.]
   [0. 1. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]]

   [[0. 0. 0. 0. 0. 0.]
   [0. 0. 1. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]]]


   [[[0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 1. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]]

   [[0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 1. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]]

   [[0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 1.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]]]


   [[[0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [1. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]]

   [[0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 1. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]]

   [[0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 1. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]]]


   [[[0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 1. 0. 0.]
   [0. 0. 0. 0. 0. 0.]]

   [[0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 1. 0.]
   [0. 0. 0. 0. 0. 0.]]

   [[0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 1.]
   [0. 0. 0. 0. 0. 0.]]]


   [[[0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [1. 0. 0. 0. 0. 0.]]

   [[0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 1. 0. 0. 0. 0.]]

   [[0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 1. 0. 0. 0.]]]


   [[[0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 1. 0. 0.]]

   [[0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 1. 0.]]

   [[0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 0. 1.]]]]

更新於: 2022年2月25日

370 次檢視

啟動你的 職業生涯

透過完成課程獲得認證

開始學習
廣告