Loading...
Random Pearls Random Pearls New Menu
  • All Pearls
    • Random Pearls
  • Concepts
    • Concepts  (parent page)
    • Information Technology  (parent page of Concepts)
  • New Menu
  • Authors
  •  
  • Contact Us
  • Sign up
  • Login
    Forgot Password?
  • Follow us on
Image Back to Top Back to top
Language Preference
This website has language specific content. Here you can set the language(s) of your preference.

This setting does not affect the current page. It is used only for filtering pages in menus, preview tiles and search results.

It can be changed any time by using the login menu (if you are logged in) or by clicking the Language button on the left bottom of the page.
Log in to save your preference permanently.



If you do not set this preference, you may see a header like This page has 'language_name' content in page preview tiles.
Search
  • Navigation
  • Similar
  • Author
  • More...
You are here:
All Content / Science and Technology / Information Technology / Concepts / Numpy Array - Sum, Axes and Dimensions
Table of Contents

Subscribe to Our Newsletter
Follow us by subscribing to our newsletter and navigate to the newly added content on this website directly from your inbox!
Login to subscribe to this page.
Categories  
Tags  
Author  
manisar
Author's Display Image

"Whenever you can, share. You never know who all will be able to see far away standing upon your shoulders!"

I write mainly on topics related to science and technology.

Sometimes, I create tools and animation.


Numpy Array - Sum, Axes and Dimensions

June 15, 2021

Author - manisar


For a numpy array $a$, the idea of $\text{a.sum()}$ along different axes (one at a time) is slightly perplexing.
In order to understand it, we need to make sure that we understand the concept of axes of an array. Let's build the idea from the ground up, and eventually get the knack of summing an array along its axes.

For a quick understanding, have a look at this video.

To make things simple, but not too simple, let's have a 3D numpy array.
Something like this will suffice.

\[\begin{bmatrix}
\begin{bmatrix}
7&4&4&2&0\\
2&8&0&0&0\\
0&9&4&6&3\\
3&7&4&6&9\end{bmatrix}\\
\begin{bmatrix}
0&9&3&6&2\\
8&8&5&6&0\\
5&4&3&6&8\\
2&9&2&9&1\end{bmatrix}\\
\begin{bmatrix}
3&4&2&1&3\\
5&4&8&5&4\\
7&5&1&3&1\\
3&9&9&0&3\end{bmatrix}
\end{bmatrix}\]

Or, $a$ = numpy.array([
[[7, 4, 4, 2, 0],
[2, 8, 0, 0, 0],
[0, 9, 4, 6, 3],
[3, 7, 4, 6, 9]],

[[0, 9, 3, 6, 2],
[8, 8, 5, 6, 0],
[5, 4, 3, 6, 8],
[2, 9, 2, 9, 1]],

[[3, 4, 2, 1, 3],
[5, 4, 8, 5, 4],
[7, 5, 1, 3, 1],
[3, 9, 9, 0, 3]]

])

For the sake of convenience let's call the innermost dimension the dimension of points or values, the next dimension the dimension of rows, and the next one, the dimension of surfaces.

Numpy Array 3D

So, we can say that our array is made up of surfaces where each surface is made up of rows and each row is made up of points.

The key to understanding numpy arrays in depth is to understand the two terms - dimension and axis. There is a kind of reciprocal relationship between the two. Let's see what that is.

Let's talk about dimensions first. The $1^\text{st}$ dimension is the innermost one - the one that has the points, or values, or the actual content of the array.

The next dimension - dimension $2$ is some grouping or collection of those points - conveniently called rows.

The next dimension (the dimension of surfaces) would then be comprised of rows, and so on.

But when it comes to axes, it's actually the other way round.

Axis-$0$ is a set of imaginary lines that run through the greatest dimension of the array.

There are two things to note here:

  • run through - what does that mean?
  • why greatest? Shouldn't it have been the smallest?

The reason for the latter, if you look at it, is not actually weird, but obvious.

Let's talk about the points of our 3D array - any such point is given by $a[i][j][k]$.
If we keep any two of the three subscripts fixed, change the value of only one, and join such points, we get parallel lines.
These lines are nothing but the axes of the array.

If it's the first subscript ($i$) that we are changing while keeping $j$ and $k$ constant, and join such points, we get axis-$0$ (remember first item is denoted by $0$ in coding at most places).

Similarly, if we keep changing only $j$ in $a[i][j][k]$ and join such points, we get axis-$1$. And so on.

Thus, we start from the left, pick one subscript and change it while keeping the other two constant, join such points to get a specific axis of the array.

Now, the leftmost subscript ($i$ in $a[i][j][k]$) happens to denote the highest dimension of the array - this is by notational design.
So, if we are changing only $i$, we are basically moving across or running through the greatest dimension.
So, in the case of our array $a$, by moving along any line given by axis-$0$, we are moving across the z-dimension, or we are running through the surfaces.

Thus we see that axis-$0$ is the set of vertical lines as shown in the video.

What would be axis $1$? This can be visualized by connecting points $a[i][j][k]$ where $i$ and $k$ are constant and $j$ changes values.
Just the way we were earlier moving across surfaces (on axis $0$), now we are moving across, or running through rows (on any given line of axis $1$).

By connecting points $a[i][j][k]$ where only $k$ changes values, we move along any line on axis $2$. Now, we are moving across points (on the same row and the same surface).

The most important thing to remember in order to make sense of axes is the idea that an axis moves across one and only one dimension - that is, it will not have two objects at the same level in terms of a specific dimension.
It can be moving along multiple objects at the same level in terms of other dimensions.

So, do not try to go by the 'along' definition of array axes, that will leave you astray as soon as you go beyond 2D arrays. Remember the 'across' definition.

Once you get the hang of axes, the sum of array along different axes immediately becomes obvious.

Let's see the video and observe it ourselves.

Numpy Array Sum Along Different Axes

Background music credit: www.bensound.com

Advertisement
Advertisement
Close ad Ad

Advertisement
Close ad Ad

Return to Concepts

Tell us what you think (select text for formatting, or click )

Copyright © randompearls.com 2020

Privacy Policy