Stack summing vectors to numpy 3d array

Learn, how to stack vectors to numpy 3d array? By Pranit Sharma Last updated : October 08, 2023

NumPy is an abbreviated form of Numerical Python. It is used for different types of scientific operations in python. Numpy is a vast library in python which is used for almost every kind of scientific or mathematical operation. It is itself an array which is a collection of various methods and functions for processing the arrays.

Problem statement

Suppose we are given two NumPy arrays and we need to stack them together in such a way that it becomes a two-dimensional array.

Stack vectors to numpy 3d array

For this purpose, we will use the numpy.stack() method. This method is used for joining multiple NumPy arrays. Unlike, concatenate(), it joins arrays along a new axis. It returns a NumPy array.

Let us understand with the help of an example,

Python program for 'stack summing vectors to numpy 3d array'

# Import numpy
import numpy as np

# Import pandas
import pandas as pd

# Creating a numpy array
arr = np.array([1, 2, 3])

# Display original array
print("Original array:\n",arr,"\n")

# Creating another numpy array
arr2 = np.array([4, 5, 6])

# Stacking the two arrays
res = np.stack((arr, arr2),axis=0)

# Display result
print("Result:\n",res,"\n")

Output

The output of the above program is:

Example: Stack summing vectors to numpy 3d array

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.