Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Python NumPy stack Function Example with 2d array

# welcome to softhunt.net
# Python program explaining
# stack() function

import numpy as np

# input array
in_arr1 = np.array([[1, 3, 5], [-1, -3, -5]] )
print ("1st Input array : 
", in_arr1)

in_arr2 = np.array([[2, 4, 6], [-2, -4, -6]] )
print ("2nd Input array : 
", in_arr2)

# Stacking the two arrays along axis 0
out_arr1 = np.stack((in_arr1, in_arr2), axis = 0)
print ("Output stacked array along axis 0:
 ", out_arr1)

# Stacking the two arrays along axis 1
out_arr2 = np.stack((in_arr1, in_arr2), axis = 1)
print ("Output stacked array along axis 1:
 ", out_arr2)

# Stacking the two arrays along last axis
out_arr3 = np.stack((in_arr1, in_arr2), axis = -1)
print ("Output stacked array along last axis :
 ", out_arr3)
Source by softhunt.net #
 
PREVIOUS NEXT
Tagged: #Python #NumPy #stack #Function #Example #array
ADD COMMENT
Topic
Name
6+4 =