NumPy: Create a 3x3x3 array with random values
NumPy: Random Exercise-3 with Solution
Write a NumPy program to create a 3x3x3 array with random values.
Sample Solution:
Python Code:
# Importing the NumPy library as np
import numpy as np
# Generating a 3x3x3 NumPy array 'x' filled with random floats between 0 and 1 using np.random.random()
x = np.random.random((3, 3, 3))
# Printing the array 'x'
print(x)
Sample Output:
[[[ 0.08372197 0.09089865 0.54581268] [ 0.62831932 0.06252404 0.1108799 ] [ 0.25040264 0.80817908 0.37027715]] [[ 0.44916756 0.66390614 0.83100662] [ 0.87831954 0.17075539 0.7506945 ] [ 0.56165801 0.72280907 0.22771692]] [[ 0.04956428 0.7143399 0.37042941] [ 0.78875282 0.27425369 0.88486919] [ 0.13395418 0.86087257 0.36234729]]]
Explanation:
x = np.random.random((3,3,3)): This line generates a 3-dimensional array (3x3x3) of random floating-point numbers using the np.random.random() function. The input tuple (3,3,3) specifies the shape of the array, which has 3 arrays, each containing 3 arrays with 3 elements each.
print(x): Finally print() function prints the generated 3-dimensional array of random floating-point numbers.
Pictorial Presentation:
Python-Numpy Code Editor:
Previous: Write a NumPy program to generate six random integers between 10 and 30.
Next: Write a NumPy program to create a 5x5 array with random values and find the minimum and maximum values.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/numpy/python-numpy-random-exercise-3.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics