Python NumPy Stack with Examples

In this Python tutorial, we will learn how to stack Numpy array by using Python. Also, we will cover these topics on Python NumPy Stack.

  • Python Numpy stack 2d array
  • Python NumPy save images
  • Python NumPy stack axis
  • Python NumPy stack rows
  • Python NumPy stack matrix
  • Python NumPy stack columns
  • Python NumPy horizontal stack
  • Python stack list into numpy array
  • Python numpy vstack hstack
  • Python numpy column_stack

Python NumPy stack

  • In this section, we will discuss how to use stack() function in NumPy Python.
  • To perform this particular task we are going to use the stack() method. In Python, the stack() method is used to combine a sequence of numpy arrays along with a given axis.

Syntax:

Here is the Syntax of numpy.stack() method

numpy.stack
           (
            arrays,
            axis=0,
            out=None
           )
  • It consists of few parameters
    • arrays: This is an input array and the sequence of array must be same shape.
    • axis: This parameter signifies the index of the axis in the dimension of the result.

Example:

Let’s take an example and check how to use the stack() function in Python

Source Code:

import numpy as np 

new_arr = np.array([[45,67],[98,23]]) 
new_arr2 = np.array([[890,123],[45,95]]) 
result= np.stack((new_arr,new_arr2),0)
print(result)

Here is the Screenshot of the following given code

Python NumPy stack
Python NumPy stack

Read Python NumPy round

Python Numpy stack 2d array

  • In this Program, we will discuss how to combine 2d arrays by using stack() method in Python.
  • To perform this particular task we are going to use the np.stack() method. In Python, this function is used to combine a sequence of NumPy arrays along with a specified axis.

Let’s take an example and understand how to use the stack() function in Python

Source Code:

import numpy as np 
9
res1 = np.array([34, 16, 98])
res2 = np.array([23, 98, 156])
b= np.stack((res1, res2))
print(b)

In the above code first, we will import a numpy library and then create a numpy array by using the np.array() method.

Now declare a variable ‘b’ and assign np.stack() function to it. Once you will print ‘b’ then the output will display the joining of given arrays with the same shape.

Here is the output of the following given code

Python Numpy stack 2d array
Python Numpy stack 2d array

Read Python Numpy unique

How to join 2d arrays in NumPy Python

In Python, the append() function is used to add an element to the end of the array or we can say to combine two existing arrays and it will return a new array.

Syntax:

Here is the Syntax of np.append() function

numpy.append
            (
             arr,
             values,
             axis=None
            )

Example:

import numpy as np 

new_val1 = np.array([45, 89, 135])
new_val2 = np.array([189, 234, 578])
b= np.append(new_val1, new_val2)
print("Combinning arrays:",b)

Here is the implementation of the following given code

Python Numpy stack 2d array
Python Numpy stack 2d array

As you can see in the Screenshot the output displays the new array

Read Python NumPy repeat

Combine 2d array in Python NumPy

By using the np.tile() method we can do this task. In Python, the np.tile() method is used to repeat the number of items available in an array.

For example, we have an array that contains 3 elements [7,8,9] then use the np.tile() method and pass the input array which you want to operate along with the number that represents the number of times to repeat the array.

Syntax:

Here is the Syntax of np.tile() method

numpy.tile
          (
           A,
           reps
          )

Example:

import numpy as np 

new_val1 = np.array([7, 8, 9])
result=np.tile(new_val1,(2,1))
print(result)

Here is the output of the following given code

Python Numpy stack 2d array
Python Numpy stack 2d array

Read Python NumPy Data types

Python NumPy save images

  • Here we can see how to save images in NumPy Python.
  • To do this task we are going to use the matplotlib library and this module provides a function imsave() and this method is used to save the numpy array as an image file.

Syntax:

Here is the Syntax of matplotlib.pyplot.imsave()

matplotlib.pyplot.imsave
                        (
                         fname,
                         arr,
                         **kwargs
                        )

Here is the Screenshot of the following given code

Python NumPy stack images
Python NumPy stack images

Image Screenshot

image size
image size

Read Python NumPy 2d array + Examples

Python NumPy stack axis

  • In this section we will discuss how to use the axis in stack NumPy stack() function by using Python.
  • In this example, the axis parameter signifies the index of the new axis. In the np.stack() function if axis=0 then it will represent the first dimension and if we want to check the last dimension then we are going to set the axis parameter =-1.
  • By default, it is an optional parameter and indicates the axis in the resultant array.

Source Code:

import numpy as np 

arr1 = np.array([[23,45],[189,456]]) 
arr2 = np.array([[96,57],[145,856]]) 
new_output= np.stack((arr1,arr2),0) 
new_result= np.stack((arr1,arr2),1)
print("Axis with 0:",new_output)
print("Axis with 1:",new_result)

In the above code first, we have imported a numpy library and then create an array by using the np.array() method.

Now set the axis=0,1 in np.stack() method for joining the array. Once you will print ‘new_output’ and ‘new_result’ then the output will display the new 2-dimension array.

You can refer to the below Screenshot

Python NumPy stack axis
Python NumPy stack axis

Read Python NumPy 3d array + Examples

Python NumPy stack rows

  • In this Program, we will discuss how to stack rows in NumPy Python.
  • In Python, the vstack() function is used to join the sequence of arrays (row-wise). In this method, it takes the array as an input parameter in the form of a tuple along with a vertical axis.

Syntax:

Here is the Syntax of vstack() method

numpy.vstack
            (
             tup
            )

Let’s take an example and check the implementation of the NumPy vstack() method

Source Code:

import numpy as np

array1 = np.array([78, 34, 97])
array2 = np.array([11, 23, 67])
output = np.vstack((array1,array2))
print ("Row-wise combined array:", output) 

In the above code, we stacked two arrays rows-wise. To do this task first we imported the numpy library and then initialize two numpy arrays in variable ‘array1’ and ‘array2’. After that, we have applied the np.vstack() method and it will make a new numpy array.

Here is the Screenshot of the following given code

Python NumPy stack rows
Python NumPy stack rows

Read Python NumPy Split + 11 Examples

Combine row-wise elements in NumPy Python

By using the np.row_stack() method we can perform this particular task and this function is used to stack the array row-wise by using Python.

Syntax:

Here is the Syntax of numpy.row_stack() method

numpy.rowstack
              (
               tup
              )

Example:

import numpy as np

new_arr1 = np.array([167, 945, 109])
new_arr2 = np.array([456, 897, 123])
output = np.row_stack((new_arr1,new_arr2))
print ("Row-wise combined array:", output) 

You can refer to the below Screenshot

Python NumPy stack rows
Python NumPy stack rows

Read Python NumPy Normalize + Examples

Python NumPy stack matrix

  • Let us see how to combine a matrix element in NumPy Python.
  • In this example we will create an array by using the np.array() method and then apply the np.stack() method in it. Once you will print ‘z’ then the output will display a new numpy matrix.

Example:

import numpy as np 

val1 = np.array([[89,578],[356,785]]) 
val2 = np.array([[324,178],[210,867]]) 
z= np.stack((val1,val2),0)
print(z)

Here is the execution of the following given code

Python NumPy stack matrix
Python NumPy stack matrix

Read Python NumPy Random [30 Examples]

Python NumPy stack columns

  • In this section, we will discuss how to stack array column-wise by using NumPy Python.
  • To perform this particular task we are going to use the np.hstack() method and this function is used to join the sequence of arrays column-wise or we can say horizontally.
  • In this method inside of the parenthesis, we can easily provide the arrays that we want to combine together. For example, suppose you have two arrays ‘new_arr’ and ‘new_arr2’. Now you want to combine horizontally then you can easily use the np.hstack() function.

Syntax:

Here is the Syntax of hstack() function

numpy.hstack
            (
             tup
            )
  • It consists of only one parameters
    • tup: This parameter indicates the collection of numpy arrays and it is provide as an input.

Example:

Let’s take an example and understand the working of the hstack() function

Source Code:

import numpy as np

new_arr1 = np.array([34, 98, 345])
new_arr2 = np.array([87, 69, 123])
new_arr3 = np.array([42,75,378])
output = np.hstack((new_arr1,new_arr2,new_arr3))
print(output)

In the above code, we stacked three input arrays column-wise. To do this task first we import the numpy library and then initialize three numpy arrays ‘new_arr1’, ‘new_arr2’ and ‘new_arr3’. After that, we are going to use the np.hstack() function and get the result with the same shape.

Here is the Screenshot of the following given code

Python NumPy stack columns
Python NumPy stack columns

Read Python NumPy max with examples

Stack array horizontally by using NumPy Python

By using numpy.column_stack() function we can solve this problem. In Python, this function is used to stack the arrays horizontally.

Example:

import numpy as np

array1 = np.array([34, 98, 345])
array2 = np.array([87, 69, 123])

new_result = np.column_stack((array1,array2))
print(new_result)

Here is the output of the following given code

Python NumPy stack columns
Python NumPy stack columns

Read Check if NumPy Array is Empty in Python

Python NumPy horizontal stack

  • In this Program, we will discuss how to use the np.hstack() function in Python.
  • In Python, the hstack() function is used to join the sequences of numpy arrays column-wise or horizontally and it will return a single dimension array that contains all the elements from given input arrays.

Syntax:

Here is the Syntax of np.hstack() method

numpy.hstack
            (
             tup
            )

Example:

Let’s take an example and understand the working of horizontally stack numpy arrays

Source Code:

import numpy as np

val1 = np.array([845, 876, 190])
val2 = np.array([999, 147, 853])

new_output = np.column_stack((val1,val2))
print(new_output)

In the above code first, we imported a numpy array and then initialize an array by using the np.array() function.

Now declare a variable ‘new_output’ and assign np.column_stack() function. Once you will print ‘new_output’ then the result will display a new array.

You can refer to the below Screenshot

Python NumPy horizontal stack
Python NumPy horizontal stack

Read Python NumPy log + Examples

Python stack list into numpy array

  • Let us see how to convert the list into a numpy array by using Python.
  • To perform this particular task we are going to apply the np.asarray() function. In Python, this function is used to convert the data into a numpy array and the data can be in the form of lists.
  • This function is available in the NumPy module package and always returns a ndarray.

Syntax:

Here is the Syntax of numpy.asarray() method

numpy.asarray
             (
              a,
              dtype=None,
              order=None,
              *,
              like=None
              )
  • It consists of a few parameters
    • a: This parameter indicates the input data that we want to convert into a numpy array.
    • dtype: By default it takes none value and it indicates the data type of each element of the array.
    • order: This is also a optional parameter and indicate the memory representation that is by-default ‘C’.

Example:

Let’s take an example and understand the working of np.asarray() function.

import numpy as np

new_list = [24, 89, 156, 892, 665, 916, 754]
new_list2 = [167, 345, 987, 145, 945, 108, 345]
result = np.asarray(new_list)
output = np.asarray(new_list2)
print("Converted list with array:",result)
print(new_list2)
stac= np.stack((result,output))
print("stacked array:",stac)

In the above code, we imported a numpy library and then initialize a list []. Now we converted the list with a numpy array by using the np.asarray() method.

Once you will print the ‘result’ and ‘output’ then the output will display the numpy array. After that, we used the np.stack() method and pass ‘result’ and ‘output’ as an argument.

Here is the Screenshot of the following given code

Python stack list into numpy array
Python stack list into numpy array

As you can see in the Screenshot the output displays the new numpy array

Read Python NumPy where with examples

Python numpy vstack hstack

  • In this section, we will discuss the combination of vstack and hstack functions by using NumPy Python.
  • These functions are available in the numpy module package and it will help the user to combine numpy arrays horizontally and vertically both along with the axis.

Example:

import numpy as np

array1 = np.array([784, 992, 456])
array2 = np.array([134, 678, 345])

stac_col = np.hstack((array1,array2))
print("Horzontally stacked array:",stac_col)

array3 = np.array([156, 934, 789])
array4 = np.array([388, 456, 908])
stac_row = np.vstack((array3,array4))
print("Vertically stacked array:", stac_row) 

In the above code we have imported a numpy library and then initialize an array by using np.array() method .

Now use the ‘np.hstack’ and ‘np.vstack’ method for combining the elements vertically and horizontally in NumPy array.

You can refer to the below Screenshot

Python numpy vstack hstack
Python numpy vstack hstack

Read Python reverse NumPy array

Python numpy column_stack

  • In this Program, we will discuss how to use np.column_stack() function in NumPy Python.
  • In Python, this function is used to merge the column elements or horizontally in the NumPy array. It always takes a sequence of the 1-dimensional array and joins them as columns to create a single 2-dimensional array.

Syntax:

Here is the Syntax of np.column_stack() function

numpy.column_stack
                  (
                   tup
                  )

Example:

Let’s take an example and understand the working of numpy.column_stack() function

Source Code:

import numpy as np

val1 = np.array([845, 876, 190])
val2 = np.array([999, 147, 853])

result = np.column_stack((val1,val2))
print(result)

Here is the execution of the following given code

Python numpy column stack
Python numpy column stack

You may also like the following Python NumPy tutorials:

In this Python tutorial, we will learn how to stack Numpy array by using Python. Also, we will cover these topics.

  • Python Numpy stack 2d array
  • Python NumPy save images
  • Python NumPy stack axis
  • Python NumPy stack rows
  • Python NumPy stack matrix
  • Python NumPy stack columns
  • Python NumPy horizontal stack
  • Python stack list into numpy array
  • Python numpy vstack hstack
  • Python numpy column_stack