w3resource

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


Inputs as Arrays with 2D/3D Views

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).

For more Practice: Solve these Related Problems:

  • Write a NumPy program to convert a 1D input into a 2D array view using np.atleast_2d.
  • Transform a list of lists into a 3D array view with np.atleast_3d and verify its dimensions.
  • Create a function that converts various inputs to arrays with at least two or three dimensions based on a parameter.
  • Apply np.atleast_2d and np.atleast_3d on different inputs and compare the shapes to ensure correctness.

Go to:


PREV : Inputs as Arrays with 1+ Dimensions
NEXT : Insert New Axis in 2D Array


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.