インデックス参照とスライス、axis

次のコードで空欄になっている行に入る適切な選択肢を 1 ~ 3 から選び、Q1 ~ 4 に答えてください.

Q1:変数 arr_2d の要素のうち、5 を出力してください.

Q2:変数 arr_2d から、以下の行列をスライスし出力してください.
   [[3, 4]
[6, 7]]

Q3:ndarray.sum() 関数を使って arr_2d から、以下のような配列を作成し出力してください.
   [3, 12, 21]

import numpy as np

arr_2d = np.arange(0,9).reshape(3,3)
print(arr_2d)

# Q1
print(##########)

[Q1の選択肢]
1. arr_2d[1,2]
2. arr_2d[2,3]
3. arr_2d[2,1]

# Q2
print(##########)

[Q2の選択肢]
1. arr_2d[1:, :2, :]
2. arr_2d[2:, :2]
3. arr_2d[1:, :2]

# Q3
print(##########)

[Q3の選択肢]
1. arr_2d.sum(axis= 0)
2. arr_2d.sum(axis=1)
3. arr_2d.sum(axis=2)

Leave a comment

Your email address will not be published. Required fields are marked *