Extracting Rows from a Random Array

Write a NumPy program to create a random 10×4 array and extract the first five rows of the array and store them into a variable. Return the variable.

Example 1:

No specific inputs. 
Outputs will be random due to random array generation.

Example 2:

No specific inputs. 
Outputs will be random due to random array generation.

You can use np.random.rand() function to generate a random array and use array slicing to extract the first five rows.

import numpy as np

def extract_rows():
    array = np.random.rand(10, 4)
    return array[:5]

print(extract_rows())
print(extract_rows())

 

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!