# Introduction to NumPy – Knoldus Inc. – Medium

In this blog, I will walk you through the basics of NumPy. If you want to do machine learning then knowledge of NumPy is necessary. It one of the most widely used python library Numeric Python. It is the most useful library if you are dealing with numbers in python. NumPy guarantees great execution speed comparing it with python standard libraries. It comes with a great number of built-in function.

Advantages of using NumPy with Python:

- array-oriented computing
- efficiently implemented multi-dimensional arrays
- designed for scientific computation

First, let’s talk about its installation. NumPy is not part of a basic Python installation. We need to install it after the installation of python in our system. We can do it by pip using command pip install NumPy or it comes with conda as well.

We are done with the installation and now let’s jump right into NumPy. First, let start with the most important object in NumPy that is the ndarray or multi-dimensional array. Multi-dimensional array in similar words in an array of arrays which means array for example ** [1,2,3]** is a one-dimensional array because it contains only one row where two-dimensional array as given below contain multiple rows as well as multiple columns.

*[[1 2 3]*

*[4 5 6]*

*[7 8 9]]*

Let’s do some coding now. Here I am using Jupyter Notebook to run my code you can use any IDE available and best suited to you.

We start with import NumPy into our project first and in order to do so just write import NumPy.

In the following code, I am renaming the package as np just for my convince.

import numpy as np

Now in order to create an array in NumPy, we are call use it’s array function as mentioned below

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

print(array)

Output: [1 2 3]

This an example of a one-dimensional array.

Another way to create an array in NumPy is by using zeros function.

zeros = np.zeros(3)

print(zeros)

Output: [0. 0. 0.]

If you look closely at the output the generated array contains three zeros but the type of the value is a float and by default, NumPy creates the array of float values.

type(zeros[0])

Output: numpy.float64

Going back to the first example inside NumPy’s array function we pass a list so we can also pass the list variable inside array function and the output will be the same.

my_list = [1,2,3]

array = np.array(my_list)

print(array)

Output: [1 2 3]

Now let’s look into how to create a two-dimensional array using NumPy. Instead of passing the list now we have to pass a list of tuples or list of lists as mentioned below.

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

print(two_dim_array)

Output:

[[1 2 3]

[4 5 6]

[7 8 9]]

Note that the number of columns should be equal otherwise NumPy will create an array of a list.

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

print(arr)

Output: [list([1, 2, 3]) list([4, 6]) list([7, 8, 9])]

Now to create an array of a range which very good for making plot we use the linspace function.

range_array = np.linspace(0, 10, 4)

print(range_array)

Output: [ 0. 3.33333333 6.66666667 10. ]

Here the first argument is the starting point and next is the endpoint and the last argument defines how many elements you want in your array.

Now to create random arrays we can use random function. Here I created an array of random integers, therefore, used randint where first I specified the maximum value and then the size of my array.

random_array = np.random.randint(15, size=10)

print(random_array)

Output: [ 7 11 8 2 6 4 9 6 10 9]

Now we know the basic of how to create arrays in NumPy. Now let’s look into some of its basic operations. First, we will start by finding the size and shape of an array. Size will give the number of elements in an array where shape will give us the shape of an array.

For a one dimensional array, the shape would be (n,) where n is the number of elements in your array.

For a two dimensional array, the shape would be (n,m) where n is the number of rows and m is the number of columns in your array.

print(array.size)

Output: 3

print(array.shape)

Output: (3,)

print(multi_dim_array.size)

Output: 9

print(multi_dim_array.shape)

Output: (3, 3)

If we want to change the shape of an array we can easily do it with reshape function. It will look like something

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

two_dim_array = two_dim_array.reshape(4,2)

print(two_dim_array)

Output:

[[1 2]

[3 4]

[5 6]

[7 8]]

We need to make sure that the rows and columns can be interchangeable for example here we can change rows and columns from (2,4) to (4,2) but can not to (4,3) because for that we need 12 elements and here we have only 8. Doing so will give an error as mentioned below.

ValueError: cannot reshape array of size 8 into shape (4,3)

To check the dimension of our array we can use the **ndim** function.

print(two_dim_array.ndim)

Output: 2

Now to get values from array also known as slicing can be done in various ways. For example array[1] will fetch the second element of my array but if I want a range we can use array[0:1] which will bring me the first two elements. For the last value of the array, we can use array[-1] similar to the standard method of getting elements from the list in python.

Now to find the sum all we have to use is sum() function but if we want to find the sum of axis we can pass an argument for the axis.

print(two_dim_array.sum(axis=0))

Output: [ 6 8 10 12]

print(two_dim_array.sum(axis=1))

Output: [10 26]

Now to add two array all we have to use if + operator for example:

print(two_dim_array + two_dim_array)

Output:

[[ 2 4 6 8]

[10 12 14 16]]

Similarly, we can use other operands as well like multiple, subtract and divide.

We have many other operations present in NumPy like **sqrt** which will give us the square root of every element and **std** to find the standard deviation. To explore more about these operations visit the NumPy’s documentation.

And that’s it for the introduction of NumPy.

Resources: http://www.numpy.org/

## Leave a Reply