Learn Python NumPy With Examples

Everything you need to know to get started with NumPy

Shahzaib Khan
19 min readJul 21, 2020

If you want to be a data scientist, you are gonna need to know numpy (numerical python). It is one of the most useful library of python especially, if you are crunching numbers. As majority of Data Science and Machine Learning revolves around Statistics, numpy becomes much more important to have hands-on.

Unlike other NumPy tutorials, this is a concise NumPy programming tutorial for people who think that reading is boring. I try to show everything with simple code examples; there are no long and complicated explanations with fancy words.

This section will get you started with using NumPy and you’ll be able to learn more about whatever you want after studying it.

The current post focuses on the following topics:

Installation
Arrays
Array Creation
Modifying Array
Deleting in Array
Reshaping Array
Array Sorting
Array Indexing / Slicing
Array Stacking
Array Splitting
Array Operations
Array Broadcasting
Iterating Over Arrays
Searching Arrays
Generating random numbers

Note: For this post we will be using Windows as our Operating System along with PyCharm as IDE.

Installation

To start with, we will be using PyCharm i.e Python IDE (Integrated Development Environment).

PyCharm is an IDE for professional developers. It is created by JetBrains, a company known for creating great software development tools.

There are two versions of PyCharm:

  • Community — free open-source version, lightweight, good for Python and scientific development
  • Professional — paid version, full-featured IDE with support for Web development as well

PyCharm provides all major features that a good IDE should provide: code completion, code inspections, error-highlighting and fixes, debugging, version control system and code refactoring. All these features come out of the box.

Personally speaking, PyCharm is my favorite IDE for Python development. The only major complaint I have heard about PyCharm is that it’s resource-intensive. If you have a computer with a small amount of RAM (usually less than 4 GB), your computer may lag.

You can download the PyCharm community version which is FREE and can be downloaded from their official website and follow the steps as shown over the video:

Once you have setup Python and PyCharm. Let’s install NumPy.

To install NumPy on PyCharm, click on File and go to the Settings. Under Settings, choose your Python project and select Python Interpreter.

You will see the + button. Click on it and search for the NumPy in the search field. You will see the NumPy package as the left side and its description, version on the right side.

Selecting numpy click on the Install Package on the left bottom. It will install the packages.

How to test if numpy is installed or not?

After the installation of the numpy on the system you can easily check whether numpy is installed or not. To do so, just use the following command to check. Inside the Pycharm write the following code and run the program for getting the output.

import numpy as np
print (np.__version__)

You will see the following output

So we are all set to start learning NumPy.

Before we move on to coding, lets just set go through some basic stuff :)

Arrays

In general, an array is a data structure that contains a group of elements. Typically these elements are all of the same data type, such as an integer or string. They are commonly used in programs to organize data so that a related set of values can be easily sorted or searched.

When it comes to NumPy, an array is a central data structure of the library. It’s a grid of values and it contains information about the raw data, how to locate an element, and how to interpret an element. It has a grid of elements that can be indexed in various ways. The elements are all of the same type, referred to as the array dtype (data type).

An array can be indexed by a tuple of non-negative integers, by booleans, by another array, or by integers. The rank of the array is the number of dimensions. The shape of the array is a tuple of integers giving the size of the array along each dimension.

One way we can initialize NumPy arrays is from nested Python lists.

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

We can access the elements in the array using square brackets. When you’re accessing elements, remember that indexing in NumPy starts at 0. That means that if you want to access the first element in your array, you’ll be accessing element “0”.

print(a[0])

Output:

[1 2 3 4].

Some more information about arrays

You might occasionally hear an array referred to as a “ndarray,” which is shorthand for “N-dimensional array.” An N-dimensional array is simply an array with any number of dimensions. You might also hear 1-D, or one-dimensional array, 2-D, or two-dimensional array, and so on. The NumPy ndarray class is used to represent both matrices and vectors. A vector is an array with a single column, while a matrix refers to an array with multiple columns.

What are the attributes of an array?

An array is usually a fixed-size container of items of the same type and size. The number of dimensions and items in an array is defined by its shape. The shape of an array is a tuple of non-negative integers that specify the sizes of each dimension.

In NumPy, dimensions are called axes. This means that if you have a 2D array that looks like this:

[[0., 0., 0.],
[1., 1., 1.]]

Your array has 2 axes. The first axis has a length of 2 and the second axis has a length of 3.

Just like in other Python container objects, the contents of an array can be accessed and modified by indexing or slicing the array. Different arrays can share the same data, so changes made on one array might be visible in another.

Array attributes reflect information intrinsic to the array itself. If you need to get, or even set, properties of an array without creating a new array, you can often access an array through its attributes.

Now back to business, lets look into coding : )

Array Creation

To create a NumPy array, you can use the function np.array().

All you need to do to create a simple array is pass a list to it.

#Load Library
import numpy as np
#create an array
a = np.array([1, 2, 3])

Besides creating an array from a sequence of elements you can easily create arrays by using other functions. The functions are discussed as under:

numpy.zeros: You can easily create an array filled with 0s by using numpy.zeros as it returns a new array of specified size, filled with zeros.

np.zeros(2) 

It will output an array with 2 zero’s:

[0., 0.]

Note − Don’t get confused with np.zeros and numpy.zeros. Since we imported NumPy library as: import numpy as np that is the reason we are using np.zeros.

numpy.ones: returns a new array of specified size and type, filled with ones.

np.ones(5)

Output:

[ 1.  1.  1.  1.  1.]

numpy.empty: The function empty creates an array whose initial content is random and depends on the state of the memory.

# Create an empty array with 2 elements
np.empty(2)

Output:

[ 5.26288483e+064 -1.80841072e-141]

Note − The elements in an array show random values as they are not initialized.

The default data type is floating point (float64), you can explicitly specify which data type you want using dtype.

a = np.empty([3,2], dtype = int)

The output is as follows showing 3 x 2 array:

[[ 873918640      32764]
[ 873923184 32764]
[1851858988 1752375396]]

numpy.arange: You can create an array with a range of elements.

Input:

np.arange(4)

Output:

[0, 1, 2, 3]

And even an array that contains a range of evenly spaced intervals. To do this, you will specify the first number, last number, and the step size.

Input:

np.arange(2,9,2)

Output:

[2, 4, 6, 8]

numpy.linspace: This function is similar to arange() function. In this function, instead of step size, the number of evenly spaced values between the interval is specified.

Input:

np.linspace(10,20,5)

Output:

[10.  12.5 15.  17.5 20. ]

If you notice, we started from the value 10 and end it till 20 with evenly space for 5 values.

Modifying Array

Let say you start with this array:

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

You can add elements to your array any time with np.append(). Make sure to specify the array and the elements you want to include.

Input:

np.append(arr, [1,2])

Output:

array([1, 2, 3, 4, 5, 6, 7, 8, 1, 2])

Deleting in Array

You can delete an element with np.delete(). If you want to delete the element in position 1 of your array, you can run:

Input:

np.delete(arr, 1)

Output:

array([1, 3, 4, 5, 6, 7, 8])

Note − The index of an array start with 0, so position 1 refers at actual the 2nd element.

Reshaping Array

Using np.reshape() will give a new shape to an array without changing the data. Just remember that when you use the reshape function, the array you want to produce needs to have the same number of elements as the original array. If you start with an array with 12 elements, you’ll need to make sure that your new array also has a total of 12 elements.

But before even moving into reshape method, it is important to know the shape and size of your array. To get this, NumPy provide us with few functions:

ndarray.ndim will tell you the number of axes, or dimensions, of the array.

ndarray.size will tell you the total number of elements of the array. This is the product of the elements of the array’s shape.

ndarray.shape will display a tuple of integers that indicate the number of elements stored along each dimension of the array. If, for example, you have a 2D array with 2 rows and 3 columns, the shape of your array is (2,3).

So let say, we start with this array:

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

Now lets first find out the dimensions of the array:

print (arr.ndim)

Output:

2

Now lets first find out the size of the array:

print (arr.size)

Output:

6

Now lets first find out the shape of the array:

print (arr.shape)

Output:

(2, 3)

Let’s now look into reshaping the array, first example is a single dimensional array with 6 element inside. Hence the we could convert this into a 2 x 3 array.

a = np.arange(6)
print(a)

Output:

[0 1 2 3 4 5]

You can use reshape() to reshape your array. For example, you can reshape this array to an array with three rows and two columns:

Input:

b = a.reshape(3,2)
print(b)

Output:

[[0 1]
[2 3]
[4 5]]

Lets take another example:

This time lets do invert, we take 3 x 2 Array and convert it into 1 x 6:

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

Output:

[[1 2 3 4 5 6]

Array Sorting

Sorting an element is simple with np.sort(). You can specify the axis, kind, and order when you call the function.

If you start with this array:

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

You can quickly sort the numbers in ascending order with:

Input:

print(np.sort(arr))

Output:

[1, 2, 3, 4, 5, 6, 7, 8]

Array Indexing / Slicing

You can index and slice NumPy arrays in the same ways you can slice Python lists.

data = np.array([1,2,3])
print(data[0])
print(data[1])
print(data[0:2])
print(data[1:])
print(data[-2:])

Output:

1
2
[1 2]
[2 3]

You may want to take a section of your array or specific array elements to use in further analysis or additional operations. To do that, you’ll need to subset, slice, and/or index your arrays.

For example, if you start with this array:

a = np.arange(10)
print (a)

Output:

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

Now slice items starting from index 2:

print (a[2:])

Output:

[2 3 4 5 6 7 8 9]

Lets now try slice between range: 2 to 5

print (a[2:5])

Output:

[2 3 4];

Now lets try and do the same with multi-dimensional array:

a = np.array([[1,2,3],[3,4,5],[4,5,6]]) 
print (a)
# slice items starting from index
print a[1:]

Output:

[[3 4 5]
[4 5 6]]

Note −Don’t confuse yourself here, remember we sliced it from index 1 onward, in short 0 index will get removed from the top.

If you want to select values from your array that fulfill certain conditions, it’s straightforward with NumPy.

For example, if you start with this array:

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

You can easily print all of the values in the array that are less than 5.

print(a[a<5])

Output:

[1 2 3 4]

You can also select, for example, numbers that are equal to or greater than 5, and use that condition to index an array.

five_up = (a >= 5)
print(a[five_up])

Output:

[ 5  6  7  8  9 10 11 12]

You can select elements that are divisible by 2:

divisible_by_2 = a[a%2==0]
print(divisible_by_2)

Output:

[ 2  4  6  8 10 12]

Or you can select elements that satisfy two conditions using the & and | operators:

Input:

c = a[(a > 2) & (a < 11)]
print(c)

Output:

[ 3  4  5  6  7  8  9 10]

You can also make use of the logical operators & and | in order to return boolean values that specify whether or not the values in an array fulfill a certain condition. This can be useful with arrays that contain names or other categorical values.

Input:

five_up = (a > 5) | (a == 5)
print(five_up)

Output:

[[False False False False]
[ True True True True]
[ True True True True]]

You can also use np.where() to select elements or indices from an array.

Starting with this array:

Input:

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

You can use np.where() to print the indices of elements that are, for example, less than 5:

Input:

b = np.where(a<5)
print(b)

Output:

(array([0, 0, 0, 0]), array([0, 1, 2, 3]))

Array Stacking

Joining Array

Joining means putting contents of two or more arrays in a single array. We pass a sequence of arrays that we want to join to the concatenate() function, along with the axis. If axis is not explicitly passed, it is taken as 0.

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.concatenate((arr1, arr2))
print(arr)

Output:

[1 2 3 4 5 6]

Let’s take another example, this time with a 2D array:

arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
arr = np.concatenate((arr1, arr2), axis=1)
print(arr)

Output:

[[1 2 5 6]
[3 4 7 8]]

Joining Arrays Using Stack Functions

Stacking is same as concatenation, the only difference is that stacking is done along a new axis.

We can concatenate two 1-D arrays along the second axis which would result in putting them one over the other, ie. stacking.

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.stack((arr1, arr2), axis=1)
print(arr)

Output:

[[1 4]
[2 5]
[3 6]]

Stacking Along Rows

NumPy provides a helper function: hstack() to stack along rows.

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.hstack((arr1, arr2)) #h refers to horizontal i.e row
print(arr)

Output:

[1 2 3 4 5 6]

Stacking Along Columns

NumPy provides a helper function: vstack() to stack along columns.

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.vstack((arr1, arr2))
print(arr)

Output:

[[1 2 3]
[4 5 6]]

Array Splitting

Splitting is reverse operation of Joining.

Joining merges multiple arrays into one and Splitting breaks one array into multiple.

We use array_split() for splitting arrays, we pass it the array we want to split and the number of splits.

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

Output:

[array([1, 2]), array([3, 4]), array([5, 6])]

Let’s take another example:

This time split the array in 4 parts:

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

You will notice here that we have 6 elements in an array and we have to split it into 4. Hence, if an array has less elements than required, it will adjust from the end accordingly.

Output:

[array([1, 2]), array([3, 4]), array([5]), array([6])]

Split Into Arrays

The return value of the array_split() method is an array containing each of the split as an array.

If you split an array into 3 arrays, you can access them from the result just like any array element:

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

print(newarr[0])
print(newarr[1])
print(newarr[2])

Output:

[1 2]
[3 4]
[5 6]

Splitting 2-D Arrays

Use the same syntax when splitting 2-D arrays.

Use the array_split() method, pass in the array you want to split and the number of splits you want to do.

Example: Split the 2-D array into three 2-D arrays:

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

Output:

[array([[1, 2],
[3, 4]]), array([[5, 6],
[7, 8]]), array([[ 9, 10],
[11, 12]])]

You can split an array into several smaller arrays using hsplit. You can specify either the number of equally shaped arrays to return or the columns after which the division should occur.

Let’s say you have this array:

arr = np.array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12],
[13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]])

If you wanted to split this array into three equally shaped arrays, you would run:

Input:

np.hsplit(arr,3)

Output:

[array([[ 1,  2,  3,  4],
[13, 14, 15, 16]]), array([[ 5, 6, 7, 8],
[17, 18, 19, 20]]), array([[ 9, 10, 11, 12],
[21, 22, 23, 24]])]

If you wanted to split your array after the third and fourth column, you’d run:

Input:

np.hsplit(arr,(3,4))

Output:

[array([[ 1,  2,  3],
[13, 14, 15]]), array([[ 4],
[16]]), array([[ 5, 6, 7, 8, 9, 10, 11, 12],
[17, 18, 19, 20, 21, 22, 23, 24]])]

Array Operations

Basic operations are simple with NumPy. If you want to find the sum of the elements in an array, you’d use sum(). This works for 1D arrays, 2D arrays, and arrays in higher dimensions.

a = np.array([1, 2, 3, 4])# Add all of the elements in the array
print (a.sum())

Output:

10

Let’s now test it with 3 x 4 Array:

a = np.array([[1,2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print (a.sum())

Output:

78

Similarly other arithmetic operations such as add(), subtract(), multiply(), and divide() are also easy to use:

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

b = np.array([10,10,10])
print (b)

print (np.add(a,b))
print (np.subtract(a,b))
print (np.multiply(a,b))
print (np.divide(a,b))

Note — Input arrays for performing arithmetic operations such as add(), subtract(), multiply(), and divide() must be either of the same shape or should conform to array broadcasting rules.

Array Broadcasting

The term broadcasting refers to the ability of NumPy to treat arrays of different shapes during arithmetic operations. Arithmetic operations on arrays are usually done on corresponding elements. If two arrays are of exactly the same shape, then these operations are smoothly performed.

a = np.array([1,2,3,4]) 
b = np.array([10,20,30,40])
c = a * b
print (c)

Output:

[ 10  40  90 160]

If the dimensions of two arrays are dissimilar (i.e not same), element-to-element operations are not possible. However, operations on arrays of non-similar shapes is still possible in NumPy, because of the broadcasting capability. The smaller array is broadcast to the size of the larger array so that they have compatible shapes.

Example:

a = np.array([[0.0,0.0,0.0],[10.0,10.0,10.0],[20.0,20.0,20.0],[30.0,30.0,30.0]])
b = np.array([1.0,2.0,3.0])
print (a)
print (b)
print (a + b)

The following figure demonstrates how array b is broadcast to become compatible with a.

Output:

[[ 0.  0.  0.]
[10. 10. 10.]
[20. 20. 20.]
[30. 30. 30.]]
[1. 2. 3.][[ 1. 2. 3.]
[11. 12. 13.]
[21. 22. 23.]
[31. 32. 33.]]

Iterating Over Arrays

Iterating means going through elements one by one.

As we deal with multi-dimensional arrays in numpy, we can do this using basic for loop of python.

If we iterate on a 1-D array it will go through each element one by one.

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

for x in arr:
print(x)

Output:

1
2
3

Iterating 2-D Arrays

In a 2-D array it will go through all the rows.

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

for x in arr:
print(x)

Output:

[1 2 3]
[4 5 6]

Iterating 3-D Arrays

In a 3-D array it will go through all the 2-D arrays.

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

for x in arr:
print(x)

Output:

[[1 2 3]
[4 5 6]]
[[7 8 9]
[10 11 12]]

Searching Arrays

You can search an array for a certain value, and return the indexes that get a match. To search an array, use the where() method.

Find the indexes where the value is 4:

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

x = np.where(arr == 4)
print(x)

Output:

(array([3, 5, 6], dtype=int64),)

The value 4 is present at index 3, 5, and 6.

Find the indexes where the values are even:

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

x = np.where(arr%2 == 0)
print(x)

Output:

(array([1, 3, 5, 7], dtype=int64),)

Generating random numbers

NumPy offers the random module to work with random numbers. You will need to use the from keyword to import the random from numpy.

import numpy as np#Import Random Module
from numpy import random
x = random.randint(100)
print(x)

Output:

86

Generate Random Float

The random module’s rand() method returns a random float between 0 and 1.

x = random.rand()
print(x)

Output:

0.048781085334483776

Generate Random Array

In NumPy we work with arrays, and you can use the two methods from the above examples to make random arrays.

Integer

The randint() method takes a size parameter where you can specify the shape of an array.

x=random.randint(100, size=(5))

print(x)

Output:

[13 31 70 76 31]

Floats

The rand() method also allows you to specify the shape of the array.

x = random.rand(5)
print(x)

Output:

[0.64724056 0.96725784 0.52396417 0.57882588 0.41752778]

Generate Random Number From Array

The choice() method allows you to generate a random value based on an array of values.

x = random.choice([3, 5, 7, 9])
print(x)

Output:

9

If you enjoyed this post…it would mean a lot to me if you could click on the “claps” icon…up to 50 claps allowed — Thank You!

--

--

Shahzaib Khan

Developer / Data Scientist / Computer Science Enthusiast. Founder @ Interns.pk You can connect with me @ https://linkedin.com/in/shahzaibkhan/