Numpy-Long Answer Questions
Numpy-Long Answer Questions
1. 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 3 equal
parts and print the result. And 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.
2. 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’ and ‘u’.
c. A 2–D array called ones having 2 rows and 5 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.
3. Using the arrays created in Question 4 above, write NumPy commands for the
following:
a. Find the dimensions, shape, size, datatype of the items and item size of
arrays zeros, vowels, ones, myarray1and 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.
4. Using the arrays created in Question 4 above, write 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 element–wise.
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.
5. 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.
6. 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.
7. Write commands to perform following operations on two 4x4 ndarrays namely P and
Q:
a. Adding 10 to P
b. Multiplication of the two array P and Q
c. Divide all elements of Q by 7
d. Calculate log of all elements of P
e. Round all the elements of Q to nearest integer
f. Calculate remainder of all elements of P when divided by 7
g. Calculate square root of all elements of Q
8. 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’ and ‘u’.
c. A 2-D array called ones having 2 rows and 5 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.
9. Using the arrays created in Question 8 above, write NumPy commands for the following:
a. Find the dimensions, shape, size, and 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.
10. Using the arrays created in Question 8 above, write 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.