0

newbie here. Trying to learn conceptually what it means to have an n-dimensional array in python. For example, if I create an ndarrray using the following code:

x = np.array([[1, 2, 3], [4, 5, 6]], np.int32)

how exactly are the 1,2,3 and 4,5,6 blocks related?

what if I added another block [6, 7, 8]? can I think of them as separate rows of the same grid? I get that i can create an array of N-dimensions by passing in N lists using the above, but can't just grasp conceptually what it means for an array to have more than one dimension.

thanks so much

realr
  • 3,652
  • 6
  • 23
  • 34
dbs5
  • 133
  • 2
  • 9
  • While it's fairly easy to relate 1d and 2d arrays to things we work with, such as lines or lists, and matrices (and vectors), it's harder to conceptualize other dimensions, 0d, 3d, 4d etc. Some people, coming from a linear algebra background even have problems with 1d. To them everything is 2d, a matrix. `numpy` has taken the concept of 1 and 2d, and generalized it to 0d and up. So in a sense an `N-dimensional` array is an abstract object, that strictly speaking can't be conceptualized. – hpaulj Jan 21 '20 at 01:53

1 Answers1

0

Number of dimensions is the same as the depth to which lists are nested. Your example is 2d because it is a list of lists. Adding a third list doesn't change the dimension. What would make it 3d would be if it was a list of lists of lists.

If you print that array, you'll see it displayed as a 2d grid, where each of the lists is a row, and each list index is a column. It gets harder to imagine as you go to higher dimensions, but a 3d grid could be rendered as a cube, and then 4d and beyond is too difficult to imagine.

--Edit--

As pointed out, an array is not the same as nested lists. They are functionally very different. But the original question was about conceptualizing multidimensional arrays.

import numpy as np

array = np.arange(27)
print(array)
array = array.reshape(3, 9)
print(array)
array = array.reshape(3, 3, 3)
print(array)

If you run this script, you can see the same information displayed as a 1d, 2d, and 3d array.

# 1D
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26]

# 2D
[[ 0  1  2  3  4  5  6  7  8]
 [ 9 10 11 12 13 14 15 16 17]
 [18 19 20 21 22 23 24 25 26]]

# 3D
[[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]]

 [[ 9 10 11]
  [12 13 14]
  [15 16 17]]

 [[18 19 20]
  [21 22 23]
  [24 25 26]]]

The 1d array is akin to a number line. The 2d array is like a grid. The 3d array could represent each of the cells in a 3x3x3 cube. For learning how to operate on an array, this isn't particularly useful. But for being able to conceptually grasp the structure of an N-dimensional array, it's helpful to me.

schwartz721
  • 767
  • 7
  • 19
  • appreciate it @schwartz721! is it fair to think of them as rows in the same grid? – dbs5 Jan 20 '20 at 23:48
  • 1
    NumPy arrays are not nested lists. They can be created from nested lists, but they are conceptually and behaviorally different. – user2357112 Jan 20 '20 at 23:49
  • 1
    As user2357112 said, arrays aren't the same as nested lists because of how arrays are operated on, etc. But for conceptualization it's not a bad place to start – schwartz721 Jan 20 '20 at 23:55
  • @schwartz721 this is EXACTLY what I was looking for. thank you!!! – dbs5 Jan 21 '20 at 00:11
  • That was really helpful @schwartz721 thank you –  Oct 14 '21 at 22:31