how to understand empty dimension in python numpy array? how to understand empty dimension in python numpy array? numpy numpy

how to understand empty dimension in python numpy array?


Let me just give you one example that illustrate one important difference.

d1 = np.array([1,2,3,4,5]) # array([1, 2, 3, 4, 5])d1.shape -> (5,) # row array.    d1.size -> 5# Note: d1.T is the same as d1.d2 = d1[np.newaxis] # array([[1, 2, 3, 4, 5]]). Note extra []d2.shape -> (1,5) d2.size -> 5# Note: d2.T will give a column arrayarray([[1],       [2],       [3],       [4],       [5]])d2.T.shape -> (5,1)


I also thought ndarrays would represent even 1-d arrays as 2-d arrays with a thickness of 1. Maybe because of the name "ndarray" makes us think high dimensional, however, n can be 1, so ndarrays can just have one dimension.

Compare these

x = np.array([[1], [2], [3], [4]])x.shape# (4, 1)x = np.array([[1, 2, 3, 4]])x.shape#(1, 4)x = np.array([1, 2, 3, 4])x.shape#(4,)

and (4,) means (4).

If I reshape x and back to (4), it comes back to original

x.shape = (2,2)x# array([[1, 2],#       [3, 4]])x.shape = (4)x# array([1, 2, 3, 4])


The main thing to understand here is that indexing with an integer is different than indexing with a slice. For example, when you index a 1d array or a list with an integer you get a scalar but when you index with a slice, you get an array or a list respectively. The same thing applies to 2d+ arrays. So for example:

# Make a 3d array:import numpy as nparray = np.arange(60).reshape((3, 4, 5))# Indexing with ints gives a scalarprint array[2, 3, 4] == 59# True# Indexing with slices gives a 3d arrayprint array[:2, :2, :2].shape# (2, 2, 2)# Indexing with a mix of slices and ints will give an array with < 3 dimsprint array[0, :2, :3].shape# (2, 3)print array[:, 2, 0:1].shape# (3, 1)

This can be really useful conceptually, because sometimes its great to think of an array as a collection of vectors, for example I can represent N points in space as an (N, 3) array:

n_points = np.random.random([10, 3])point_2 = n_points[2]print all(point_2 == n_points[2, :])# True