w3resource

NumPy: Create a 3x3x3 array filled with arbitrary values

NumPy: Basic Exercise-31 with Solution

Write a NumPy program to create a 3x3x3 array filled with arbitrary values.

This problem involves writing a NumPy program to generate a 3x3x3 array filled with arbitrary values. The task requires utilizing NumPy's array creation functionalities to efficiently construct the multi-dimensional array with arbitrary values. By specifying the desired shape of the array and providing arbitrary values, the program generates a 3x3x3 array suitable for various computational and analytical tasks.

Sample Solution :

Python Code :

# Importing the NumPy library with an alias 'np'
import numpy as np

# Creating a 3x3x3 array filled with random numbers between 0 and 1 using np.random.random()
x = np.random.random((3, 3, 3))

# Printing the randomly generated 3x3x3 array 'x'
print(x) 

Output:

[[[ 0.51919099  0.31268732  0.58506582]
  [ 0.12730206  0.30556451  0.55981097]
  [ 0.92910493  0.73947119  0.14252086]]

 [[ 0.96159407  0.21341612  0.6814465 ]
  [ 0.71884351  0.01271011  0.98812225]
  [ 0.47553515  0.04584955  0.59425412]]

 [[ 0.44064227  0.26612916  0.58861619]
  [ 0.37935141  0.0640071   0.53717733]
  [ 0.77986385  0.0771148   0.92183522]]]                         

Explanation:

The above code creates a 3D array of random numbers between 0 and 1 with the shape (3, 3, 3) and prints the resulting array.

Here ‘np.random.random((3, 3, 3))’ creates a 3D array with the shape (3, 3, 3), filled with random numbers between 0 and 1. The input to np.random.random() is a tuple representing the desired shape of the output array.

Finally print(x) prints the generated 3D array to the console.

Visual Presentation:

NumPy: Create a 3x3x3 array filled with arbitrary values.

Python-Numpy Code Editor:

Previous: NumPy program to create an 4x4 matrix in which 0 and 1 are staggered, with zeros on the main diagonal.
Next: NumPy program to compute sum of all elements, sum of each column and sum of each row of an given array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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://198.211.115.131/python-exercises/numpy/basic/numpy-basic-exercise-31.php