NumPy in Python

We offer you a brighter future with FREE online courses - Start Now!!

Have you ever come across arrays and matrices in mathematics? This article covers the module that helps us in doing different operations on matrices along with its creation.

Yes, Python provides a module named NumPy. We will learn about this module, and also different functions and methods we can use to handle arrays and matrices. So, let us start with an introduction to this module.

Introduction to NumPy Module

NumPy is a library that helps us handle large and multidimensional arrays and matrices. It provides a large collection of powerful methods to do multiple operations.

It stands for ‘Numeric Python’. It is a cross-platform module and contains tools to iterate with C and C++. It can be thought of as a Python alternative to MATLAB. It is used in different applications including linear algebra, Fourier transforms, manipulating shapes, and generating random numbers.

Installing and Importing NumPy

Learning about NumPy, you would be interested to know more by jumping into the coding part. But before coding, we need to install NumPy. We can install it by writing the below command.

pip install numpy

Once the installation is done, we can use it by importing.

import numpy

NumPy Arrays

NumPy arrays are n-dimensional arrays containing data of the same type in the form of rows and columns. We can create these arrays in the following way:

Example of creating a numpy array:

import numpy as np
#importing the module numpy and creating a short form as np

arr=np.array([1,2,3,4]) #creating a numpy array
print(f"The array is {arr} and the type is {type(arr)}")

Output:

The array is [1 2 3 4] and the type is <class ‘numpy.ndarray’>

Python Numpy Array

We can access its elements by indexing them in the same way we do with the lists and other ordered iterables.

Example of accessing the elements of a numpy array:

print("arr[0]:",arr[0])
print("arr[3]:",arr[3])

Output:

arr[0]: 1
arr[3]: 4

In addition, we can also find the shape and size of the array. The shape indicates the number of rows and columns of the array. Whereas, size indicates the number of elements in the array. The below example shows the way of finding it.

Example of finding shape and size of a numpy array:

print("Shape:",arr.shape)
print("Size:",arr.size)

Output:

Shape: (4,)
Size: 4

Here, we got the shape as (4,) as we have only one row.

Let us also have a look at an array with more than one dimension.

Example of a multidimensional numpy array:

import numpy as np

arr=np.array([[1,3,5],[0,2,4]]) 
print(f"The array is {arr}")
print(f"The type is {type(arr)}")
print("arr[1]:",arr[1])
print("arr[0][2]:",arr[0][2])
print("Shape:",arr.shape)
print("Size:",arr.size)

Output:

The array is [[1 3 5]
[0 2 4]]
The type is <class ‘numpy.ndarray’>
arr[1]: [0 2 4]
arr[0][2]: 5
Shape: (2, 3)
Size: 6

2-D NumPy Array in Python

Here we can see that the array is 2 dimensional and it has the lists nested one in another. Also, the shape is (2,3) showing the array has 2 rows and 3 columns. There is another way to access the values of the nested lists. The below example shows this.

Example of accessing elements from a multidimensional numpy array:

print("arr[1][1]:",arr[1,1])

Output:

arr[1][1]: 2

We can also specify some optional parameters to this function. Some examples are given below.
Example of the array() with optional parameters:

import numpy as np

np.array([1,2,3],ndmin=2) #setting the minimum number of dimensions 

Output:

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

Example of the array() with optional parameters:

import numpy as np

np.array([1,2,3],dtype=float) #setting the data type of the values of the array 

Output:

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

NumPy arrays vs Lists

Seeing the above concepts and examples, you would be wondering why we cannot use the lists instead? Why do we have to introduce a new concept of numpy array?

The following are the reasons for giving preference to the numpy array over lists:
1. Numpy arrays occupy lesser memory
2. They are faster
3. They are also comparatively convenient to use, especially when we deal with multiple dimensions

The first point says that numpy arrays occupy less space compared to the lists. To not leave any doubt, let us check this point with an example. We will create a numpy array and a list continuing the same elements and then we will check the space occupied by each of them.

Example of comparing the sizes of a numpy array and a list:

import numpy as np
import sys

list1= [1,2,3,4,5,6,7,8,9,10]
#finding the memory occupied by the list by multiplying the size of integer into number of integers in the list
print("Size of the list:",sys.getsizeof(int)*len(list1)) 

 
arr= np.array([1,2,3,4,5,6,7,8,9,10])
#finding the memory occupied by the list by multiplying the size of an element of the array with the size of the array
print("Size of the array:",arr.size*arr.itemsize)

Output:

Size of the list: 4160
Size of the array: 40

We can see a lot of difference in the space occupied by both the constructs though they hold the same values. Now let us also check the speed and convenience.

Example of comparing the speed and convenience of a numpy array and a list:

import numpy as np
import time
  
list1= range(10000,20000)
list2=range(10000)
arr1= np.array(list1)
arr2=np.array(list2)

s=0
start= time.time()
result=[(x,y) for x,y in zip(list1,list2)]
print((time.time()-start)*1000)

s=0
start=time.time()
arr=arr1+arr2
print((time.time()-start)*1000)

Output:

1.992940902709961
0.9975433349609375

We can see that the array is faster and also the operation of adding the arrays was easier than that done with the list.

Now we know the reason for learning the numpy arrays, let us discuss them in more detail.

More ways of creating NumPy arrays

Besides the np. array() function, there are many other ways of creating arrays in numpy. Let us see each of them.

1. Using arange():

We can get a range of values from the starting to ending specified values, we can use this function. For example,
Example of arange():

import numpy as np

np.arange(5)

Output:

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

2. Using random():

This function is used to get an array of specified dimensions containing random values.
Example of random():

import numpy as np

np.random.random((3,4))

Output:

array([[0.4441699 , 0.38858925, 0.38763883, 0.20776355],
[0.07331366, 0.6099461 , 0.21647803, 0.33916448],
[0.85049907, 0.57260728, 0.92850385, 0.4203164 ]])

3. Using ones():

This function is used to get an array of specified dimensions containing all the values equal to 1.
Example of ones():

import numpy as np

np.ones((3,2))

Output:

array([[1., 1.],
[1., 1.],
[1., 1.]])

4. Using zeros():

This function is used to get an array of specified dimensions with all the values equal to 0.
Example of zeros():

import numpy as np

np.zeros((2,3))

Output:

array([[0., 0., 0.],
[0., 0., 0.]])

5. Using eye():

We can create an identity matrix of the specified dimension using this function.
Example of eye():

import numpy as np

np.eye(5)

Output:

array([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])

6. Using full():

Using this function, we can create a matrix of the specified dimensions containing all the values equal to the given number.
Example of full():

import numpy as np

np.full((2,4),10)

Output:

array([[10, 10, 10, 10],
[10, 10, 10, 10]])

7. Using linspace():

We can create an array containing an evenly spaced certain number of values in the given interval.
Example of linspace():

import numpy as np

np.linspace(1,5,4)

Output:

array([1. , 2.33333333, 3.66666667, 5. ])

8. Using empty():

This function can be used to create an array of specified dimensions containing random uninitialized values.
Example of empty():

import numpy as np

np.empty([4,3])

Output:

array([[0.4441699 , 0.38858925, 0.38763883],
[0.20776355, 0.07331366, 0.6099461 ],
[0.21647803, 0.33916448, 0.85049907],
[0.57260728, 0.92850385, 0.4203164 ]])

Operations on Python NumPy Arrays

In this section, we will discuss the operations we can perform and functions we can use on the numpy arrays.

1. Checking Data type:

As said before, the numpy arrays hold data of the same type. We do not have to explicitly specify the data type, NumPy decides it. The data type could be any of the following:

bool_, int_, intc, intp, int8, int16, int32, int64, uint8, uint16, uint32, uint64, float_, float16, float32, float64, complex_, complex64, complex128.

We can check the data type of the variable using the ‘dtype’ function. It returns the data type along with the size as shown in the below example.

Example of dtype():

import numpy as np

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

Output:

int32

2. Finding Dimension:

We know that in NumPy we have an array of any dimension. It also has a function that lets us know the dimension of an array. And this function is ‘ndim’.

Example of ndim():

import numpy as np

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

Output:

3

3. Finding ByteSize:

We can find the byte size of every element of the array. We can do this by using the itemsize function.

Example of itemsize():

import numpy as np

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

Output:

4

4. Reshaping:

We can change the dimension of an array using the reshape function. The below code shows an example.

Example of the reshape():

import numpy as np

arr = np.array([(1,2,3,4),(9,8,7,6)])
print("Initial shape:",arr.shape)

arr=arr.reshape(4,2)
print("Shape after reshaping:",arr.shape)

Output:

Initial shape: (2, 4)
Shape after reshaping: (4, 2)

While reshaping, we need to give the dimension values such that the number of elements ( the size of the array) is the same before and after reshaping. If we give the wrong dimensions we get an error as shown below.

Example of reshape() giving an error on giving wrong dimensions:

import numpy as np

arr = np.array([(1,2,3,4),(9,8,7,6)])
print("Initial shape:",arr.shape)

arr=arr.reshape(4,3)
print("Shape after reshaping:",arr.shape)

Output:

Initial shape: (2, 4)

—————————————————————————
ValueError Traceback (most recent call last)
<ipython-input-31-6f832d55d294> in <module>
4 print(“Initial shape:”,arr.shape)
5
—-> 6 arr=arr.reshape(4,3)
7 print(“Shape after reshaping:”,arr.shape)

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

5. Memory Layout:

We also have a function to know how an array is stored in the memory. For this, we can use the flags() functions.

Example of flags():

import numpy as np

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

Output:

C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False

6. Slicing:

As said before, like an iterable we can use indexing to access elements of a numpy array. The below examples show slicing.

Example of slicing an array:

import numpy as np

arr=np.array([[1,2,3,4],[1,4,9,16],[1,8,27,64]])
print('arr[2]=',arr[2])
print('arr[1][3]=',arr[1][3])
print('arr[:2,1:]=',arr[:2,1:])
print('arr[2,:]=',arr[2,:])

Output:

arr[2]= [ 1 8 27 64]
arr[1][3]= 16
arr[:2,1:]= [[ 2 3 4]
[ 4 9 16]]
arr[2,:]= [ 1 8 27 64]

Now let us take a slice of the array and store it as a variable.

Example of slicing an array:

import numpy as np

arr=np.array([[1,2,3,4],[1,4,9,16],[1,8,27,64]])
arr1=arr[:,3]
print(arr1)

Output:

[ 4, 16, 64]

Now let us try modifying the new array using the index and check the original array.

Example of modifying an array:

import numpy as np

arr=np.array([[1,2,3,4],[1,4,9,16],[1,8,27,64]])
arr1=arr[:,3]
arr1[0]=-4
arr

Output:

array([[ 1, 2, 3, -4],
[ 1, 4, 9, 16],
[ 1, 8, 27, 64]])

We can see that modifying the slice changes the original array.

7. Indexing:

We can create an array from another array. There are two possibilities here. And these are integer indexing and boolean indexing.

a. Integer Indexing:

This is the way of creating a new array from the existing array(s) by using arbitrary integers as indexes. We can create an array(s) containing the required indexes and use them for indexing. For example,

Example of integer indexing to access values:

import numpy as np

arr1 = np.array([[1,3,5], [7,9,11], [2,4,6]])
ind = np.array([1,0,2])

'''
integer indexing by giving the first index as the range of values from 0 to 1 and 
the corresponding second indexes as the values of array ind
This will return all the values arr1[0,1], arr1[1,0] and arr1[2,2]
'''
arr3=arr1[np.arange(3), ind] 
arr3

Output:

array([3, 7, 6])

Using this concept, we can not only access the values but can also modify particular values. For example,

Example of integer indexing to modify values:

import numpy as np

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

'''
This will modify all the values arr1[0,0], arr1[1,1] and arr1[2,2]
'''
arr1[np.arange(3), np.arange(3)] +=100
arr1

Output:

array([[101, 3, 5],
[ 7, 109, 11],
[ 2, 4, 106]])
b. Boolean Indexing:

This is a method of returning a numpy array that contains boolean values that satisfy the specified condition.

Example of boolean indexing:

import numpy as np

arr1 = np.array([[1,-3,7], [7,-1,8], [2,-6,10]])
arr2=(arr1>0) #setting condition on the array
arr2

Output:

array([[ True, False, True],
[ True, False, True],
[ True, False, True]])

8. Arithmetic Operations:

We can addition, subtraction, multiplication, and division operations on two or more arrays of the same shape. This does the operation on the corresponding values of the arrays. For example,

Example of doing operations on arrays:

import numpy as np

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

print("Addition:")
print(arr1+arr2)

print("Subtraction:")
print(arr1-arr2)

print("Multiplication:")
print(arr1*arr2)

print("Division:")
print(arr1/arr2)

Output:

Addition:
[[ 0 0 0]
[ 8 10 12]]
Subtraction:
[[2 4 6]
[0 0 0]]
Multiplication:
[[-1 -4 -9]
[16 25 36]]
Division:
[[-1. -1. -1.]
[ 1. 1. 1.]]

9. Stacking:

Stacking is the operation of the concatenation of two or more arrays. We can do either horizontal or vertical stacking using the functions hstack() and vstack().

Example of stacking arrays:

import numpy as np

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

print("Vertical Stacking:")
print(np.vstack((arr1,arr2)))
print("Horizontal Stacking")
print(np.hstack((arr1,arr2)))

Output:

Vertical Stacking:
[[ 1 2 3]
[ 4 5 6]
[-1 -2 -3]
[-4 -5 -6]]
Horizontal Stacking
[[ 1 2 3 -1 -2 -3]
[ 4 5 6 -4 -5 -6]]

10. Ravel:

This is the function to convert the array into a one-dimensional array or an array with one row. The below code gives an example.

Example of Python NumPy ravel

Example of ravel():

import numpy as np

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

Output:

[1 2 3 4 5 6]

Mathematical Functions on NumPy arrays

The Python NumPy module provides various mathematical operations that we can perform with ease, rather than writing multiple lines of code. Let us discuss these functions.

1. add():

We can add two arrays using this function. Here the corresponding elements get added.
Example of add():

import numpy as np

a1 = np.array([[1,-2,3],[-4,5,6]])
a2=np.array([[3,5,8],[0,9,4]])
np.add(a1,a2)

Output:

array([[ 4, 3, 11],
[-4, 14, 10]])

2. subtract():

We can subtract two arrays using this function. Here the corresponding elements get subtracted.
Example of subtract():

import numpy as np

a1 = np.array([[1,-2,3],[-4,5,6]])
a2=np.array([[3,5,8],[0,9,4]])
np.subtract(a1,a2)

Output:

array([[-2, -7, -5],
[-4, -4, 2]])

3. multiply():

We can multiply two arrays using this function. Here the corresponding elements get multiplied.
Example of multiply():

import numpy as np

a1 = np.array([[1,-2,3],[-4,5,6]])
a2=np.array([[3,5,8],[1,9,4]])
np.multiply (a1,a2)

Output:

array([[ 3, -10, 24],
[ -4, 45, 24]])

4. divide():

We can divide the corresponding elements of the two arrays using this function.
Example of divide():

import numpy as np

a1 = np.array([[1,-2,3],[-4,5,6]])
a2=np.array([[3,5,8],[1,9,4]])
np.divide (a1,a2)

Output:

array([[ 0.33333333, -0.4 , 0.375 ],
[-4. , 0.55555556, 1.5 ]])

5. sqrt():

This function returns the array with the elements being square root values of the elements of the original array. The below code gives an example.

Example of sqrt():

import numpy as np

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

Output:

array([[1. , 1.41421356, 1.73205081],
[2. , 2.23606798, 2.44948974]])

6. sum():

This returns the sum of all the values of the array. For example,

Example of sum():

import numpy as np

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

Output:

21

7. max():

This returns the maximum of all the values of the array. For example,

Example of max():

import numpy as np

a1 = np.array([[1,2,-3],[4,-5,6]])
np.max (a1)

Output:

6

8. min():

This returns the min of all the values of the array. For example,

Example of min():

import numpy as np

a1 = np.array([[1,2,-3],[4,-5,6]])
np.min (a1)

Output:

-5

9. std():

This returns the standard deviation of all the values of the array. For example,

Example of std():

import numpy as np

a1 = np.array([[1,-3,5],[4,-2,6]])
np.std (a1)

Output:

3.435921354681384

10. T:

We can find the transpose of the matrix or array using the function. For example,

Example of T:

import numpy as np

a1 = np.array([[1,3],[4,5]])
a1.T

Output:

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

11. dot():

This function returns the dot product of two arrays. For example,

Example of dot():

import numpy as np

a1 = np.array([1,2,3])
a2=np.array([4,5,6])
a1.dot(a2.T)

Output:

32

In this example, we did transpose of the array a2 because we know that when we do dot product the arrays should have opposite dimensions.

Special Functions in NumPy

Besides arrays, NumPy also has functions like sine, log, etc. We can access any of these functions. The below example gives an example of using one of these functions and also plotting it using the matplotlib module.

Example of a special function:

import numpy as np
import matplotlib.pyplot as plt

teta= np.arange(0,2*np.pi,0.2) #creating an array holding the angles of the tan function
tan_func=np.tan(teta) #creating the tan function
plt.plot(teta,tan_func)
plt.show()

Output:

Special Functions in Numpy

Interview Questions on Python NumPy

Q1. Write a code to create an array of dimension 2×4 containing all zeros and is of integer type.
Ans. Below is the example of creating an array containing zeros:

import numpy as np

np.zeros((2,4), dtype=int)

Output:

array([[0, 0, 0, 0],
[0, 0, 0, 0]])

Q2. Write a program to scale all the diagonal values by 2.
Ans. Below is the example of scaling values of an array:

import numpy as np


arr1 = np.array([[1,2,3,5], [5,6,11,9], [2,9,6,0],[9,4,3,2]])
arr1[np.arange(4), np.arange(4)] *=2
arr1

Output:

array([[ 2, 2, 3, 5],
[ 5, 12, 11, 9],
[ 2, 9, 12, 0],
[ 9, 4, 3, 4]])

Q3. Write a program to create an array of equally spaced values from 1 to 5 and plot the square function.
Ans. Below is the example of using linspace:

import numpy as np
import matplotlib.pyplot as plt

a=np.linspace(1,5,21)
func=a*a
plt.plot(a,func)

Output:

Numpy Linspace

Q4. Write a code to convert a 2 dimensional array into a 1 dimensional array.
Ans. Below is the example of raveling array:

import numpy as np

a=np.array([[1,2,3,4],[8,9,3,2]])
a.ravel()

Output:

array([1, 2, 3, 4, 8, 9, 3, 2])

Q5. Write a function to get an array containing boolean values obtained by comparing two arrays using the ‘>’ symbol.
Ans. Below is the example of comparing arrays:

import numpy as np

a=np.arange(10)
b=np.arange(10,0,-1)
a>b

Output:

array([False, False, False, False, False, False, True, True, True,
True])

Quiz on Python NumPy

Conclusion

We are at the end of the article. We learned a lot of concepts related to numpy including creating nD arrays, doing operations on them, and applying functions on them. We also got to know some special functions in this module.
In the end, we practiced some questions. Hoping that you learned some new concepts by reading this article. Happy coding!

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google | Facebook


Leave a Reply

Your email address will not be published. Required fields are marked *