「NumPy」maxメソッドで配列の行、列ごとの最大値を取得するサンプル
環境
PyCharm 2021.3
Python 3.9.7
構文
1.最大値
np.min()に配列ndarrayを渡すと、すべての要素の最大値が取得されます。
2.列、行ごとの最大値
引数axisに0を渡すと列ごとの最大値、1を渡すと行ごとの最大値が取得されます
使用例
import numpy as np
#4 x 4の配列
cft = np.arange(16).reshape(4, 4)
print(cft.shape)
print("配列の要素")
print(cft)
print("すべての要素の最大値")
print(np.max(cft))
print("列ごとの最大値")
print(np.max(cft, axis=0))
print("行ごとの最大値")
print(np.max(cft, axis=1))
import numpy as np
#4 x 4の配列
cft = np.arange(16).reshape(4, 4)
print(cft.shape)
print("配列の要素")
print(cft)
print("すべての要素の最大値")
print(np.max(cft))
print("列ごとの最大値")
print(np.max(cft, axis=0))
print("行ごとの最大値")
print(np.max(cft, axis=1))
import numpy as np #4 x 4の配列 cft = np.arange(16).reshape(4, 4) print(cft.shape) print("配列の要素") print(cft) print("すべての要素の最大値") print(np.max(cft)) print("列ごとの最大値") print(np.max(cft, axis=0)) print("行ごとの最大値") print(np.max(cft, axis=1))
実行結果
(4, 4)
配列の要素
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
すべての要素の最大値
15
列ごとの最大値
[12 13 14 15]
行ごとの最大値
[ 3 7 11 15]
(4, 4)
配列の要素
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
すべての要素の最大値
15
列ごとの最大値
[12 13 14 15]
行ごとの最大値
[ 3 7 11 15]
(4, 4) 配列の要素 [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] すべての要素の最大値 15 列ごとの最大値 [12 13 14 15] 行ごとの最大値 [ 3 7 11 15]