[PYTHON] Differences between Numpy 1D array [x] and 2D array [x, 1]

A note for Python beginners. Please be careful about mistakes in the content.

import numpy as np

def test():

a = np.zeros ([5,1]) # 2D array column is created b = np.ones ([5]) # 1D array without 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 operation print

    a = np.squeeze(a)

Change to 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.]

Recommended Posts

Differences between Numpy 1D array [x] and 2D array [x, 1]
Differences between numpy and pandas methods for finding variance
About Numpy array and asarray
Performance comparison between 2D matrix calculation and for with numpy
Differences between Windows and Linux directories
Differences between symbolic links and hard links
Difference between Numpy randint and Random randint
Differences between Python, stftime and strptime
Differences in authenticity between Python and JavaScript
Differences between Ruby and Python in scope
Differences in syntax between Python and Java
Matplotlib Basics / Differences between fig and axes
Differences in multithreading between Python and Jython
Differences between Django's request.POST ['hoge'] and request.POST.get ('hoge')
Differences between Ruby and Python (basic syntax)
Differences between queryStringParameters and multiValueQueryStringParameters in AWS Lambda
Summary of the differences between PHP and Python
NumPy array manipulation (3)
list and numpy
Differences and commonalities between dict, list, and tuple types
Behavior of numpy.dot when passing 1d array and 2d array
Differences between glibc, musl libc and go resolvers
[Blender x Python] 3D Bouncing Ball and other animations
Difference between Numpy (n,) and (n, 1) notation [Difference between horizontal vector and vertical vector]
EP 3 Know the Differences Between bytes, str, and unicode
Python and numpy tips
Between parametric and nonparametric
python numpy array calculation
AtCoder ABC155 Problem D Pairs Review Note 2 NumPy and Python
Python> Difference between inpbt and print (inpbt) output> [1. 2. 3.] / array ([1., 2., 3.], dtype = float32)
To go back and forth between standard python, numpy, pandas ①
A rough summary of the differences between Windows and Linux
Extract array elements and indexes in descending order with numpy
I tried to enumerate the differences between java and python
Summary of Differences Between Ruby on Rails and Django ~ Basics ~