NumPy: Create a random 10x4 array and extract the first five rows of the array and store them into a variable
NumPy: Random Exercise-5 with Solution
Write a NumPy program to create a random 10x4 array and extract the first five rows of the array and store them into a variable.
Sample Solution :
Python Code :
import numpy as np
x = np.random.rand(10, 4)
print("Original array: ")
print(x)
y= x[:5, :]
print("First 5 rows of the above array:")
print(y)
Sample Output:
Original array: [[ 0.38593391 0.52823544 0.8994567 0.22097238] [ 0.16639229 0.74964167 0.58102198 0.2811601 ] [ 0.56447627 0.42575759 0.71297527 0.91099347] [ 0.00261548 0.0064798 0.66096109 0.54514293] [ 0.7216008 0.95815426 0.53370551 0.28116107] [ 0.16252081 0.26191659 0.40883164 0.60653848] [ 0.55934457 0.37814126 0.63287808 0.01856616] [ 0.03788236 0.22705078 0.82024426 0.83019741] [ 0.31140166 0.43926341 0.38685152 0.92402934] [ 0.00581032 0.83925377 0.95246879 0.28570894]] First 5 rows of the above array: [[ 0.38593391 0.52823544 0.8994567 0.22097238] [ 0.16639229 0.74964167 0.58102198 0.2811601 ] [ 0.56447627 0.42575759 0.71297527 0.91099347] [ 0.00261548 0.0064798 0.66096109 0.54514293] [ 0.7216008 0.95815426 0.53370551 0.28116107]]
Pictorial Presentation:
Python-Numpy Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a NumPy program to create a 5x5 array with random values and find the minimum and maximum values.
Next: Write a NumPy program to shuffle numbers between 0 and 10 (inclusive).
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Find current directory and file's directory:
To get the full path to the directory a Python file is contained in, write this in that file:
import os dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)
To get the current working directory use
import os cwd = os.getcwd()
Documentation references for the modules, constants and functions used above:
- The os and os.path modules.
- The __file__ constant
- os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
- os.path.dirname(path) (returns "the directory name of pathname path")
- os.getcwd() (returns "a string representing the current working directory")
- os.chdir(path) ("change the current working directory to path")
Ref: https://bit.ly/3fy0R6m
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework