The concept of **“dimensional”** in numpy **is different** with dimensional** in math.** In Math, dimensions is defined as **the minimum number of coordinates** needed to specify points within a space. So, 1 dimension consist of 1 variable X , 2 dimensions consist of 2 variables X &Y and 3 dimensions consist of 3 variables X,Y & Z.

Meanwhile **dimensional in python** is a** container of items of the same type and size**.The dimension can consist of of 1 or multi dimensional container. That’s why it named N-dimensional array.

I will give you examples.

Open your Linux Terminal, run python and type the code below.

**Sample for 1 dimensional array**

$ python3 Python 3.6.9 (default, Nov 7 2019, 10:44:02) [nGCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>import numpy as np>>>x=np.array([1,2,3])>>>xarray([1, 2, 3]) >>>x.ndim1 >>>x.shape(3,) >>>

This is 1 dimensional array that consist of 3 columns.

**Sample for 2 dimensional array**

>>> x=np.array([[1,1],[2,2]]) >>> x array([[1, 1], [2, 2]]) Display the dimension >>> x.ndim 2

Display the array shape

>>> x.shape (2, 2)

You can read it as 2 ‘1-dimensional array’ ([1,1] and [2,2]) that each of it consist of 2 columns ([1,1] and [2,2]).

>>> x=np.array([[1,1,1],[2,2,2]]) >>> x array([[1, 1, 1], [2, 2, 2]]) >>> x.ndim 2 >>> x.shape (2, 3)

You can read it as 2 ‘1-dimensional array’ ([1,1,1] and [2,2,2]) that each of it consist of 3 columns ([1,1,1] and [2,2,2]).

>>> x=np.array([[1,1,1],[2,2,2],[3,3,3]]) >>> x array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])

It’s still 2 dimensional.

>>> x.ndim 2 >>> x.shape (3, 3) >>>

**Sample of 3 dimensional array.**

>>>x=np.array([[[1,1,1],[1,1,1]],[[2,2,2],[2,2,2]],[[3,3,3],[3,3,3]]])>>>xarray([[[1, 1, 1], [1, 1, 1]], [[2, 2, 2], [2, 2, 2]], [[3, 3, 3], [3, 3, 3]]]) >>> x.ndim 3 >>> x.shape (3, 2, 3)

You can read as 3 ‘__2 dimensional array__’ and each it consist of 2 ‘__1 dimensional array with 3 columns__’.

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

**Sample of 4 dimensional array.**

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

**What will happen if you input a wrong size array?**

It will automatically became a ‘List’ python.

For example:

You enter** 2 different size of array**.

The** first is 3 column** ([1,2,3], the second one is** 4 column** ([4,5,6,7,8]).

>>>x=np.array([[1,2,3],[5,6,7,8]])

The array will** automatically become List.**

>>> x array([list([1, 2, 3]), list([5, 6, 7, 8])], dtype=object)

How to transpose the dimensional array?

With numpy, you can transpose the dimensional array easily.

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

## Leave a Reply