EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

NumPy stack

Home » Software Development » Software Development Tutorials » NumPy Tutorial » NumPy stack

NumPy stack

Introduction to NumPy stack

The following article provides an outline for NumPy stack. Python provides different functions to the users. To work with arrays, the python library provides a NumPy function. The stack() characteristic is used to be a part of a sequence of equal dimension arrays alongside a new axis. The axis parameter of array specifies the sequence of the new array axis in the dimensions of the output. For example, suppose axis=0 that means it determines the first dimension and if axis=-1 that means it determines the last dimension. NumPy is an acronym for numerical python. Basically, NumPy is an open source project. NumPy performs logical and mathematical operations of arrays. Therefore, processing and manipulating can be done efficiently.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

numpy.stack(Arr, Axis)

Explanation:

  • Arr: In the above syntax the first parameter is Arr means array, it is used to define a sequence of equal dimension arrays with the same shape of array.
  • Axis: It is the final output array along with the input array.

How stack Function work in NumPy?

  • We must install Python on our system.
  • We must install numpy using the pip command.
  • We required basic knowledge about Python.
  • We required basic knowledge about stack function with parameters.
  • We required basic knowledge about arrays.
  • We can perform different operations using a numpy stack function.

Examples of NumPy stack

Given below are the examples of NumPy stack:

Example #1

For if Axis=0 and Axis=1.

Code:

import numpy as snp
A_x = snp.array([ 2, 3, 4] )
print ("First Input Array: \n", A_x)
A_y = snp.array([ 5, 6, 7] )
print ("Second Input array  :       \n", A_y)
A = snp.stack((A_x, A_y), axis = 0)
print ("\n ", A)
B = snp.stack((A_x, A_y), axis = 1)
print ("\n ", B)

Explanation:

  • We import NumPy functions and use them as snp.
  • We declared variable two input arrays such as A_x and A_y with array values.
  • We tried to print the value of the input array with their values respectively.
  • Then we used a stack function with both input arrays along with axis values 0 and 1 respectively.
  • Finally we have printed both resultant output arrays with axis value.
  • In the above example we implemented a stack function with axis values 0 and 1.

Output:

NumPy stack 1

Example #2

For if Axis= 0, Axis=1 and Axis=-1.

Code:

import numpy as snp
A_x = snp.array([[ 2, 3, 4], [ -2, -3, -4]] )   #input array
print ("First Input Array  : \n",A_x)
A_y = snp.array([[ 5, 6, 7], [ -5, -6, -7]] ) #input array
print ("Second Input array   : \n", A_y)
A  = snp.stack((A_x,A_y), axis = 0)       # Array with axis=0
print ("   0:  \n ", A)
B = snp.stack((A_x, A_y), axis = 1)  # Array with axis=1
print ("   1:   \n ", B)
C = snp.stack((A_x, A_y), axis = -1)  # Array with axis=-1
print (" -1: \n ", C)

Explanation:

  • We import NumPy functions and use them as snp.
  • We declared variable two input arrays such as A_x and A_y with array values.
  • We tried to print the value of the input array with their values respectively.
  • Then we used a stack function with both input arrays along with axis value 0, 1 and -1 respectively.
  • Finally we tried to print both resultant output arrays with axis value.
  • In the above example we implemented a stack function with axis values 0, 1 and -1.

Output:

if Axis= 0, Axis=1 and Axis=-1

Example #3

For if Axis = 1 and Axis = 2.

Code:

import numpy as snp
A_x = snp.array([ 1, 2, 3] )   #input array
print ("First Input Array : \n",A_x)
A_y = snp.array([ 4, 5, 6] )     #input array
print ("Second Input array  : \n",A_y)
A = snp.stack((A_x, A_y), axis = 1)   #  Array  with axis=1
print ("     \n ", A)
B = snp.stack((A_x, A_y), axis = 2) # Array  with axis=2
print ("     \n ", B)

Explanation:

  • We import NumPy functions and use them as snp.
  • We declared variable two input arrays such as A_x and A_y with array values.
  • We try to print the value of the input array with their values respectively.
  • Then we used a stack function with both input arrays along with axis values 1 and 2 respectively.
  • Finally we try to print both resultant output arrays with axis value.
  • In the above example we implemented a stack function with axis values 1 and 2. See in this example it only executes an array with axis 1 and when we assign axis 2 at that time it shows an error message as axis 2 is out of bounds for array dimension 2.

Output:

For if Axis = 1 and Axis = 2

NumPy stack 4

Example #4

For if Axis= 1 , Axis=2 and Axis=-1 with shape function.

Code:

import numpy as np
arrays = [np.random.randn(4, 5) for _ in range(10)]    #input array
A_x = snp.stack(arrays, axis=0).shape     #Array with axis=0
print("\n", A_x)
A_y = snp.stack(arrays, axis=1).shape  #Array with axis=1
print("\n", A_y)
z = np.stack(arrays, axis=2).shape #Array with axis=2
print("\n", z)

Popular Course in this category
Pandas and NumPy Tutorial (4 Courses, 5 Projects)4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (4,870 ratings)
Course Price

View Course

Related Courses
Python Training Program (36 Courses, 13+ Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Explanation:

  • We import numpy functions and use them as snp.
  • We declared a random array.
  • We try to print the value of the input with their array values respectively.
  • Then we used a stack function with both input arrays along with axis values 1 and 2 respectively.
  • Finally we try to print both resultant output arrays with axis value.
  • In the above example we implemented numpy stack with shape function.

Output:

NumPy stack 5

Conclusion

From the above article we saw basic syntax of NumPy stack functions. We also saw how we can implement them in python with different examples. From this article we also saw how we can handle NumPy stack functions in python.

Recommended Articles

This is a guide to NumPy stack. Here we discuss the introduction, how stack function work in NumPy? along with examples respectively. You may also have a look at the following articles to learn more –

  1. NumPy Array Functions
  2. numpy.ravel()
  3. numpy.dot()
  4. NumPy Ndarray

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
NumPy Tutorial
  • Basics
    • Introduction to NumPy
    • Install NumPy
    • NumPy Data Types
    • NumPy Functions
    • NumPy Histogram
    • numPy.where()
    • numpy.sort
    • NumPy floor()
    • Matrix in NumPy
    • Matrix Multiplication in NumPy
    • NumPy Arrays
    • NumPy Array Functions
    • NumPy Ndarray
    • NumPy Array Append
    • NumPy empty array
    • NumPy ndarray tolist
    • numpy.linspace()
    • NumPy.argmax()
    • NumPy Linear Algebra
    • numpy.diff()
    • numpy.unique( )
    • NumPy zeros
    • numpy.mean()
    • numpy.dot()
    • Numpy.argsort()
    • numpy.pad()
    • numpy.ravel()
    • NumPy stack
    • NumPy vstack
    • NumPy hstack
    • NumPy sum
    • NumPy cumsum
    • NumPy max
    • NumPy squeeze
    • NumPy median
    • NumPy Round
    • NumPy NaN
    • NumPy divide
    • NumPy square
    • NumPy ones
    • NumPy Meshgrid
    • NumPy exponential
    • NumPy percentile
    • NumPy fft
    • NumPy flatten
    • NumPy Concatenate
    • NumPy Outer
    • NumPy zip
    • NumPy Newaxis
    • NumPy Format
    • Numpy Random Seed ()
    • NumPy random choice
    • NumPy random normal
    • NumPy unravel_index
    • Numpy.eye()
    • NumPy append
    • NumPy searchsorted
    • NumPy shape
    • NumPy reshape
    • NumPy savetxt
    • NumPy genfromtxt
    • NumPy standard deviation
    • NumPy covariance
    • NumPy repeat
    • NumPy?Tile
    • NumPy Broadcasting

Related Courses

Pandas And NumPy Training Course

Python Training Course

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Pandas And NumPy Training Course Learn More