LoginSignup
3
3

More than 5 years have passed since last update.

Numpy 1D array[x] と 2D array[x,1]の違い

Last updated at Posted at 2016-04-01

Python初心者のメモ。内容の間違いなどご注意ください。

import numpy as np

def test():
    a = np.zeros([5,1]) #2D array columnができる
    b = np.ones([5]) #1D array columnなし。

    print a 
    print 
    print "Dimension a: ", a.shape 
    print 
    print b, b.shape
    print 
    print "Dimension b: ", b.shape 
    print
    print a + b #1D + 2Dの演算になる
    print 

    a = np.squeeze(a)
    print a, #1D に変更
    print 
    print a + b

if __name__ == '__main__':
    test()

Output:

[[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]]

Dimension a:  (5, 1)

[ 1.  1.  1.  1.  1.] (5,)

Dimension b:  (5,)

[[ 1.  1.  1.  1.  1.]
[ 1.  1.  1.  1.  1.]
[ 1.  1.  1.  1.  1.]
[ 1.  1.  1.  1.  1.]
[ 1.  1.  1.  1.  1.]]

[ 0.  0.  0.  0.  0.]
[ 1.  1.  1.  1.  1.]
3
3
2

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
3