This page contains the NCERT Informatics Practices class 11 chapter 6 Introduction To NumPy. You can find the solutions for the chapter 6 of NCERT class 11 Informatics Practices Exercise. So is the case if you are looking for NCERT class 11 Informatics Practices related topic Introduction to NumPy questions and answers for the Exercise. If you’re looking for case study solutions in this chapter, you can find them at Case Study Solutions
Exercise
1. What is NumPy? How to install it?
NumPy standing for ‘Numerical Python’ is a package for data analysis and scientific computing with Python. NumPy
●
uses a multidimensional array object
●
an has functions and tools for working with these arrays.
The array used by NumPy is powerful and it speeds-up data processing.
NumPy can be installed by typing the following command in the terminal or command prompt (pre-requisite is that Python should have been already installed in the machine).
pip install NumPy
And then press Enter.
This will start installing the NumPy.
2. What is an array and how it is different from a list? What is the name of the built-in array class in NumPy?
An array is a data type used to store multiple values using a single identifier (variable name). The following are the important characteristics of an array.
●
Each element in the array is of the same data type. Their values can be different.
●
The entire array is stored contiguously in the memory. In otherwords, each element of the array is arranged one beside the other in the memory. This makes the operations on the array fast.
●
Each element is referreed by the array name (variable name) along with the index of the element. This will be unique for each element.
A NumPy array is different from a list in the following ways:
Basis
List
Array
Stored Data Type
Lists can have elements of different data types. For instance
1, 3, 4, 'hello', 'a@'
All elements of an array are of same data type. For example, an array of floats may be :
[1.2, 5.4, 2.7]
Contiguous Storage in Memory
The elements of a a list are not necessarily stored continuously in memory.
Array elements are stored in contiguous memory locations.
Speed of Operations
As the elements are not stored contiguously in memory, the operations on lists of are slightly slower as compared to the operations on arrays.
As the elements are stored in contiguous memory locations, the operations on arrays are faster than lists.
Element wise Operations
Lists do not support element wise operations like addition, multiplication etc. This is because, the elemnents may not be of the same type.
On the other hand, arrays support element wise operations like addition, multiplication, division by a number etc. For instance, if
A1
is an array, the code A1/3
denotes that each element of the array is divided by 3.Memory Required
As each element in a list can be of different data type, the python program must store the type information for every element along with its value. Due to this, the lists take more space in memory and are relatively less efficient.
On the otherhand, as each element in the NumPy array is of the same datatype, the arrays need to store the datatype information only once (not required for each element). Thus the amount of space required by the NumPy arrays is comparatively less. This makes them relatively more efficient as compared to the lists.
Module
Lists are part of core Python
Array(ndarray) is a part of NumPy library.
The name of the built-in array class in NumPy is ‘
ndarray
‘, generally referred to as ‘array
‘3. What do you understand by rank of an ndarray?
An ‘
ndarray
‘ can be of 1-Dimention or multiple dimensions. Thus it can be 1-D, 2-D or n-D. NumPy calls the dimensions as axes (plural of axis). Thus a 2_D array has two axes. The row-axis is called axis-0 and the column-axis is called axis-1. The number of axes is also called the array’s rank.4. Create the following NumPy arrays:
a)
A 1_D array called
zeros
having 10 elements and all the elements are set to zero.b)
A 1_D array called
vowels
having the elements ‘a’, ‘e’, ‘i’, ‘o’, ‘u’.c)
A 2_D array called
ones
having two rows and five columns and all the elements are set to 1 and dtype as int.d)
Use nested Python lists to create a 2-D array called
myarray1
having 3 rows and 3 columns and store the following data:2.7,
2,
-19
0,
3.4,
99.9
10.6,
0,
13
e)
A 2_D array called
myarray2
using arange()
having 3 rows and 5 columns with start value = 4, step size 4 and dtype as float.The following are the NumPy arrays created as required:
import numpy as np
# a) 1-D array of zeros
zeros = np.zeros(10)
print("zeros array:\n", zeros)
# b) 1-D array of vowels
vowels = np.array(['a', 'e', 'i', 'o', 'u'])
print("vowels array:\n", vowels)
# c) 2-D array of ones
ones = np.ones((2, 5), dtype=int)
print("ones array:\n", ones)
# d) 2-D array created from nested Python lists
myarray1 = np.array([[2.7, -2, -19], [0, 3.4, 99.9], [10.6, 0, 13]])
print("myarray1:\n", myarray1)
# e) 2-D array using arange() as well as reshape()
# Remember the arithmetic progression.
# The last value can be calculated as follows:
# l = a + (n - 1)d
# here a = start value = 4
# n = number of elements = 3 × 5 = 15
# d = common difference = step = 4
# So, last value = 4 + (15 - 1)4 = 60
# So, we should use a value more than 60 as the stop value
# it can be 60.5 or 60.0001 or 64 (but less than or equal to 64)
myarray2 = np.arange(4, 64, 4, dtype=float).reshape(3, 5)
print("myarray2:\n", myarray2)
The following will be the output of each array variable.
zeros array:
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
vowels array:
['a' 'e' 'i' 'o' 'u']
ones array:
[[1 1 1 1 1]
[1 1 1 1 1]]
myarray1:
[[ 2.7 -2. -19. ]
[ 0. 3.4 99.9]
[ 10.6 0. 13. ]]
myarray2:
[[ 4. 8. 12. 16. 20.]
[24. 28. 32. 36. 40.]
[44. 48. 52. 56. 60.]]
5. Using the arrays created in Question 4 above, write NumPy commands for the following:
a)
Find the dimensions, shape, size, data type of the items and itemsize of arrays
zeros
, vowels
, ones
, myarray1
and myarray2
.b)
Reshape the array
ones
to have all the 10 elements in a single row.c)
Display the 2nd and 3rd element of the array
vowels
.d)
Display all elements in the 2nd and 3rd row of the array
myarray1
.e)
Display the elements in the 1st and 2nd column of the array
myarray1
.f)
Display the elements in the 1st column of the 2nd and 3rd row of the array
myarray1
.g)
Reverse the array of
vowels
.a) NumPy commands to find the dimensions, shape, size, data type of the items and itemsize of arrays
zeros
, vowels
, ones
, myarray1
and myarray2
:print("Zeros array: ")
print("Dimensions: ", zeros.ndim)
print("Shape: ", zeros.shape)
print("Size: ", zeros.size)
print("Data type: ", zeros.dtype)
print("Itemsize: ", zeros.itemsize)
print("Vowels array: ")
print("Dimensions: ", vowels.ndim)
print("Shape: ", vowels.shape)
print("Size: ", vowels.size)
print("Data type: ", vowels.dtype)
print("Itemsize: ", vowels.itemsize)
print("Ones array: ")
print("Dimensions: ", ones.ndim)
print("Shape: ", ones.shape)
print("Size: ", ones.size)
print("Data type: ", ones.dtype)
print("Itemsize: ", ones.itemsize)
print("Myarray1 array: ")
print("Dimensions: ", myarray1.ndim)
print("Shape: ", myarray1.shape)
print("Size: ", myarray1.size)
print("Data type: ", myarray1.dtype)
print("Itemsize: ", myarray1.itemsize)
print("Myarray2 array: ")
print("Dimensions: ", myarray2.ndim)
print("Shape: ", myarray2.shape)
print("Size: ", myarray2.size)
print("Data type: ", myarray2.dtype)
print("Itemsize: ", myarray2.itemsize)
Output:
Zeros array:
Dimensions: 1
Shape: (10,)
Size: 10
Data type: float64
Itemsize: 8
Vowels array:
Dimensions: 1
Shape: (5,)
Size: 5
Data type: <U1
Itemsize: 4
Ones array:
Dimensions: 2
Shape: (2, 5)
Size: 10
Data type: int32
Itemsize: 4
Myarray1 array:
Dimensions: 2
Shape: (3, 3)
Size: 9
Data type: float64
Itemsize: 8
Myarray2 array:
Dimensions: 2
Shape: (3, 5)
Size: 15
Data type: float64
Itemsize: 8
b) NumPy command to reshape the array
ones
to have all the 10 elements in a single row:ones_reshape = ones.reshape(1, 10)
print(ones_reshape)
Output:
[[1 1 1 1 1 1 1 1 1 1]]
c) NumPy command to display the 2nd and 3rd element of the array
vowels
print(vowels[1:3])
Output:
['e' 'i']
d) NumPy command to display all elements in the 2nd and 3rd row of the array
myarray1
print(myarray1[1:3, :])
Output:
[[ 0. 3.4 99.9]
[10.6 0. 13. ]]
e) NumPy command to display the elements in the 1st and 2nd column of the array myarray1
print(myarray1[:, 0:2])
Output:
[[ 2.7 -2. ]
[ 0. 3.4]
[10.6 0. ]]
f) NumPy command to display the elements in the 1st column of the 2nd and 3rd row of the array
myarray1
print(myarray1[1:3, 0:1])
Output:
[[ 0. ]
[10.6]]
g) NumPy command to reverse the array of
vowels
vowels_reverse = vowels[::-1]
print(vowels_reverse)
Output:
['u' 'o' 'i' 'e' 'a']
6. Using the arrays created in Question 4 above, write
NumPy commands for the following:
NumPy commands for the following:
a)
Divide all elements of array
ones
by 3.b)
Add the arrays
myarray1
and myarray2
.c)
Subtract
myarray1
from myarray2
and store the result in a new array.d)
Multiply
myarray1
and myarray2
elementwise.e)
Do the matrix multiplication of
myarray1
and myarray2
and store the result in a new array myarray3
.f)
Divide
myarray1
by myarray2
.g)
Find the cube of all elements of
myarray1
and divide the resulting array by 2.h)
Find the square root of all elements of
myarray2
and divide the resulting array by 2. The result should be rounded to two places of decimals.a) To divide all elements of array
ones
by 3:ones_divided_by_3 = ones / 3
print(ones_divided_by_3)
Output:
[[0.33333333 0.33333333 0.33333333 0.33333333 0.33333333]
[0.33333333 0.33333333 0.33333333 0.33333333 0.33333333]]
b) To add the arrays
myarray1
and myarray2
:my_array_sum = myarray1 + myarray2
print(myarray_sum)
Output:
Exception has occurred: ValueError
operands could not be broadcast together with shapes (3,3) (3,5)
c) To subtract
myarray1
from myarray2
and store the result in a new array:my_array_diff = myarray2 - myarray1
print(myarray_diff)
Output:
Exception has occurred: ValueError
operands could not be broadcast together with shapes (3,5) (3,3)
d) To Multiply
myarray1
and myarray2
elementwise:my_array_prod = myarray1 * myarray2
print(myarray_prod)
Output:
Exception has occurred: ValueError
operands could not be broadcast together with shapes (3,3) (3,5)
e) To do the matrix multiplication of
myarray1
and myarray2
and store the result in a new array myarray3
:array3 = myarray1 @ myarray2
print(myarray3)
Output:
[[ -873.2 -946.4 -1019.6 -1092.8 -1166. ]
[ 4477.2 4890.4 5303.6 5716.8 6130. ]
[ 614.4 708.8 803.2 897.6 992. ]]
f) To divide
myarray1
by myarray2
:array_div = myarray1 / myarray2
print(myarray_div)
Output:
Exception has occurred: ValueError
operands could not be broadcast together with shapes (3,3) (3,5)
g) To find the cube of all elements of
myarray1
and divide the resulting array by 2:myarray1_cube = myarray1 ** 3
myarray1_cube_divided_by_3 = myarray1_cube / 2
print(myarray1_cube_divided_by_3)
Output:
[[ 9.841500e+00 -4.000000e+00 -3.429500e+03]
[ 0.000000e+00 1.965200e+01 4.985015e+05]
[ 5.955080e+02 0.000000e+00 1.098500e+03]]
h) To find the square root of all elements of
myarray2
and divide the resulting array by 2 and then rounding the result to two places of decimals:myarray2_sqrt = np.sqrt(myarray2)
myarray2_sqrt_divided_by_2 = myarray2_sqrt / 2
myarray2_sqrt_divided_by_2_rounded = myarray2_sqrt_divided_by_2.round(2)
print(myarray2_sqrt_divided_by_2_rounded)
Output:
[[1. 1.41 1.73 2. 2.24]
[2.45 2.65 2.83 3. 3.16]
[3.32 3.46 3.61 3.74 3.87]]
7. Using the arrays created in Question 4 above, write NumPy commands for the following:
a)
Find the transpose of
ones
and myarray2
.b)
Sort the array
vowels
in reverse.c)
Sort the array
myarray1
such that it brings the lowest value of the column in the first row and so on.a) To find the transpose of
ones
and myarray2
:ones_transpose = ones.transpose()
myarray2_transpose = myarray2.transpose()
print("Transpose of ones:\n", ones_transpose)
print("Transpose of myarray2:\n", myarray2_transpose)
Output:
Transpose of ones:
[[1 1]
[1 1]
[1 1]
[1 1]
[1 1]]
Transpose of myarray2:
[[ 4. 24. 44.]
[ 8. 28. 48.]
[12. 32. 52.]
[16. 36. 56.]
[20. 40. 60.]]
b) To sort the array
vowels
in reverse:# Do not use vowels.sort()
# vowels.sort() will modify the original array and returns None
# So, the logic used to reverse the sorting gives an error
# Ideally, there is no need to use sort()
# as the original array vowels is already sorted
# (while constructing itself)
vowels_reverse_sorted = np.sort(vowels)[::-1]
print("Reverse sorted vowels array:\n", vowels_reverse_sorted)
Output:
Reverse sorted vowels array:
['u' 'o' 'i' 'e' 'a']
c) To sort the array
myarray1
such that it brings the lowest value of the column in the first row and so on:
# in this case also, if we use
# myarray1.sort(), it'll modify the original array
# and as it returns 'None', the variable
# myarray1_sorted will have 'None'
# So, we're using np.sort()
myarray1_sorted = np.sort(myarray1, axis=0)
print("Sorted myarray1:\n", myarray1_sorted)
Output:
Sorted myarray1:
[[ 0. -2. -19. ]
[ 2.7 0. 13. ]
[ 10.6 3.4 99.9]]
8. Using the arrays created in Question 4 above, write NumPy commands for the following:
a)
Use
NumPy.split()
to split the array myarray2
into 5 arrays columnwise. Store your resulting arrays in myarray2A
, myarray2B
, myarray2C
, myarray2D
and myarray2E
. Print the arrays myarray2A
, myarray2B
, myarray2C
, myarray2D
and myarray2E
.b)
Split the array
zeros
at array index 2, 5, 7, 8 and store the resulting arrays in zerosA
, zerosB
, zerosC
and zerosD
and print them.c)
Concatenate the arrays
myarray2A
, myarray2B
and myarray2C
into an array having 3 rows and 3 columns.a) To use
NumPy.split()
to split the array myarray2
into 5 arrays columnwise and store the resulting arrays in myarray2A
, myarray2B
, myarray2C
, myarray2D
and myarray2E
and print the arrays myarray2A
, myarray2B
, myarray2C
, myarray2D
and myarray2E
:myarray2A, myarray2B, myarray2C, myarray2D, myarray2E = np.split(myarray2, 5, axis=1)
print("myarray2A:\n", myarray2A)
print("myarray2B:\n", myarray2B)
print("myarray2C:\n", myarray2C)
print("myarray2D:\n", myarray2D)
print("myarray2E:\n", myarray2E)
Output:
myarray2A:
[[ 4.]
[24.]
[44.]]
myarray2B:
[[ 8.]
[28.]
[48.]]
myarray2C:
[[12.]
[32.]
[52.]]
myarray2D:
[[16.]
[36.]
[56.]]
myarray2E:
[[20.]
[40.]
[60.]]
b) To split the array
zeros
at array index 2, 5, 7, 8 and store the resulting arrays in zerosA
, zerosB
, zerosC
and zerosD
and print them:zerosA, zerosB, zerosC, zerosD, zeroE = np.split(zeros, [2, 5, 7, 8])
print("zerosA:\n", zerosA)
print("zerosB:\n", zerosB)
print("zerosC:\n", zerosC)
print("zerosD:\n", zerosD)
Output:
zerosA:
[0. 0.]
zerosB:
[0. 0. 0.]
zerosC:
[0. 0.]
zerosD:
[0.]
Note: Note that the array
[2, 5, 7, 8]
will split the array zeros
into five parts as follows:1.
index 0 to 2 ( elsement at index 2 is not included)
2.
index 2 to 5 ( elsement at index 5 is not included)
3.
index 5 to 7 ( elsement at index 7 is not included)
4.
index 7 to 8 ( elsement at index 8 is not included)
5.
index 8 to end
So, it can not be assigned to just four variable i.e.,
zeroA
, zeroB
, zeroC
, zeroD
. So, we have considered an additional variable zeroE
and didn’t use it. We considered only the four required variables in the print
statement.c) To concatenate the arrays
myarray2A
, myarray2B
and myarray2C
into an array having 3 rows and 3 columns:reshaped = np.concatenate((myarray2A, myarray2B, myarray2C), axis=1).reshape(3, 3)
print("Concatenated and Reshaped Array:\n", reshaped)
Output:
Concatenated and Reshaped Array:
[[ 4. 8. 12.]
[24. 28. 32.]
[44. 48. 52.]]
Note: In the above, the concatenated array itself is of shape (3, 3). So, we don’t have to reshape it again to (3, 3). If you do, there won’t be any change.
9. Create a 2-D array called
myarray4
using arange()
having 14 rows and 3 columns with start value = -1, step size 0.25 having. Split this array row wise into 3equal parts and print the result.import numpy as np
# Remember the arithmetic progression.
# The last value can be calculated as follows:
# l = a + (n - 1)d
# here a = start value = -1
# n = number of elements = 14 × 3 = 42
# d = common difference = step = 0.25
# So, last value = -1 + (42 - 1)0.25 = 9.25
# So, we should use a value more than 9.25 as the stop value
# it can be 9.25 or 9.2501 or 9.5 (but less than or equal to 9.5)
myarray4 = np.arange(-1, 9.5, 0.25).reshape(14, 3)
myarray4_parts = np.split(myarray4, 3, axis=1)
print("Original array:\n", myarray4)
print("\nArray split into 3 equal parts:\n", myarray4_parts)
Output:
Original array:
[[-1. -0.75 -0.5 ]
[-0.25 0. 0.25]
[ 0.5 0.75 1. ]
[ 1.25 1.5 1.75]
[ 2. 2.25 2.5 ]
[ 2.75 3. 3.25]
[ 3.5 3.75 4. ]
[ 4.25 4.5 4.75]
[ 5. 5.25 5.5 ]
[ 5.75 6. 6.25]
[ 6.5 6.75 7. ]
[ 7.25 7.5 7.75]
[ 8. 8.25 8.5 ]
[ 8.75 9. 9.25]]
Array split into 3 equal parts:
[array([[-1. ],
[-0.25],
[ 0.5 ],
[ 1.25],
[ 2. ],
[ 2.75],
[ 3.5 ],
[ 4.25],
[ 5. ],
[ 5.75],
[ 6.5 ],
[ 7.25],
[ 8. ],
[ 8.75]]), array([[-0.75],
[ 0. ],
[ 0.75],
[ 1.5 ],
[ 2.25],
[ 3. ],
[ 3.75],
[ 4.5 ],
[ 5.25],
[ 6. ],
[ 6.75],
[ 7.5 ],
[ 8.25],
[ 9. ]]), array([[-0.5 ],
[ 0.25],
[ 1. ],
[ 1.75],
[ 2.5 ],
[ 3.25],
[ 4. ],
[ 4.75],
[ 5.5 ],
[ 6.25],
[ 7. ],
[ 7.75],
[ 8.5 ],
[ 9.25]])]
10. Using the myarray4 created in the above questions, write commands for the following:
a)
Find the sum of all elements.
b)
Find the sum of all elements row wise.
c)
Find the sum of all elements column wise.
d)
Find the max of all elements.
e)
Find the min of all elements in each row.
f)
Find the mean of all elements in each row.
g)
Find the standard deviation column wise.
The following are the commands for the given tasks for the myarray4 created in the above question.
a) To find the sum of all elements:
# a) sum of all elements
sum_all = myarray4.sum()
print("Sum of all elements:", sum_all)
Output:
Sum of all elements: 173.25
b) To find the sum of all elements row wise:
# b) sum of all elements row wise
sum_row = myarray4.sum(axis=1)
print("Sum of all elements row wise:", sum_row)
Output:
Sum of all elements row wise: [-2.25 0. 2.25 4.5 6.75 9. 11.25 13.5 15.75 18. 20.25 22.5
24.75 27. ]
c) To find the sum of all elements column wise:
# c) sum of all elements columnwise
sum_col = myarray4.sum(axis=0)
print("Sum of all elements column wise:", sum_col)
Output:
Sum of all elements column wise: [54.25 57.75 61.25]
d) To find the max of all elements:
# d) max of all elements
max_val = myarray4.max()
print("Max of all elements:", max_val)
Max of all elements: 9.25
e) To find the min of all elements in each row:
# e) min of all elements in each row
min_row = myarray4.min(axis=1)
print("Min of all elements in each row:", min_row)
Output:
Min of all elements in each row: [-1. -0.25 0.5 1.25 2. 2.75 3.5 4.25 5. 5.75 6.5 7.25
8. 8.75]
f) To find the mean of all elements in each row:
# f) mean of all elements in each row
mean_row = myarray4.mean(axis=1)
print("Mean of all elements in each row:", mean_row)
Output:
Mean of all elements in each row: [-0.75 0. 0.75 1.5 2.25 3. 3.75 4.5 5.25 6. 6.75 7.5
8.25 9. ]
g) To find the standard deviation column wise:
# g) standard deviation column wise
std_col = myarray4.std(axis=0)
print("Standard deviation column wise:", std_col)
Output:
Standard deviation column wise: [3.02334666 3.02334666 3.02334666]