0% found this document useful (0 votes)
5 views6 pages

3252 5 Ids

The document provides an overview of stacking, concatenating, and broadcasting NumPy arrays, detailing functions like vstack(), hstack(), and dstack() for stacking, as well as np.concatenate() for concatenation. It includes code examples demonstrating the creation and manipulation of arrays, showcasing vertical and horizontal stacking, concatenation, and broadcasting operations. The results of the operations are printed, confirming the successful implementation of these NumPy functionalities.

Uploaded by

nbkr115
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

3252 5 Ids

The document provides an overview of stacking, concatenating, and broadcasting NumPy arrays, detailing functions like vstack(), hstack(), and dstack() for stacking, as well as np.concatenate() for concatenation. It includes code examples demonstrating the creation and manipulation of arrays, showcasing vertical and horizontal stacking, concatenation, and broadcasting operations. The results of the operations are printed, confirming the successful implementation of these NumPy functionalities.

Uploaded by

nbkr115
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Student ID:23kb1a3252

EXPERIMENT -V

Department of Computer Science and Engineering Page | 27


Student ID:23kb1a3252

Stacking and Concatenating a NumPy Array


a. Stacking nd array
b. Concatenating nd array
c . Broadcasting nd array

Description:

Stacking : NumPy implements the function of stacking. We can perform stacking along three
dimensions:

• vstack() – it performs vertical stacking along the rows.


• hstack()– it performs horizontal stacking along with the columns.
• dstack()– it performs in-depth stacking along a new third axis.
NumPy stack function takes two input parameters – the arrays for stacking and the axis for the resultant
array.

vstack() : We use the vstack()function to sequentially stack arrays in a vertical order i.e. along the rows.
This is a more specific concept of array stacking. It is very useful for arrays with up to 3 dimensions. The
vsplit() function splits the array into a list of multiple sub-arrays vertically.

hstack() : We use this function to sequentially stack arrays in horizontal order i.e. along with the
columns. This is a specific concept for column vice concatenation. We cannot perform horizontal stacking
if the number of rows is not equal in both the arrays.

dstack() : This function is for stacking the arrays along depth. The resultant array is along a third new
dimension.

Concatenating : : A group of things linked together or occurring together in a way that produces a
particular result or effect

Broadcasting : The term broadcasting describes how NumPy treats arrays with different shapes during
arithmetic operations. Subject to certain constraints, the smaller array is “broadcast” across the larger
array so that they have compatible shapes. Broadcasting provides a means of vectorizing array operations
so that looping occurs in C instead of Python.

a. Stacking Array

Create Stack Array

v_stack = np.vstack((self.arr1,self. arr2))


print("Vertical Stack:",v_stack)
h_stack = np.hstack(( self.arr1,self. arr2))
print("Horizontal Stack:",h_stack)
d_stack = np.dstack(( self.arr1,self. arr2))
print("Depth Stack:",d_stack)

Department of Computer Science and Engineering Page | 28


Student ID:23kb1a3252
b. Concatenating Array

Create Concatenate Array

self.tarr1=self.arr1.reshape(2,2)
print("2D arr1=",self.tarr1)
self.tarr2=self.arr2.reshape(2,2)
print("2D arr2=",self.tarr2)
v_concate= np.concatenate(( self.tarr1,self. tarr2), axis=0)
print("vertical concatenation:",v_concate)
h_concate = np.concatenate(( self.tarr1,self. tarr2), axis=1)
print("horizontal concatenation:",h_concate)

c. Broadcasting Array

Create Broadcast Array

print("2D arr2=",self.tarr1)
vector = np.array([10, 20])
print("\nVector:",vector)
result = self.tarr1+ vector
print("\nResult after broadcasting and addition:")
print(result)

Code:

import numpy as np
class Array:
def createArray(self):
L1=[]
L2=[]
n=int(input("Enter size :"))
for i in range(0,n):
Elements=int(input("Enter elements of arr1 :"))
L1.append (Elements)
self.arr1=np.array(L1)
for i in range(0,n):
Elements=int(input("Enter elements of arr2:"))
L2.append(Elements)

Department of Computer Science and Engineering Page | 29


Student ID:23kb1a3252
self.arr2 =np.array(L2)

def displayArray(self):
print(self.arr1)
print(self.arr2)

def stacking(self):
v_stack = np.vstack((self.arr1,self. arr2))
print("Vertical Stack:",v_stack)
h_stack = np.hstack(( self.arr1,self. arr2))
print("Horizontal Stack:",h_stack)
d_stack = np.dstack(( self.arr1,self. arr2))
print("Depth Stack:",d_stack)

def concate(self):
self.tarr1=self.arr1.reshape(2,2)
print("2D arr1=",self.tarr1)
self.tarr2=self.arr2.reshape(2,2)
print("2D arr2=",self.tarr2)

v_concate= np.concatenate(( self.tarr1,self. tarr2), axis=0)


print("vertical concatenation:",v_concate)
h_concate = np.concatenate(( self.tarr1,self. tarr2), axis=1)
print("horizontal concatenation:",h_concate)

def broadcasting(self):
print("2D arr2=",self.tarr1)
vector = np.array([10, 20])
print("\nVector:",vector)
result = self.tarr1+ vector
print("\nResult after broadcasting and addition:")
print(result)

Department of Computer Science and Engineering Page | 30


Student ID:23kb1a3252

a=Array()
a.createArray()
a.displayArray()
a.stacking()
a.concate()
a.broadcasting()

Output:

Enter size : 4
Enter elements of arr1 : 29
Enter elements of arr1 : 10
Enter elements of arr1 : 4
Enter elements of arr1 : 5

Enter elements of arr2:14


Enter elements of arr2:31
Enter elements of arr2: 3
Enter elements of arr2: 53
[29 10 4 5]
[14 31 3 53]

Vertical Stack: [[29 10 4 5]


[14 31 3 53]]

Horizontal Stack: [29 10 4 5 14 31 3 53]


Depth Stack: [[[29 14]
[10 31]
[ 4 3]
[ 5 53]]]

Department of Computer Science and Engineering Page | 31


Student ID:23kb1a3252

2D arr1= [[29 10]


[ 4 5]]
2D arr2= [[14 31]
[ 3 53]]
vertical concatenation: [[29 10]
[ 4 5]
[14 31]
[ 3 53]]
horizontal concatenation: [[29 10 14 31]
[ 4 5 3 53]]
2D arr2= [[29 10]
[ 4 5]]

Vector: [10 20]

Result after broadcasting and addition:


[[39 30]
[14 25]]

Result:
Verified Concatenating , Broadcasting and Stacking of numpy arrays.
Signature of Faculty: Grade:

Department of Computer Science and Engineering Page | 32

You might also like