Q:

Write a NumPy program to create an 1-D array of 20 elements. Now create a new array of shape (5, 4) from the said array, then restores the reshaped array into a 1-D array

0

Write a NumPy program to create an 1-D array of 20 elements. Now create a new array of shape (5, 4) from the said array, then restores the reshaped array into a 1-D array.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np
array_nums = np.arange(0, 40, 2)
print("Original array:")
print(array_nums)
print("\nNew array of shape(5, 4):")
new_array = array_nums.reshape(5, 4)
print(new_array) 
print("\nRestore the reshaped array into a 1-D array:")
print(new_array.flatten())

Sample Output:

Original array:
[ 0  2  4  6  8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38]

New array of shape(5, 4):
[[ 0  2  4  6]
 [ 8 10 12 14]
 [16 18 20 22]
 [24 26 28 30]
 [32 34 36 38]]

Restore the reshaped array into a 1-D array:
[ 0  2  4  6  8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now