NCERT solutions Introduction to NumPy
1. What is NumPy ? How to install it?
Numpy stands for Numberical Python . It is a package for data analysis and scientific computing with Python. |
2. What is an array and how is it different from a list? What is the name of the built-in array class in NumPy? |
Array is a data type used to store multiple values using a single identifier (variable name ) . An array constains an ordered collection of data elements where each element is of the same type and can be referenced by its index ( position ) |
3. What do you understand by rank of an ndarray? |
The number of dimensions or axes of the array is called rank of the array. |
4. Create the following NumPy arrays: |
zeros = np.zeros((1,10)) |
b) A 1-D array called vowels having the elements ‘a’,‘e’, ‘i’, ‘o’ and ‘u’ |
vowels = np.array([‘a’,’e’,’i’,’o’,’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. |
ones = np.ones((2,5),dtype=int) |
d) Use nested Python lists to create a 2-D array calledmyarray1 having 3 rows and 3 columns and store the following data: |
myarray1 = np.array([[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. |
myarray2 = np.array(np.arange(4,61,4),dtype=float).reshape(3,5) |
NCERT solutions Introduction to NumPy5. Using the arrays created in Question 4 above, write NumPy commands for the following: |
zeros.shape |
b) Reshape the array ones to have all the 10 elements in a single row |
ones.reshape(1,10) |
c) Display the 2nd and 3rd element of the array vowels. |
print(vowal[1],vowel[2]) |
d) Display all elements in the 2nd and 3rd row of the array myarray1. |
print(myarray1[1:3,]) |
e) Display the elements in the 1st and 2nd column of the array myarray1. |
print(myarray1[0:3,0:2]) |
f) Display the elements in the 1st column of the 2nd and 3rd row of the array myarray1 |
print((myarray1[1:3,0:3]).reshape(1,6)) |
g) Reverse the array of vowels. |
print(vowal[::-1] |
6. Using the arrays created in Question 4 above, write NumPy commands for the following: |
ones/1 |
b) Add the arrays myarray1 and myarray2. |
myarray1 + myarray2 |
c) Subtract myarray1 from myarray2 and store the result in a new array. |
newarray = myarray2 – myarray1 |
d) Multiply myarray1 and myarray2 elementwise. |
myarray1 * myarray2 |
e) Do the matrix multiplication of myarray1 and myarray2 and store the result in a new array myarray3. |
myarray3 = myarray1 @ myarray2 |
f) Divide myarray1 by myarray2. |
myarray1 / myarray2 |
g) Find the cube of all elements of myarray1 and divide the resulting array by 2. |
myarray1 **3/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. |
np.around(myarray**(1/2)/2,2) |
7. Using the arrays created in Question 4 above, write NumPy commands for the following: |
ones.transpose() |
b) Sort the array vowels in reverse. |
(np.sort(vowels))[::-1) |
c) Sort the array myarray1 such that it brings the lowest value of the column in the first row and so on. |
myarray1.sort(axis=0) |
8. Using the arrays created in Question 4 above, write NumPy commands for the following: |
myarray2A, myarray2B, myarray2C, myarray2D, myarray2E = np.array_split(myarray2,5,axis=1) |
c) Concatenate the arrays myarray2A, myarray2B and myarray2C into an array having 3 rows and 3 columns. |
(np.array([myarray2A, myarray2B, myarray2C])).reshape(3,3) |
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 3 equal parts and print the result. |
myarray4 = np.arrange(-1,9.5,0.25) |
NCERT solutions Introduction to NumPy10. Using the myarray4 created in the above questions, write commands for the following: |
myarray4.sum() |
b) Find the sum of all elements row wise. |
myarray4.sum(axis=1) |
c) Find the sum of all elements column wise. |
myarray4.sum(aixis=0) |
d) Find the max of all elements. |
myarray4.max() |
e) Find the min of all elements in each row. |
myarray4.min() |
f) Find the mean of all elements in each row. |
myarray4.mean(axis=1) |
g) Find the standard deviation column wise. |
myarray4..std(axis=0) |
| |
No comments:
Post a Comment
Have query? Just say it.