配列の大きさをそろえる方法がわからないです。
以下のコードを実行すると
# coding: utf-8 from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import numpy as np a = np.arange(5) b = np.arange(24).reshape((4,3,2)) while a.ndim > b.ndim: b = b[np.newaxis,:] print(b) print(b.size) print(b.ndim) while b.ndim > a.ndim: a = a[np.newaxis,:] print(a) print(a.size) print(a.ndim)
[[0 1 2 3 4]] 5 2 [[[0 1 2 3 4]]] 5 3
と出力結果が出ました。
a = np.arange(5) b = np.arange(24).reshape((4,3,2))
の出力結果は
[0 1 2 3 4] [[[ 0 1] [ 2 3] [ 4 5]] [[ 6 7] [ 8 9] [10 11]] [[12 13] [14 15] [16 17]] [[18 19] [20 21] [22 23]]]
で、while文でa・b2つの変数の中で使われている
np.newaxis の役割がわかりません。
np.newaxisは配列の次元を増やす役割をするので
特に
b = b[np.newaxis,:]
でbの次元数が減っていることが理解できません。
ここではaとbの次元数をそろえることをおこなっているとおもうのですが、
どうしてnp.newaxisにより次元数がそろえられるのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。