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.

Python-Numpy Code Editor: