Python 演算子でNumpy配列の全要素を累乗するサンプル

環境
Windows 11 Pro 64bit
Python 3.11

構文
result = 配列名 ** N
演算子を使ってNumpy配列(Array)の全要素を累乗・べき乗するには、「**」を使います。
左辺にNumpy配列、右辺に乗数を指定した「**」で演算します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import numpy as np
ns = np.array([1, 2, 3, 4, 5])
res = ns ** 4
print(res)
import numpy as np ns = np.array([1, 2, 3, 4, 5]) res = ns ** 4 print(res)
import numpy as np

ns = np.array([1, 2, 3, 4, 5])

res = ns ** 4

print(res)

実行結果
[ 1 16 81 256 625]

Python

Posted by arkgame