Computer Science, asked by PHEONIX001, 8 months ago

Suppose you have an array 'p': [[1, 5], [3, 7], [4, 9]] What will be the output of the following code? np.reshape(p, -1)

Answers

Answered by Shanaya1220
3

to find out the answer outlook u have to multiply the code with the bar given

Answered by vishakasaxenasl
0

Answer:

np.reshape(p, -1)

Output:

[1,5,3,7,4,9]

Explanation:

NumPy array contains similar kinds of elements. NumPy library has shape and reshape methods.

Shape method

The shape method defines the shape of the array. By shape, it is meant the dimension of the array.

For example:

example = [[1,2,3]

                   [4,5,6] ]

print(np.shape)

Output = (3,3)

That means it has 3 rows and 3 columns

Reshape method

Reshape method is used to modify the shape of an existing np array. You can either reshape the array into 1 dimension or into a multi-dimension. But remember that reshaping can only be applied to a number of elements that are present. If your array has less number of elements then it will throw an error.

For example:

arr = [1, 5], [3, 7]]

print(arr.reshape(4,1))

Output -

[1

5

3

7 ]

But if you try to run like:

print(arr.reshape(3,2)

It will throw an error because your array does not contain 3x2 = 6 elements and 4 elements can be organized into 3 rows and 4 columns.

-1 is used to flatten the array and make it one-dimensional.

#SPJ2

Similar questions