LoginSignup
3
2

More than 5 years have passed since last update.

numpyでの行列とベクトルの結合に関するメモ

Last updated at Posted at 2019-03-18

何回も間違えたので自分用にメモ

numpyでのデータの形成で同じミスを何回もしたのでメモ

ベクトルとベクトルの結合は問題なくできる

v1.shape
#(2,)

v2.shape
#(3,)

v = np.hstack(v1,v2)

v.shape
#(5,0)

行列と行列もできる

v1.shape
#(3,2)

v2.shape
#(3,4)

v = np.hstack(v1,v2)

v.shape
#(3,6)

行列とベクトルの場合も同じだろうたかをくくると
ValueError: all the input array dimensions except for the concatenation axis must match exactly
で泣きを見るハメになる

その場合はreshapeでaxis=1に値を入れることで解決できる。つまり、ベクトルの列数を1であると明示的に書く。

v1.shape
#(3,2)

v2.shape
#(3,)

v2 = v2.reshpe(v2,1)

v2.shape
#(3,1)

v = np.hstack(v1,v2)

v.shape
#(3,3)

僕はこの内容で三回もつまずきました。同じミスした君は僕の仲間だね。

参考URL
https://deepage.net/features/numpy-stack.html

3
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2