w3resource

NumPy: Create two arrays of size bigger and smaller than a given array

NumPy: Array Object Exercise-123 with Solution

Write a NumPy program to create two arrays of size bigger and smaller than a given array.

Sample Solution:

Python Code:

import numpy as np    
x = np.arange(16).reshape(4,4)
print("Original arrays:")
print(x)
print("\nArray with size 2x2 from the said array:")
new_array1 = np.resize(x,(2,2))
print(new_array1)
print("\nArray with size 6x6 from the said array:")
new_array2 = np.resize(x,(6,6))
print(new_array2)

Sample Output:

Original arrays:
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

Array with size 2x2 from the said array:
[[0 1]
 [2 3]]

Array with size 6x6 from the said array:
[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15  0  1]
 [ 2  3  4  5  6  7]
 [ 8  9 10 11 12 13]
 [14 15  0  1  2  3]]

Pictorial Presentation:

Python NumPy: Create two arrays of size bigger and smaller than a given array

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to join a sequence of arrays along a new axis.
Next: Write a NumPy program to broadcast on different shapes of arrays where p(3,3) + q(3).

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Set comprehension:

>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}