Introduction to NumPy- Numerical Python

Raman Singh
5 min readDec 30, 2020

--

NumPy is the fundamental package for scientific computing with Python and it is the library Pandas, Matplotlib and Scikit-learn builds on top off. You might think “what’s the point of using NumPy when I could be using these libraries?” but I think that NumPy is often underrated and when used right, it could be quite a powerful tool for numerical operations in Python.

Why Use NumPy?

In Python we have lists that serve the purpose of arrays, but they are slow to process.

NumPy aims to provide an array object that is up to 50x faster than traditional Python lists.

The array object in NumPy is called ndarray, it provides a lot of supporting functions that make working with ndarray very easy.

Arrays are very frequently used in data science, where speed and resources are very important.

Why is NumPy Faster Than Lists?

NumPy arrays are stored at one continuous place in memory unlike lists, so processes can access and manipulate them very efficiently.

This behavior is called locality of reference in computer science.

This is the main reason why NumPy is faster than lists. Also it is optimized to work with latest CPU architectures.

Installing NumPy

NumPy does not come with Python by default so it needs to be installed.

To download and install NumPy you need to write the following code:

pip install numpy

Importing NumPy

After you’ve downloaded and install NumPy, you need to import it every time you want to use it in your Python IDE.

To import NumPy you need to write the following code:

import numpy as np

Getting Started with NumPy

Creating Arrays using NumPy

NumPy is used to work with arrays. The array object in NumPy is called ndarray.

We can create a NumPy ndarray object by using the array() function

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

An array can have any number of dimensions.

When the array is created, you can define the number of dimensions by using the ndmin argument.

arr = np.array([1, 2, 3, 4], ndmin=5)

NumPy Arrays Indexing

Array indexing is the same as accessing an array element.

You can access an array element by referring to its index number.

The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.

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

print(arr[0])

print(arr[3])

Accessing 2-D array

arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])

print(‘3th element on 2nd dim: ‘, arr[1, 2])

NumPy Arrays Slicing

Slicing in python means taking elements from one given index to another given index.

We pass slice instead of index like this: [start:end].

We can also define the step, like this: [start:end:step].

If we don’t pass start its considered 0

If we don’t pass end its considered length of array in that dimension

If we don’t pass step its considered 1

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[1:5])

Negative Slicing

Use the minus operator to refer to an index from the end:

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[-3:-1])

Step

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[1:5:2])

Sorting and Reshaping

array.sort() can be used to sort your NumPy array — you can pass different arguments inside the brackets to define what you want to sort on (by using the argument ‘order=string/list of strings’, for example. See more examples in the documentation). array.sort(axis=0) will sort specific axis of the array — rows or columns. two_d_arr.flatten() will flatten a 2 dimensional array to a 1 dimensional array. array.T will transpose an array — meaning columns will become rows and vice versa. array.reshape(x,y) would reshape your array to the size you set with x and y. array.resize((x,y)) will change the array shape to x and y and fill new values with zeros.

Combining and Splitting

You can use np.concatenate((array1,array2),axis=0) to combine two NumPy arrays — this will add array 2 as rows to the end of array 1 while np.concatenate((array1,array2),axis=1) will add array 2 as columns to the end of array 1. np.split(array,2) will spilt the array into two sub-arrays and np.hsplit(array,5) will split the array horizontally on the 5th index.

Adding and Removing Elements

There are, of course, commands to add and remove elements from NumPy arrays:

  • np.append(array,values) will append values to end of array.
  • np.insert(array, 3, values)will insert values into array before index 3
  • np.delete(array, 4, axis=0)will delete row on index 4 of array
  • np.delete(array, 5, axis=1) will delete column on index 5 of array

Descriptive Statistics

You can use NumPy methods to get descriptive statistics on NumPy arrays:

  • np.mean(array,axis=0) will return mean along specific axis (0 or 1)
  • array.sum() will return the sum of the array
  • array.min()will return the minimum value of the array
  • array.max(axis=0)will return the maximum value of specific axis
  • np.var(array)will return the variance of the array
  • np.std(array,axis=1)will return the standard deviation of specific axis
  • array.corrcoef()will return the correlation coefficient of the array
  • numpy.median(array) will return the median of the array elements

Doing Math with NumPy

Any tutorial to NumPy would not be complete without the numerical and mathematical operations you can do with NumPy! Let’s go over them:

np.add(array ,1) will add 1 to each element in the array and np.add(array1,array2) will add array 2 to array 1. The same is true to np.subtract(), np.multiply(), np.divide() and np.power() — all these commands would work in exactly the same way as described above.

You can also get NumPy to return different values from the array, like:

  • np.sqrt(array) will return the square root of each element in the array
  • np.sin(array) will return the sine of each element in the array
  • np.log(array) will return the natural log of each element in the array
  • np.abs(arr) will return the absolute value of each element in the array
  • np.array_equal(arr1,arr2) will return True if the arrays have the same elements and shape

It is possible to round different values in array: np.ceil(array) will round up to the nearest integer, np.floor(array) will round down to the nearest integer and np.round(array) will round to the nearest integer.

Thank you for reading! I would appreciate any comments, notes, corrections, questions or suggestions — if there’s anything you’d like me to write about, please don’t hesitate to let me know.

--

--