w3resource

NumPy: Create a 5x5x5 cube of 1's


Create 5x5x5 Cube of 1’s

Write a NumPy program to create a 5x5x5 cube of 1's.

Pictorial Presentation:

Python NumPy: Create a 5x5x5 cube of 1's

Sample Solution:

Python Code:

# Importing the NumPy library and aliasing it as 'np'
import numpy as np

# Creating a 3-dimensional array filled with zeros of size 5x5x5 and converting it to integers
# Adding 1 to each element of the array using the 'astype' method
x = np.zeros((5, 5, 5)).astype(int) + 1

# Printing the resulting array 'x' filled with the value 1
print(x)

Sample Output:

[[[1 1 1 1 1]                                                          
  [1 1 1 1 1]                                                          
  [1 1 1 1 1]                                                          
  [1 1 1 1 1]                                                          
  [1 1 1 1 1]]                                                         
                                                                       
 [[1 1 1 1 1]                                                          
  [1 1 1 1 1]                                                          
  [1 1 1 1 1]                                                          
  [1 1 1 1 1]                                                          
  [1 1 1 1 1]]                                                         
                                                                       
 [[1 1 1 1 1]                                                          
  [1 1 1 1 1]                                                          
  [1 1 1 1 1]                                                          
  [1 1 1 1 1]                                                          
  [1 1 1 1 1]]
                                                                       
 [[1 1 1 1 1]                                                          
  [1 1 1 1 1]                                                          
  [1 1 1 1 1]                                                          
  [1 1 1 1 1]                                                          
  [1 1 1 1 1]]                                                         
                                                                       
 [[1 1 1 1 1]                                                          
  [1 1 1 1 1]                                                          
  [1 1 1 1 1]                                                          
  [1 1 1 1 1]                                                          
  [1 1 1 1 1]]]   

Explanation:

The above code creates a 5x5x5 NumPy array filled with ones (integer data type).

np.zeros((5, 5, 5)): This function call creates a 3D NumPy array with dimensions 5x5x5, filled with zeros. By default, the elements are of float data type.

.astype(int): This method converts the data type of the elements in the array from float to integer.

+ 1: This operation adds 1 to each element in the integer array. Since the array was initially filled with zeros, adding 1 to each element results in an array filled with ones.

Finally print(x) function prints the 5x5x5 integer array filled with ones.


For more Practice: Solve these Related Problems:

  • Write a NumPy program to generate a 5x5x5 array filled entirely with ones using np.ones.
  • Create a function that builds an n-dimensional cube of ones and verifies its shape and content.
  • Generate a 3D cube of ones and then replace a central sub-cube with zeros to test advanced indexing.
  • Compare the output of np.ones with a manual repetition of a scalar value to form a 5x5x5 array.

Go to:


PREV : Display Array Elements in Fortran Order
NEXT : Multiply Array (3,4) Elements by 3


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.



Follow us on Facebook and Twitter for latest update.