【numpy】配列の結合np.hstack(tup)メモ
np.hsatck()では縦方向(axis=1)の結合ができる.
行列a, bに対してaxis=1のみが違うならば, aとbは結合できる.
すなわちa.shape[1]とb.shape[1]のみが異なり, a.shapeとb.shapeの他の要素が同じならば結合可能となる.
その他の結合方法としてcolumn_stack()による横方向の結合, dstak()による深さ方向の結合, vstack(),row_stack()による縦方向の結合などがあるらしい.
またnp.meshgrid()で格子点の制作に使ったnp.c_()も結合の関数である.
この場合はaとbのサイズが同じでなければならない.
> a = np.arange(8).reshape((2,4)) > b = np.arange(100,900,100).reshape(2,4) > print(a) [[0 1 2 3] [4 5 6 7]] > print(b) [[100 200 300 400] [500 600 700 800]] > print(np.r_[a, b]) [[ 0 1 2 3] [ 4 5 6 7] [100 200 300 400] [500 600 700 800]] > print(np.c_[a, b]) [[ 0 1 2 3 100 200 300 400] [ 4 5 6 7 500 600 700 800]]
結合があれば分解もあって,np.split()というものがあるらしいが, 今回はまだ勉強しない_(:3」∠)_
【参考】
・http://python-remrin.hatenadiary.jp/entry/concatenate
・https://deepage.net/features/numpy-stack.html