NumPy Operations

an illustrations of basic NumPy operations including adding arrays, functions, and dot product

Previously, we have talked about creating and slicing NumPy arrays. Now, let us see what else we can do with them. In short, a lot! The library has a huge amount of tools for numbers, vectors, matrices, tensors, etc. For tabular data analytics, there are also numerous useful NumPy operations that we can use. In this post, I will introduce the basic elementwise operations and some common functions in NumPy. You can access the complete codes in this notebook.

Basic elementwise NumPy operations

All the basic mathematical calculations in Python including +, -, *, /, %, //, and ** are applicable to NumPy arrays. Similarly, comparisons that are <, <=, ==, >=, >, != are also usable. All of these operations are elementwise, meaning that all items in the arrays go through the same calculations and generate an array of all the results. You can see an illustration in the following figure.

an illustration of elementwise NumPy operations between arrays and numbers

For examples (by the way, do not forget to import and alias NumPy in your new Jupyter session), you can see that +10, -5, and **2 are performed on the whole array as below.

Likewise, comparisons between an array and an individual number result in each item going through the same expressions. The result is now an array of the Boolean type.

You can also have all the previously mentioned operations between two arrays. In the simplest case, the two arrays must have the same size (same numbers of rows and columns). Generally, this condition is not that strict due to NumPy broadcasting, but this is the topic for another day. For now, let us assume that the two arrays have the same sizes. This type of NumPy operations then generates a new array in which each item is the result from items at the same positions in the two inputs. An illustration and examples are below.

An illustration of elementwise operations between arrays

Functions in NumPy Operations

Besides regular operations, NumPy also provides a big collections of functions from power, logarithm, to arithmetic, to trigonometrical, and much more. You can find a complete list in the library’s documentation. Some of the most common functions from my perspective are log(), exp(), sum(), mean(), std(), var(). All these functions come from NumPy, so we use the library’s name or alias to call them. For examples,

In short, trigonometrical and mathematical functions are elementwise – they yield an array having the results from applying the function on each item as you can see above with log() and sin(). On the other hand, statistical functions summarize the inputs and generate the result for the whole array, or each row/column in a smaller array. I will first showcase these functions applying on the whole inputs.

The axis option

To use these functions to obtain the statistics of items along rows or columns, we need to add an argument axis=. axis=0 means the functions result one value for each column, and axis=1 yields one value for each row. Below are an illustration with mean() and different options for axis.

an illustration of summarized function mean() applying on a numpy array with axis=0 and axis=1

Now let us observe the code below. We can see that axis=0 generates four results with ranges similar to that of the four columns, so these are their means. In contrast, axis=1 creates five fairly similar numbers, showing that they are the mean for each row. You can do the same thing with the other statistical functions, so do try that out.

Applications in data analytics

Needless to say, all the operations and functions that I mentioned, in combinations with array slicing, are widely use throughout data analysis. So, let me give a small data example and see what we can do with it. The data below contains three years of GPAs of several students:

Student IDFirst year GPASecond year GPACurrent GPA
00012523.123.313.54
00032152.572.592.55
00023242.392.783.11
00010123.212.912.73
00021513.523.553.62

First, we create the data as a NumPy array. You can notice that I do not include student IDs here because they are not of interests at the moment.

We will talk about exploratory analysis later, but means and standard deviations of columns are always nice to look at to have a general ideas of their values. We can now do that using NumPy. An example on how to interpret them is that the average GPA of students in the first year is 2.962, and on average, a student’s GPA deviate within 0.419 from 2.962. By the way, we can use functions as input to other functions as you can see here. I put these two in print() so that their outputs both show up in the same cell.

To look at the changes in GPAs of the students throughout the years, we slice the columns and take their differences. For examples, the changes from year 1 to year 2, and year 2 to year 3. We can see which students made the most improvement, or who lost performances.

Finally, a log transformation is very commonly applied on data. We can perform that here very easily.

Conclusion

In this post, I briefly introduce the basics of NumPy elementwise operations and some common functions. Of course, the library can do much more. Next, we will discuss the concept of concatenation. So, see you there!

2 Comments

Comments are closed