w3resource

NumPy: Create random set of rows from 2D array

NumPy: Array Object Exercise-114 with Solution

Write a NumPy program to create random set of rows from 2D array.

Sample Solution:

Python Code:

import numpy as np
new_array = np.random.randint(5, size=(5,3))
print("Random set of rows from 2D array array:")
print(new_array)

Sample Output:

Random set of rows from 2D array array:
[[4 0 2]
 [4 2 4]
 [1 0 4]
 [4 4 3]
 [3 4 3]]

Pictorial Presentation:

Python NumPy: Create random set of rows from 2D array

Python Code Editor:

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

Previous: Write a NumPy program to build an array of all combinations of three numpy arrays.
Next: Write a NumPy program to find indices of elements equal to zero in a numpy array.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

List comprehension:

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