[Day-7]NumPy Array-Operations

Advait Joshi
4 min readApr 19, 2024

--

Hello everyone! Today I delved in to NumPy Array Operations.

The Topics covered in this article:

  1. Addition, Subtraction, Multiplicaton, Division
  2. Broadcasting
  3. Maximum, Minimum, Sum, Mean, Product, Standard Deviation

Let’s begin…

Let’s say, that you’ve created two arrays, one called “data” and one called “ones”:

You can add the arrays together with the plus sign.

data = np.array([1, 2])
ones = np.ones(2, dtype=int)
data + ones
#Output :
array([2, 3])

The other operations:

data - ones
#Output :
array([0, 1])

data * data
#Output :
array([1, 4])

data / data
#Output :
array([1., 1.])

sum() :

This works for 1D arrays, 2D arrays, and arrays in higher dimensions.

a = np.array([1, 2, 3, 4])

a.sum()
#Output :
10

To add the rows or the columns in a 2D array, you would specify the axis.

If you start with this array:

b = np.array([[1, 1], [2, 2]])

You can sum over the axis of rows with:

b.sum(axis=0)
#Output :
array([3, 3])

You can sum over the axis of columns with:

>>> b.sum(axis=1)
#Output :
array([2, 4])

Broadcasting :

Broadcasting is a mechanism that allows NumPy to perform operations on arrays of different shapes. The dimensions of your array must be compatible, for example, when the dimensions of both arrays are equal or when one of them is 1. If the dimensions are not compatible, you will get a ValueError.

Let’s say, you want to carry out an operation between an array and a single number (also called an operation between a vector and a scalar) or between arrays of two different sizes. For example, your array (we’ll call it “data”) might contain information about distance in miles but you want to convert the information to kilometers. You can perform this operation with:

data = np.array([1.0, 2.0])
data * 1.6
#Output :
array([1.6, 3.2])

NumPy understands that the multiplication should happen with each cell. That concept is called broadcasting.

More Operations :

NumPy also performs aggregation functions. In addition to min, max, and sum, you can easily run mean to get the average, prod to get the result of multiplying the elements together, std to get the standard deviation, and more.

data.max()
#Output :
2.0

data.min()
#Output :
1.0

data.sum()
#Output :
3.0

Let’s start with this array, called “a”

a = np.array([[0.45053314, 0.17296777, 0.34376245, 0.5510652],
[0.54627315, 0.05093587, 0.40067661, 0.55645993],
[0.12697628, 0.82485143, 0.26590556, 0.56917101]])

It’s very common to want to aggregate along a row or column. By default, every NumPy aggregation function will return the aggregate of the entire array. To find the sum or the minimum of the elements in your array, run:

a.sum()
#Output :
4.8595784

Or:

a.min()
#Output :
0.05093587

You can specify on which axis you want the aggregation function to be computed. For example, you can find the minimum value within each column by specifying axis=0.

a.min(axis=0)
#Output :
array([0.12697628, 0.05093587, 0.26590556, 0.5510652 ])

The four values listed above correspond to the number of columns in your array. With a four-column array, you will get four values as your result.

This is all that I learned about the different numpy array operations today. My learnings will be documented here on Medium for better understanding. Stay tuned for the next blog.

All The Best People☺️.

Follow me on Linkedin:

www.linkedin.com/in/advaitszone

To read my previous blog “[Day-6]NumPy Array-Functions(4)”:

https://medium.com/@advaitszone/day-6-numpy-array-functions-4-d2dee92e1413

--

--