NumPy: Create a 3x3x3 array with random values
3. 3x3x3 Random Array
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:
For more Practice: Solve these Related Problems:
- Create a 3x3x3 array of random values and compute the mean along the first axis.
 - Reshape the 3x3x3 random array into a 9x3 array and verify the new dimensions.
 - Compute the standard deviation of the elements in each 3x3 sub-array along the third axis.
 - Apply a threshold to the 3x3x3 array to set all values below 0.5 to zero and count the nonzero elements.
 
Go to:
PREV : Random Integers in Range.
NEXT :  5x5 Array Min/Max.
Python-Numpy Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
