はじめに
個人的なnumpyのメモ
ランダムな値の配列を作る
0〜9までのランダムな値を持つ、2×2の配列を作る
python
# coding: utf-8
import numpy as np
rand_arr = np.random.randint(0, 10, (2, 2))
print(rand_arr)
# 結果
# [[3 7]
# [2 6]]
配列の合計、最大値(最小値)を取得する
第2引数にaxis=0を設定すると、各列に対する結果になる
第2引数にaxis=1を設定すると、各行に対する結果になる
python
# coding: utf-8
import numpy as np
random_arr = np.random.randint(0, 10, (2, 2))
print(random_arr)
# 結果
# [[4 8]
# [3 1]]
arr_sum = np.sum(random_arr)
print(arr_sum)
# 結果
# 16
arr_sum_by_col = np.sum(random_arr, axis=0)
print(arr_sum_by_col)
# 結果
# [7 9]
arr_sum_by_row = np.sum(random_arr, axis=1)
print(arr_sum_by_row)
# 結果
# [12 4]
arr_max = np.max(random_arr)
print(arr_max)
# 結果
# 8
arr_max_by_col = np.max(random_arr, axis=0)
print(arr_max_by_col)
# 結果
# [4 8]
arr_max_by_row = np.max(random_arr, axis=1)
print(arr_max_by_row)
# 結果
# [8 3]