Labaran Adam

Fell in love with a dumb machine that does exactly what I instruct it to do, when and how. Hoping our relationship makes the world a better place. || SOFTWARE ENGINEER

Ultimate Guide To Numpy For Machine Learning

March 27, 2020

Numpy (Numerical Python) is a linear Algebra Library for python programming. Numpy helps with scientific computation with python. It is incredibly fast as it has binding C libraries. Numpy is one of the most use python libraries for Data-science, almost all the libraries in the PyData ecosystem depends on it. Numpy is more efficient and convenient as it provides a lot of vector and matrix operations. Numpy array are Homogeneous as they can only take one data type at a time

Installation

In order to be able to use NumPy in your program, you first have to install it. Install NumPy by going to your terminal or command prompt and type

pip install numpy

Or

conda install numpy

When working with Anaconda distribution

In order to follow along with this tutorial , you first have to create a python file, with your preferred IDE,

Using Numpy

To use NumPy we first have to import it in our python file.

import numpy as np

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


Numpy Array

One of the main reason we will be using numpy is with array creation. There are many ways we can create an array with numpy . Let look at the various method 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 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 take a look at some various methods one after another.

Arange

Just like python range method, given an interval, returns evenly spaced values. Takes in start (start of the interval and it is inclusive in the interval to be generated ), stop (This number is the end of the interval and it is not inclusive in the interval to be generated) and step (This is an optional argument you can pass, it tells the spacing between the values in the interval ) as an argument.

let look at 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]


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

Since all the three-argument where provided, in which the third argument is the step argument. This will generate numbers from 0 to 11 inclusive and exclusive respectively with a step of 2. Basically 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 pass to the zeros method and an argument, it returns a one-dimensional array of the number.

np.zeros((4,3))

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

Ones

Ones, just like zeros will return an array filled with ones given a shape.

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

Full

What 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.

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 (4,5) which means four rows and five columns. The value three (3) will be filled in all the entries of the array

linspace

linspace return evenly space point numbers over a given interval. Takes in 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. It has a default of 50)

np.linspace(0,5,5)

Given a range of 0 t0 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 which has all the values of it diagonals as one and the other entries as zero.

np.eye(4)

You pass the number of rows as an argument and it will return an identity matrix base 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 look at some useful methods of an array

Size

The size method returns the number of elements in an array, it does not require any argument unless you are calling it from the numpy class

array1.size

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

np.size(array1)

either way, is accepted.

Reshape

The reshape method is used to change the dimension of an array, can be used to convert one-dimensional array to a two-dimensional array. Takes the 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. Can use the size method to check to see how many shapes you can transform a given array.

max

This return the maximum number in an array

array1.max()

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


Min

Returns the minimum number in an array

array1.min()

argmax

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

array1.argmax()

argmin

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

array1.argmin()

Indexing

Indexing of a NumPy array is the same with python list, or any sequence in python. To access a value in an array we use its index. In order to that, we pass the value of the index in a square bracket just like python list.

array1[4]

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

Numpy also accept range indexing, that is getting values in a range using the sliced 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.

Substraction

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 results in an error but will return nan

Built-in Mathematical functions

Numpy comes with many built-in mathematical functions. You can find a full 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

This method is use to find the log of every entry in an array

np.log(array1)

Conclusion

I believe this covered most of the basics of NumPy, you can find more from their documentation here

Share This Post