NUMPY

NumPy (or Numpy) is a Linear Algebra Library for Python. NumPy stands for Numerical Python. It contains a multi-dimensional array and matrix data structures. It can be utilised to perform a number of mathematical operations on arrays such as trigonometric, statistical, and algebraic routines.

NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.

Prerequisite: Jupyter Notebook is required for learning numpy.

How will you Import Numpy library?

import numpy as np

What is Numpy array?

Numpy arrays come in two forms: vectors and matrices.

Vectors are 1-dimentional arrays and matrices are 2-dimentional arrays.

How to create a numpy array using a python list?

Creating a numpy array from a python list:

  1.  One dimensional array
Let’s take a python list list1:

 list1 = [2,4,6,8,10]

 print(list1)

Output: [2, 4, 6, 8, 10]

numpy_arr=np.array(list1)

print(numpy_arr)

Output: [ 2  4  6  8 10]

 2-dimensional array

matrix1 = [[1,2,3], [4,5,6], [7,8,9]]

print(matrix1)

Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

np.array(matrix1)
print(matrix1)
Output: 
array([[1, 2, 3],

[4, 5, 6],

[7, 8, 9]])

Built in methods for creating an array in Numpy

How do you use arange function in Python?

arange-arange function returns evenly spaced values within a given interval.

Syntax:

arange([start,] stop [, step,], dtype=None)

Example:

np.arange(0,10)

Output: array ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

np.arange(0,21,2)

Output: array ([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20])

What are Numpy zeros and ones functions?

Zeros function return a new array of given shape and type, filled with zeros.

Example:

  1. np.zeros(5)

This will return an array of 5 filled with zeros.

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

  •  np.zeros((5,4))

This will return a matix of 5 rows and 4 columns filled with zeros.

Output:

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

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

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

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

                                          [0., 0., 0., 0.]])

  Ones function return a new array of given shape and type, filled with ones.

Example:

  1. np.ones(5)

This will return an array of 5 filled with one.

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

  • np.ones((4,4))

This will return a matix of 4 rows and 4 columns filled with one.

 Output:  array ([[1., 1., 1., 1.],

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

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

                                             [1., 1., 1., 1.]])

What does NumPy Linspace return?

Linspace

             Returns evenly spaced numbers over a specified interval.

Example:

np.linspace(0,10,5)
This will return 5 evenly spaced number between 1 to 10.

Output: array ([ 0., 2.5, 5., 7.5, 10.])

np.linspace(0,20,21)
        This will return 21 evenly spaced number between 0 to21

Output: array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20.])

How do I create an identity matrix in NumPy?

We can create an identity matrix using eye function.

Example: np.eye(4)

This will return a 2-D array with ones on the diagonal and zeros elsewhere.

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

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

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

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

How do you generate a random number in Numpy?

There are multiple ways to generate:

  1. rand()

rand() function is used to generate an array of the given shape and populate it with random samples from a uniform distribution over “[0, 1)“.

Example:

  1. np.random.rand(5)
array([0.75768371, 0.08661447, 0.0269465 , 0.02594679, 0.86623105])
  • np.random.rand(3, 3)

array([[0.38169555, 0.74914437, 0.78573338],

            [0.03406496, 0.64432113, 0.67194466],

            [0.42780565, 0.49245212, 0.28861278]])

2. randn()

Return a sample (or samples) from the “standard normal” distribution. Unlike rand which is uniform.

Example:

  1. np.random.randn(4)

              array([-0.21680073, 1.19005641, 0.25593789, -1.22919086])

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

              array([[-0.20485846, -0.15928615, -0.07235331],

                          [-0.08408529, -0.68321452, 0.67376678],

                          [ 0.9076339 , 0.30330106, -0.0903779 ],

                          [ 0.47459555, -0.40872827, -0.02688751]])

 

  • randint()

Returns random integers from low (inclusive) to high (exclusive).

Example:

  1. np.random.randint(1,100)

45

  1. np.random.randint(1,200,10)

                      array([110, 124, 189, 35, 105, 130, 30, 82, 107, 172])

Array Attributes and Methods

arr1 = np.arange(25)

arr1

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

arr2 = np.random.randint(0,100,10)

arr2

Output: array([32, 52, 49, 5, 1, 92, 40, 38, 44, 75])

What is the use of reshape in Python?

Reshape returns an array containing the same data with a new shape.

arr.reshape(5,5)

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

            [ 5, 6, 7, 8, 9],

            [10, 11, 12, 13, 14],

            [15, 16, 17, 18, 19],

            [20, 21, 22, 23, 24]])

What is the difference between argmax and max?

argmax function returns the index location of the maximum value in the array whereas max function returns the maximum value in the given array.

For example:

Lets take an array arr1 and arr2 using arange function from  0 to 25 and randint function respectively

arr1 = np.arange(25)

arr1

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

arr2 = np.random.randint(0,100,10)

arr2

Output: array([32, 52, 49, 5, 1, 92, 40, 38, 44, 75])

We can find the maximum value in the array using max function and its index using argmax

arr1.max()

24

arr1.argmax()

24

arr2.max()

92

arr2.argmax()

5

What is the difference between argmin and min?

argmax function returns the index location of the minimum value in the array whereas max function returns the minimum value in the given array.

For example:

Let’s take an array arr1 and arr2 using arange function from  0 to 25 and randint function respectively

arr1 = np.arange(25)

arr1

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

arr2 = np.random.randint(0,100,10)

arr2

Output: array([32, 52, 49, 5, 1, 92, 40, 38, 44, 75])

We can find the minimum value in the array using max function and its index using argmax

arr1.min()

0

arr2.min()

1

arr2.argmin()

4

Explain the use of reshape for a numpy array.

Shape is an attribute that arrays have (not a method).

Example:

  1. arr1.shape

                   (25,)

  1. arr1.reshape(1,25)

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

  1. arr1.reshape(1,25).shape

                    (1, 25)

                    arr1.reshape(25,1).shape

                    (25,1)

What is the use of dtype in Python?

It displays the data type of the object in the array.

Example:

arr2.dtype

dtype(‘int32’)

How indexing and selection is used in a Numpy?

Selecting one or some elements of an array is like python lists.

For example,

First let’s create an array of 10 elements.

import numpy as np

num = np.arange(0,11)

num

output: array ([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

for selecting a value say 5, we have to give its index:

num[5]

output: 5

for selecting values in a range :

num[1:5]

output: array([1, 2, 3, 4])

How to set a value with index range?

num[0:5]=25

num

output: array([25, 25, 25, 25, 25,  5,  6,  7,  8,  9, 10])

Explain indexing in 2-d array.

Create a 2-d array

arr_2d = np.array(([5,10,15],[10,20,30],[15,30,45]))

arr_2d

output:

array([[ 5, 10, 15],

            [10, 20, 30],

            [15, 30, 45]])

  1. For indexing row:

arr_2d[2]

output: array([15, 30, 45])

  • To get an individual column say 30, we must give its column number along with the row number.

arr_2d[2][1]

output: 30

  • Slicing arrays

arr_2d[:2]

outputarray([[ 5, 10, 15],

                        [10, 20, 30]])

This will slice the last column from the array

arr_2d[:,1:]

output: array([[10, 15],

                           [20, 30],

                           [30, 45]])

We have sliced the first column here.

Now slicing from top right,

arr_2d[:2,1:]

output: array ([[10, 15],

                           [20, 30]])

In simple, we can say like for rows start from 0 and take all up to 2 (not included) and for column start from the 1’st column (included) till the end.

Explain 2-d array selection based off comparison operators.

arr = np.arange(1,20,2)

array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19])

arr

output: array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19])

bool_arr = arr> 10

bool_arr

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

arr[bool_arr]

output: array([11, 13, 15, 17, 19])

What are the operations that we can perform on numpy?

We can easily perform array with array arithmetic, or scalar with array arithmetic.

import numpy as np

arr = np.arange(0,10)

arr + arr

output: array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])

arr * arr

output: array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81])

arr – arr

output: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

arr/arr

output: array([nan,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])

divide by zero will not give any error but replace it by a nan

also for

1/arr:

outputarray([        inf,  1.        ,  0.5       ,  0.33333333,  0.25      ,

        0.2       ,  0.16666667,  0.14285714,  0.125     ,  0.11111111])

arr**2

Output: array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81], dtype=int32)

Some universal array functions:

  1. Taking square roots:
np.sqrt(arr)

output: array([ 0.        ,  1.        ,  1.41421356,  1.73205081,  2.        ,

        2.23606798,  2.44948974,  2.64575131,  2.82842712,  3.        ])
  • Calcualting exponential (e^)
np.exp(arr)

output: array([  1.00000000e+00,   2.71828183e+00,   7.38905610e+00,

         2.00855369e+01,   5.45981500e+01,   1.48413159e+02,

         4.03428793e+02,   1.09663316e+03,   2.98095799e+03,

         8.10308393e+03])
  • Calculating log
np.log(arr)

output: array([       -inf,  0. ,  0.69314718,  1.09861229,  1.38629436,

        1.60943791,  1.79175947,  1.94591015,  2.07944154,  2.19722458])

Question: Let’s say you have a two dimensional numpy array called “twod” and you want to split it row-wise into two equal halves. Then, which of these numpy functions would you call on it to do so?

rowsplit(twod,2)

hsplit(twod,2)

vsplit(twod,2)

colsplit(twod,2)

Ans:- vsplit(twod,2)

Question: Some of the features of digital images in Numpy are given below. Which of these are true?

In numpy, images can be represented as a 3D matrix where the first two dimensions represent the pixels in the image that are arranged in the form of a grid and the third dimension specifies the number of channels for the image

A digital image is a multidimensional array and every pixel in a digital image is represented by a number

In a grayscale image, three values (for Red, Green and Blue) are used to represent a pixel in an image whereas for a colour image, a single value is used to represent a pixel

Ans:-

In numpy, images can be represented as a 3D matrix where the first two dimensions represent the pixels in the image that are arranged in the form of a grid and the third dimension specifies the number of channels for the image

A digital image is a multidimensional array and every pixel in a digital image is represented by a number

Question: Let’s say you have an image that you have split into two equal halves along the x axis. You have stored these two halves of the original image in the variables x1 and x2 respectively. Which numpy function would you use to combine these two halves to reconstruct the original image?

concatenate((x1,x2))

concatenate((x2,x1),axis=1)

concatenate((x1,x2),axis=1)

concatenate((x2,x1))

Ans:- concatenate((x1,x2),axis=1)

Question: Let’s say you have a numpy array called “array_1” and you initialize “another array called “array_2” with the help of a following command:
array_2 = array_1.view()
Match the following statements about “array_2” with the correct Boolean value
Instruction: Match each option with its correct target. Each category may have more than one match.
Answer Options:
A:array_1 and array_2 contain the same elements
B:If we re-assign array_2, then we will end up re-assigning array_1 as well and change its contents
C:array_2 points to the same object as array_1
D:The base for array_2 points to the same object as array_1

True

A

B

C

D
Ans:- A,D

False

A

B

C

D
Ans:- B,C

Question: Let’s say you have a numpy array called “array_3” and you initialize “array_4” with the help of a following command:
array_4 = array_3.copy()

Match the following statements about “array_4” with the correct Boolean value

Answer Options:
A:array_3 and array_4 contain the same elements
B:If we re-assign array_4, then we will end up re-assigning array_3 as well and change its contents
C:If we change a single element of array_4, then the corresponding element in array_3 changes too
D:Changing the shape of array_4 will change the shape of array_3 as well

False

A

B

C

D
Ans:- B,C,D
True

A

B

C

D
Ans:- A

Question: Let’s say you have a 1-D numpy array called “cubes” consisting of the cubes of the numbers 1,2,3 and so on till 10. What would be the value of the array:
cubes [ [ [ 4, 5 ], [ 1, 2 ] ] ]

[ [ 64, 125], [ 1, 8] ]

[ [ 125, 216] , [ 8, 27] ]

The program will show an error since we can’t use a 2-D array to index a 1-D array

[ 125, 216 , 8, 27 ]

Ans:- [ [ 125, 216] , [ 8, 27] ]

Question: Some of the features of Pandas is given below. Which of these are true?

A Numpy array is an abstraction on top of a pandas dataframe

A particular column of a pandas dataframe can be referenced by its column header

The column header of a Pandas dataframe can be treated in the same way as the index label of a numpy array

Numpy is built on top of Pandas

Ans:-

A particular column of a pandas dataframe can be referenced by its column header

The column header of a Pandas dataframe can be treated in the same way as the index label of a numpy array

Question: Let’s say you imported numpy as np and you have initialized a 1-D array of integers called “array”. What would np.all (x < 50) return?

This function would return a true boolean value if your array contains less than 50 elements and false otherwise

This function would return true if there is at least one entry in your array that is less than 50 and false otherwise

This function would return a true boolean value if all the entries in your array are less than 50 and false otherwise

This function would return a true boolean value if all the entries in your array are greater than 50 and false otherwise

Ans:- This function would return a true boolean value if all the entries in your array are less than 50 and false otherwise

Question: Let’s say you have a Pandas dataframe called “phone_data” which contains the data of various phones released in 2018 and their prices. It has the following three columns:
“manufacturer”, “phone name” and “ price”.
You want only the names of all the phones that are priced more than 10,000. Which of these commands can be used to print these values?

phone_data[‘price’][‘phone name’] > 10000

phone_data[‘price’] > 10000

[phone_data[‘price’] > 10000][‘phone name’]

phone_data[phone_data[‘price’] > 10000][‘phone name’]

Ans:- phone_data[phone_data[‘price’] > 10000][‘phone name’]

Question: What are the conditions under which broadcasting can take place between two elements in Numpy?

Broadcasting works when at least one of the elements is a scalar

Broadcasting works between any two two-dimensional arrays

A smaller array can be broadcast on a larger array only when the corresponding dimensions of the two arrays being operated upon are compatible i.e. when the corresponding dimensions are equal or one of the two dimensions is 1

Broadcasting works when both the elements are scalars

Ans:-

Broadcasting works when at least one of the elements is a scalar

A smaller array can be broadcast on a larger array only when the corresponding dimensions of the two arrays being operated upon are compatible i.e. when the corresponding dimensions are equal or one of the two dimensions is 1

Question: Match the following statements about broadcasting with the correct Boolean value:

Answer Options:
A:The array [ [ 1, 2] , [ 3, 4] ] and the scalar 10 are incompatible with broadcasting
B:The scalar 10 and the scalar 20 are compatible with broadcasting
C:The array [ [ 1, 2] , [ 3, 4] ] and the array [ 1, 2 ,3 ] are incompatible with broadcasting
D:The array [ [ 1, 2] , [ 3, 4] ] and the array [ [1], [2] ] are compatible with broadcasting

False

A

B

C

D
Ans:- A

True

A

B

C

D
Ans:- B,C,D

Question: Which of these correctly match the following libraries in the Numpy ecosystem with what that library is used for?

Answer Options:
A:Statsmodel
B:Scikit-image
C:Matplotlib

Data visualization tool used for plotting 2-D graphs

A

B

C

Ans:- C

Contains a collection of algorithms used for image processing

A

B

C

Ans:- b

Used to perform statistical operations

A

B

C

Ans:-A

Question: Let’s say you have imported the numpy package as np and you want to assign the variable “x” with a 3 by 2 array of type integer, all of whose values are 1. Which of these commands will you use to do so?

x=np.ones((2,3),dtype=np.int32)

x=np.ones((3,2))

x=np.ones((3,2),dtype=np.int32)

x=np.ones((2,3))

Ans:- x=np.ones((3,2),dtype=np.int32)

What will be the value stored in the variable y after we have executed the following code

import numpy np

y=np.arange(2,4,0.5)

[2, 2.5, 3, 3.5, 4 ]

[0.5, 2.5]

[ [0.5, 0.5, 0.5, 0.5], [0.5, 0.5, 0.5, 0.5] ]

[2, 2.5, 3, 3.5]

Ans:- [2, 2.5, 3, 3.5]

Let’s say you have imported the numpy package as np and you want to print the first 2000 natural numbers in the form of an array and you want all 2000 of the numbers to be visible on screen when printing (including the number 2000). Which of these commands would you use to do so?

print(np.arange(1,2000))

print(np.arange(2000))

np.set_printoptions(threshold=np.nan) print(np.arange(1,2000))

np.set_printoptions(threshold=np.nan) print(np.arange(1,2001))

Ans:- np.set_printoptions(threshold=np.nan) print(np.arange(1,2001))

What would be the output of the following code:

import numpy as np
x=np.array([[1,2] , [3,4]])
y=np.array([ [5,6],[7,8]])
z=(x*y)
z

[ [5,12], [21,32] ]

[ [4, 10] , [1, 18] ]

[ [0,0] , [7, 16] ]

[ [19,22] , [43, 50] ]

Ans:- [ [5,12], [21,32] ]

What would be the output of the following section of code:

Import numpy as np
x=np.array([4,6,2,8])
np.median(x)

2

6

5

4

Ans:- 5

Which of these slicing operations can be used to quickly get the reversed contents of a numpy array called “array”?

array[ ::-1]

array[ ::]

array[ -1:0]

array[ ::1]

Ans:- array[ ::-1]

Match the following features of the numpy nditer function mentioned here with the correct Boolean category

Answer Options:
A:The nditer can accept only one dimensional arrays as input
B:Using this function, we can iterate through each of the individual elements of the array passed as an input argument
C:By default, the nditer object returns arrays that can be written on

False

A

B

C
Ans:- A, C

True

A

B

C
Ans:- B

Which of these statements regarding the ravel() object in Python are true?

ravel() belongs to the numpy library and can be used on any object that can be parsed

ravel() belongs to an array object and can only be used on numpy arrays

The ravel function reduces a multi-dimensional array to a single dimensional array

The ravel function converts a single dimensional array to a multidimensional array

Ans:-

ravel() belongs to the numpy library and can be used on any object that can be parsed

The ravel function reduces a multi-dimensional array to a single dimensional array