0% found this document useful (0 votes)
13 views6 pages

Numpy Quiz Questions and Answers

The document contains a quiz with 21 questions focused on numpy operations and functionalities. Each question presents multiple-choice answers related to array manipulation, reshaping, and mathematical operations using numpy. The quiz tests the understanding of numpy's capabilities and syntax.

Uploaded by

Anu Deeban
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Numpy Quiz Questions and Answers

The document contains a quiz with 21 questions focused on numpy operations and functionalities. Each question presents multiple-choice answers related to array manipulation, reshaping, and mathematical operations using numpy. The quiz tests the understanding of numpy's capabilities and syntax.

Uploaded by

Anu Deeban
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Numpy Quiz Progress : 12/21

1) For a given numpy array, how will you change the dimensions to 5 using the existing
parameters in the numpy array. my_array = [Link]([[[[1,3,4]]]])

 [Link](my_array, ndim=5)

 [Link](my_array, ndim=1)

 [Link](my_array, ndmin=5)

 [Link](my_array, ndim= my_array.ndmin + 1)

2) For the given numpy arrays, Array1 and Array2, what will be the dot product for the same.
Array1 = array([[1, 2, 3], [4, 5, 6]]) Array2 = array([[2, 3],[3, 2]])

 Valueerror

 array([[ -9, 6, -1],[-12, 18, -7]])

 Keyerror

 None of the Above

3) In a given numpy array, using the slicing operations print the reverse of the array. array =
[Link]([10,3,1,203,404,204,20,302,30,402,192])

 array[-1:0]

 array1[len(array1):0]

 array[::-1]

 array1[-1:-len(array1)]

4) What is the output of the below code: a = [[1, 0], [0, 1]] b = [[4, 1], [2, 2]] [Link](a, b)

 array([[4, 1],[2, 2]])

 None of the above

 array([[4, 4],[2, 4]])

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


5) What is the output of the following code: a = [Link](3*4*5*6).reshape((3,4,5,6)) b =
[Link](3*4*5*6)[::-1].reshape((5,4,6,3)) [Link](a, b)[2,3,2,1,2,2]

 499128

 399118

 Value Error

 909128

6) What will be output for the following code? import numpy as np a = [Link]([3, 7, 32],
dtype = complex) print(a)

 Error

 [ 3.+0.j 7.+0.j 32.+0.j]

 [ 3.+0.k 7.+0.j 32.+0.l]

 [ 3.0+0.j 7.0+0.j 32.0+0.j]

7) How to replace all values greater than a given value with a given cuto ? For example: In
array ‘a’, replace all values greater than 30 to 30 and less than 10 to 10. Input:
[Link](100) a = [Link](1,50, 20)

 [Link](a, a_min=10, a_max=30)

 None of the above

 [Link](a < 10, 10, [Link](a > 30, 30, a))

 Both of the above ---correct answer

8) For the given arrays, array1 and array2, if we stack the two arrays row wise, what will be
the output? array1 = [Link]([[1,2],[3,4]]) array2 = [Link]([[1,2],[3,4],[5,6],[7,8],[9,10]])

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

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

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

 None of the above


9) For a given numpy array, how are you going to insert a new value at the specified
position? array = [Link]([10,3,1,203,404,204,20,302,30,402,192]) Elem_to_be_inserted =
[1,2,3,4] The position to be inserted at = before 404

 [Link](array2, [Link](404), array2)

 [Link](array1, 4, array2)

 [Link](array1, [Link](404), array2)

 [Link](array2, 404, array2)

10) Create a 3x3 matrix using numpy, and all the values of the matrix must be a constant k.

 [Link]((3,3), ‘k’)

 [Link](3,3)

 [Link]((3,3), ‘k’)

 [Link](‘k’)

11) For the given python code that implements bubble sort, what will be the output for the
given numpy array. def bub_sort(array): for i in range(0, len(array)): for j in range(0, len(array)
- i - 1): if array[j] < array[j + 1]: temp = array[j] array[j] = array[j + 1] array[j+1] = temp return
array my_array = [Link]([20,14,25,16,45,60,12,9])

 array([60, 45, 20, 25, 16, 14, 12, 9])

 array([ 9, 12, 14, 16, 20, 25, 45, 60])

 array([ 9, 12, 16, 14, 20, 25, 45, 60])

 array([60, 45, 25, 20, 16, 14, 12, 9])

12) For a given numpy array of the shape (2,5) ,How will you reshape the array in the shape
(5,2).

 [Link](5,2)

 [Link]((2,5) == (5,2))

 [Link](5,2)

 [Link](5,2)
13) What will be the shape of the sample numpy array after flattening it? Sample =
[Link]([[1,2],[3,4],[5,6],[7,8]])

 array([1], [2], [3], [4], [5], [6], [7], [8])

 None of the above

 array([1, 2, 3, 4, 5, 6, 7, 8])

 array([1, 2], [3, 4], [5, 6], [7, 8])

14) In the given array, how can we get the following output - array([2, 5, 8]). Sample =
[Link]([[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]])

 array[0:3][1]

 None of the above

 array[[0:3], [1]]

 array[0:3, 1]

15) Given two numpy arrays, we will perform Horizontally stack the given arrays array1 and
array2. What will be the output of the above operation? The sample arrays are as follows.
Array1 = [Link](20,2) Array2 = [Link]([1,2,3,4,5,6,7,8,9,10])

 array([[ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])

 None of the above

 array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

 array([[ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])

16) For the given arrays, array1 and array2, if we stack the two arrays column wise, what
will be the output? array1 = [Link]([[1,2],[3,4]]) array2 = [Link]([[5,6],[7,8]])

 array([[1, 2], [5, 6],[3, 4], [7, 8]])

 array([[1, 2, 5, 6 ,3, 4, 7, 8]])

 None of the above

 array([[1, 2, 5, 6],[3, 4, 7, 8]])


17) Given two vectors A and B, find the cross product between the two vectors. A =
[Link]([[4],[12],[29]]) B = [Link]([[13],[21],[4]])

 array([[-555 361 -72]])

 array([[-561 360 -72]])

 array([[-561 361 -72]])

 array([[-560 360 -71]])

18) Given two vectors A and B, find the correlation coe icient of the following vectors. A =
[Link]([1,3,5,7,9,11,13,15,17,19,21,23,25]) B =
[Link]([0,2,4,6,8,10,12,14,16,18,20, 22, 24])

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

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

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

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

19) Create a nested numpy array from a given dictionary data. sample = {1: [1,2], 2: [[1],[2]],
3: [[1,2], [3,4], [4,5]], 4: [1], 5: [1,2,3,4,5]}

 array([[1, list([1, 2])], [2, list([[1], [2]])], [3, list([[1, 2], [3, 4], [4, 5]])], [4, list([1])], [5, list([1,
2, 3, 10, 5])]], dtype=object)

 array([[1, list([1, 2])], [2, list([[1], [2]])], [3, list([[1, 2], [3, 4], [4, 5]])], [4, list([1])], [5, list([1,
2, 3, 4, 5])]], dtype=object)

 array([[1, list([1, 1])], [2, list([[1], [2]])], [3, list([[1, 2], [3, 4], [4, 5]])], [4, list([1])], [5, list([1,
2, 3, 4, 5])]], dtype=object)

 array([[1, list([1, 2])], [2, list([[1], [2]])], [3, list([[1, 2], [1, 4], [4, 5]])], [4, list([1])], [5, list([1,
2, 3, 4, 5])]], dtype=object)

 ARRAY([[1, LIST([1, 2])], [2, LIST([[1], [2]])], [3, LIST([[1, 2], [3, 4], [4, 5]])], [4,
LIST([1])], [5, LIST([1, 2, 3, 4, 5])]], DTYPE=OBJECT)
20) A 2-dimensional array with 3 rows and 3 columns containing random numbers from 1 to
9 is given as - arr1= [Link]([[3,2,1],[6,4,5],[8,7,9]]) Find the di erence between the
maximum element across the columns and the minimum element across the rows.

 [7 3 1]

 [7 1 2]

 [8 3 2]

 [7 3 2]—correct answer

21) For a given numpy array, how will you change the dimensions to 5 using the existing
parameters in the numpy array. my_array = [Link]([[[[1,3,4]]]])

 [Link](my_array, ndim=5)

 [Link](my_array, ndim=1)

 [Link](my_array, ndim= my_array.ndmin + 1)

 [Link](my_array, ndmin=5)

Common questions

Powered by AI

You can replace all values in a NumPy array exceeding certain thresholds using np.clip() or np.where() functions. Specifically, np.clip(a, a_min=10, a_max=30) and np.where(a < 10, 10, np.where(a > 30, 30, a)) can both achieve this task by setting values below 10 to 10 and values above 30 to 30 .

Reshaping a NumPy array from shape (2,5) to (5,2) requires using numpy.reshape. The operation is performed with numpy.reshape(array, (5,2)), where 'array' is your initial array. This rearranges the data into the new specified shape .

Flattening a 2D NumPy array transforms it into a 1D array containing the same elements in row-major order. For instance, flattening a sample array like numpy.array([[1,2],[3,4],[5,6],[7,8]]) results in a 1D array array([1, 2, 3, 4, 5, 6, 7, 8]).

The output of the dot product using np.dot(a, b) with a = [[1, 0], [0, 1]] and b = [[4, 1], [2, 2]] is array([[4, 1],[2, 2]]) because multiplying the identity matrix a with b results in b itself .

To horizontally stack two NumPy arrays, such as Array1 = numpy.arange(20)[:10] reshaped to (10,) and Array2 = numpy.array([1,2,3,4,5,6,7,8,9,10]), use numpy.hstack((Array1, Array2)). This concatenates the arrays along columns, and the output for these specific arrays would be a single 1D array with shape (20,).

To insert elements into a NumPy array at a specified position correctly, use the numpy.insert(array, index, values) method. For example, if inserting [1,2,3,4] before the element 404 in the array np.array([10,3,1,203,404,204,20,302,30,402,192]), numpy.insert(array, 4, [1,2,3,4]) correctly places the new values before the fifth element, which is 404 .

To create a 3x3 constant matrix in NumPy, you can use the numpy.full function. For a constant value k, the command would be numpy.full((3,3), k), which fills a 3x3 matrix with the constant value k .

You can change the dimensions of an existing NumPy array to 5 dimensions by using the 'ndmin' parameter. Specifically, you can use numpy.array(my_array, ndmin=5) to ensure the array has five dimensions .

To reverse a NumPy array, you can use slicing with a step of -1. The operation array[::-1] will correctly reverse the array .

Attempting to compute the dot product of two NumPy arrays with incompatible shapes, such as Array1 with shape (2, 3) and Array2 with shape (2, 2), will result in a ValueError. The number of columns in the first matrix must equal the number of rows in the second matrix .

You might also like