The ultimate guide to NumPy for Machine Learning

In this Writer's Room blog, Andela Community member Adam Labaran explores the value and benefits of using NumPy, a library for Python supporting large, multi-dimensional arrays and matrices, in this ultimate guide to NumPy.

Introduction to NumPy

NumPy (Numerical Python) is a linear Algebra Library for Python programming. It helps with scientific computation with Python and is incredibly fast due to its binding C libraries. NumPy is one of the most commonly used Python libraries for Data Science, and almost all the libraries in the PyData ecosystem depend on it. NumPy is more efficient and convenient than other libraries because it provides a lot of vector and matrix operations. NumPy libraries are homogeneous, as they can only take one data type simultaneously.

Installation

To use NumPy in your program, you first have to install it. Install NumPy by going into your terminal or command prompt and typing the following:

pip install numpy

or

conda install numpy

When working with Anaconda distribution:

To follow this tutorial correctly, you must create a Python file with your preferred IDE.

Using NumPy

To use NumPy, we first have to import it into our Python file.

import numpy as np

Importing numpy as np, is universally accepted as the standard way to import NumPy and use it in your application. By importing the library, you get access to all its functionalities. Now let's dive into what we can do with NumPy.

NumPy array

One of the main reasons we will be using NumPy is for array creation. There are many ways we can create an array with NumPy. Let's look at the various methods we can use to do that.

From a Python List

list1 = [1,3,4,5]array1 = np.array(list1)

Here we created a Python list called list1. To convert a list into a NumPy array, we have to pass the list to the numpy.array() method, which is what we did in the second line of code.

Hint: You can use the Python type method to check the data type of both list1 and array1.

print(type(list1))print(type(array1))

This tells us that list1 is a Python list and array1 is a NumPy array.

Using NumPy built-in methods

NumPy comes with a lot of built-in methods to generate arrays. Let's take a look at various methods, one after another.

Arange

Like the Python range method, arange returns evenly spaced values given an interval. It takes the start ( inclusive ) and stop (exclusive) values of the interval, as well as an optional step argument that specifies the spacing between the values.

Here's how we can generate an array with the arange method:

np.arange(0,10)

Here we only pass two positional arguments, start and stop. This will generate numbers ranging from 0 inclusive and 10 exclusives. The output will be [0,1,2,3,4,5,6,7,8,9].

array1 = np.arange(0,10,2)

The above example provided all three arguments, including the step argument. This will generate numbers from 0 to 11 inclusive and exclusive, respectively, with a step of 2. This will generate even numbers less than 11.

Zeros

Given a shape, the zeros method will return an array filled with zeros.

np.zeros(4)

When a single number is passed to the zeros method and an argument, it returns a one-dimensional array of the number.

np.zeros((4,3))

You can also get your preferred dimension by passing a tuple with your desired rows and columns; this will return an array filled with zeros of four rows and three columns.

Ones

Similar to zeros, the one's method returns an array filled with ones given a shape.

np.ones(5)np.ones((4,3)

Full

If we don't want to fill our array with only ones and zeros, the full method helps us fill our array with our desired number. It takes a shape and a fill value as an argument. Here's an example:

np.full(4,3)

This will output a one-dimensional array of 4 columns containing the value 3 only.

np.full((4,5),3)

You can also pass the dimension you want as a tuple, followed by the number you want to be filled. For example, (4,5) means four rows and five columns. All the array entries will fill the value three(3).

Linspace

The linspace returns evenly spaced point numbers over a given interval. Takes in a three-argument: start (this is the starting value of the sequence), stop (this is the stop value of the sequence), and num (this tells how many evenly spaced numbers should be returned from the given interval. The default value for num is 50).

np.linspace(0,5,5)

Given a range of 0 to 5 inclusive, return 5 evenly spaced numbers between this range.

Eye

The eye method is used to create an identity matrix. An identity matrix is a square matrix that has all the values of its diagonals as one, and the other entries as zero.

np.eye(4)

You pass the number of rows as an argument, which will return an identity matrix based on the number of rows.

Diag.

What if we don't want to return only ones as our diagonal? The diag method can be used to return a square matrix of our desired diagonals

np.diag([1,2,3,4,5,6,7])

This will return a square matrix with the values 1,2,3,4,5,6,7.

Some useful methods

Let's look at some useful methods of an array.

Size

The size method returns the number of elements in an array. Unless you call it from the NumPy class, it does not require any argument.

array1.size

This will return the size of the array, which is the element count in the array. The size method can also be called from the NumPy class directly.

np.size(array1) .

Either way is acceptable.

Reshape

The reshape method changes an array's dimension and can convert a one-dimensional array to a two-dimensional array. It takes a new shape as an argument.

array1.reshape(2,5)

Hint: Before an array can be reshaped, the multiplication of its dimension must be equal to its size. You can use the size method to see how many shapes you can transform a given array into.

Max

This returns the maximum number in an array

array1.max()

This will return 9 since 9 is the maximum number in the array.

Min

This method returns the minimum number in an array.

array1.min()

Argmax

This method will return the index of the maximum values in an array.

array1.argmax()

Argmin

This method will return the index of the minimum number in an array.

array1.argmin()

Indexing

Indexing of a NumPy array is the same as a Python list or any sequence in Python. To access a value in an array, we use its index. To do that, we pass the value of the index in a square bracket, just like the Python list.

array1[4]

By doing this, we are accessing the value at the index of 4 in the array.

NumPy also accepts range indexing, which means getting values in a range using the slicing notation, just like a Python list.

array1[1:5]

This will return the values of array1 from index 1 to 5 exclusive.

Arithmetic Operation

All basic mathematical operations can be performed with NumPy array.

Addition

array1 + array1

The addition method will add the various corresponding entries of each array. The addition also can be done with a scalar number.

Subtraction

array1- array1

The subtraction operation will also subtract various corresponding entries of each array.

Multiplication

array1 * array1

All the entries in the first array will be multiplied by their corresponding entries in the second array. NumPy array also supports scalar multiplication.

Division

array1/array1

All the entries in the first array will be divided by their corresponding entries in the second array. Division by zero won't result in an error but will return a nan.

Built-in mathematical functions

NumPy comes with many built-in mathematical functions. You can find a complete list of the mathematical functions here.

sqrt

This method helps find the square root of every entry in an array.

np.sqrt(array1)

Sin

This method helps find the sin of every entry in an array.

np.sin(arr)

Log

You can find the log method's logarithm of every entry in the array.

np.log(array1)

Conclusion

NumPy is an essential library for scientific computing and data analysis in Python. Its creative and manipulation capabilities make it a powerful tool for handling large data sets efficiently. I believe this article covered most of the basics of NumPy; you can find more on their website here. Happy Coding!

Want to be part of a vibrant tech community?

Then join the Andela Talent Network!

Find Work

Related posts

The latest articles from Andela.

Visit our blog

Writer's Room: 5 tips for technical writing

Are you a technologist craving to share your work with the world? Then turn your technical project into a blog with our 5 top tips for technical writing.

Asynchronous and synchronous communication in the remote workplace

While synchronous collaboration was the preferred method for many global organizations, remote work has increased the popularity of asynchronous communication. But which is more beneficial, both to employees, and to business?

Navigating the tech landscape: Andela’s predictions for 2024

What will the tech industry look like in 2024? Read our predictions on how work structures, African tech talent, human-GenAI collaboration, and diversity and inclusion will shape the future of technology and your team.

We have a 96%+
talent match success rate.

The Andela Talent Operating Platform provides transparency to talent profiles and assessment before hiring. AI-driven algorithms match the right talent for the job.