w3resource

NumPy: View inputs as arrays with at least two dimensions, three dimensions

NumPy: Array Object Exercise-55 with Solution

Write a NumPy program to view inputs as arrays with at least two dimensions, three dimensions.

Sample Solution:

Python Code:

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

# Defining a scalar value
x = 10

# Printing view inputs as arrays with at least two dimensions for scalar x
print("View inputs as arrays with at least two dimensions:")
print(np.atleast_1d(x))

# Creating a NumPy array with shape (2, 2) using arange and reshape functions
x = np.arange(4.0).reshape(2, 2)

# Viewing x as an array with at least two dimensions
print(np.atleast_1d(x))

# Printing view inputs as arrays with at least three dimensions for scalar x
print("View inputs as arrays with at least three dimensions:")

# Redefining the scalar value
x = 15

# Viewing x as an array with at least three dimensions
print(np.atleast_3d(x))

# Creating a NumPy array using arange function
x = np.arange(3.0)

# Viewing x as an array with at least three dimensions
print(np.atleast_3d(x)) 

Sample Output:

View inputs as arrays with at least two dimensions:                    
[10]                                                                   
[[ 0.  1.]                                                             
 [ 2.  3.]]                                                            
View inputs as arrays with at least three dimensions:                  
[[[15]]]                                                               
[[[ 0.]                                                                
  [ 1.]                                                                
  [ 2.]]] 

Explanation:

In the above code –

  • ‘x = 10’ defines an integer variable x with the value 10.
  • print(np.atleast_1d(x)): The np.atleast_1d() function takes the input x and converts it into an array with at least one dimension. Since x is a scalar, it is converted into a 1D array with one element. The output is [10].
  • ‘x = np.arange(4.0).reshape(2, 2)’ creates a 2D array x with shape (2, 2) using the np.arange() and reshape() functions.
  • print(np.atleast_1d(x)): Since x is already a 2D array, the np.atleast_1d() function doesn't change its dimensions.
  • .
  • ‘x = 15’ This line defines an integer variable x with the value 15.
  • print(np.atleast_3d(x)): The np.atleast_3d() function takes the input x and converts it into an array with at least three dimensions. Since x is a scalar, it is converted into a 3D array with shape (1, 1, 1). The output is [[[15]]].
  • x = np.arange(3.0): This line creates a 1D array x with elements [0., 1., 2.].
  • print(np.atleast_3d(x)): The np.atleast_3d() function takes the input x and converts it into an array with at least three dimensions. Since x is a 1D array, it is converted into a 3D array with shape (1, 3, 1).

Python-Numpy Code Editor:

Previous: Write a NumPy program to convert specified inputs to arrays with at least one dimension.
Next: Write a NumPy program to insert a new axis within a 2-D 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/python-numpy-exercise-55.php