3252 5 Ids
3252 5 Ids
EXPERIMENT -V
Description:
Stacking : NumPy implements the function of stacking. We can perform stacking along three
dimensions:
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
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
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)
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)
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)
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
Result:
Verified Concatenating , Broadcasting and Stacking of numpy arrays.
Signature of Faculty: Grade: