Basics of Numpy - Arithmetical operation and arrays handling code samples - 3

This post is in continuation to where we left in

Basics of Numpy - Arithmetical operation and arrays handling code samples

Basics of Numpy - Arithmetical operation and arrays handling code samples - 2

Lets explore more on array operation and cover dimensions, split, concatenate,addition/removal,several binary operations that we perform on ndarray object. 


Joining Arrays 

#concatenate- Joins a sequence of arrays along an existing axis

a = np.array([[1,2],[3,4]]) 

b = np.array([[5,6],[7,8]]) 

# both the arrays are of same dimensions 

#Joining the two arrays along axis 0:' 

print np.concatenate((a,b)) 

#Joining the two arrays along axis 1:' 

print np.concatenate((a,b),axis = 1)

#stack - Joins a sequence of arrays along a new axis

# Stack the two arrays along axis 0: 

print np.stack((a,b),0) 

# Stack the two arrays along axis 1:

print np.stack((a,b),1)

#hstack - Stacks arrays in sequence horizontally (column wise)

# Horizontal stacking: 

c = np.hstack((a,b)) 

print c 

vstack - Stacks arrays in sequence vertically (row wise)

# Vertical stacking:

c = np.vstack((a,b))

Split arrays

#Split -  Splits an array into multiple sub-arrays
a = np.arange(9) 
# Split the array in 3 equal-sized subarrays:
b = np.split(a,3) 
# Split the array at positions indicated in 1-D array:' 
b = np.split(a,[4,7])
print b
 
#hsplit - Splits an array into multiple sub-arrays horizontally (column-wise)
a = np.arange(16).reshape(4,4) 
# Horizontal splitting: 
b = np.hsplit(a,2) 
print b 

#vsplit - Splits an array into multiple sub-arrays vertically (row-wise)
a = np.arange(16).reshape(4,4) 
print 'Vertical splitting:' 
b = np.vsplit(a,2) 
print b

Changing dimension

Broadcast- Produces an object that mimics broadcasting
a = np.arange(8)
b = a.reshape(4,2)
print b

Broadcast_to -Broadcasts an array to a new shape
a = np.arange(4).reshape(1,4) 
# After applying the broadcast_to function:
print np.broadcast_to(a,(4,4))

expand_dims - Expands the shape of an array
x = np.array(([1,2],[3,4])) 
y = np.expand_dims(x, axis = 0) 
# The shape of X and Y array:
print x.shape, y.shape 
# insert axis at position 1 
y = np.expand_dims(x, axis = 1) 
#Array Y after inserting axis at position 1:
# x.ndim and y.ndim:
print x.ndim,y.ndim 
# x.shape and y.shape:
print x.shape, y.shape

squeeze - Removes single-dimensional entries from the shape of an array
x = np.arange(9).reshape(1,3,3) 
y = np.squeeze(x) 
print y 
# The shapes of X and Y array:
print x.shape, y.shape

Adding /Removing elements in arrays

resize - Returns a new array with the specified shape
a = np.array([[1,2,3],[4,5,6]]) 
print a 
# The shape of first array:
print a.shape
b = np.resize(a, (3,2)) 
print b 
# The shape of second array:
print b.shape 

# first row of a is repeated in b since size is bigger 
# Resize the second array:
b = np.resize(a,(3,3)) 
print b

append - Appends the values to the end of an array
a = np.array([[1,2,3],[4,5,6]]) 
print a 
#Append elements to array:
print np.append(a, [7,8,9]) 
# Append elements along axis 0: 
print np.append(a, [[7,8,9]],axis = 0) 
# Append elements along axis 1:
print np.append(a, [[5,5,5],[7,8,9]],axis = 1)

insert - Inserts the values along the given axis before the given indices
a = np.array([[1,2],[3,4],[5,6]]) 
print a 
# Axis parameter not passed. The input array is flattened before insertion.
print np.insert(a,3,[11,12]) 
# Axis parameter passed. The values array is broadcast to match input array.'
print 'Broadcast along axis 0:' 
print np.insert(a,1,[11],axis = 0) 
print 'Broadcast along axis 1:' 
print np.insert(a,1,11,axis = 1)

unique - Finds the unique elements of an array
a = np.array([15,12,16,12,17,15,16,18,12,19]) 
u = np.unique(a) 
print u 
indices = np.unique(a, return_index = True) 
print indices 
print 'Indices of unique array:' 
u,indices = np.unique(a,return_inverse = True) 
print u  
print indices 
print 'Reconstruct the original array using indices:' 
print u[indices] 
print 'Return the count of repetitions of unique elements:' 
u,indices = np.unique(a,return_counts = True) 
print u 
print indices

delete - Returns a new array with sub-arrays along an axis deleted
a = np.arange(12).reshape(3,4) 
# Array flattened before delete operation as axis not used:
print np.delete(a,5) 
# Column 2 deleted:
print np.delete(a,1,axis = 1) 
# Slice containing alternate values from array deleted:
a = np.array([1,2,3,4,5,6,7,8,9,10]) 
print np.delete(a, np.s_[::2])

Following are the functions for bitwise operations available in NumPy package.

Bitwise_AND - Computes bitwise AND operation of array elements
# Binary equivalents of 13 and 17:
a,b = 13,17 
print bin(a), bin(b) 
# Bitwise AND of 13 and 17:
print np.bitwise_and(13, 17)

Bitwise_OR - Computes bitwise OR operation of array elements
a,b = 13,17 
# Binary equivalents of 13 and 17: 
print bin(a), bin(b)  
# Bitwise OR of 13 and 17:
print np.bitwise_or(13, 17)

Invert - Computes bitwise NOT
# Invert of 13 where dtype of ndarray is uint8:
print np.invert(np.array([13], dtype = np.uint8)) 
# Comparing binary representation of 13 and 242, we find the inversion of bits 
#Binary representation of 13:
print np.binary_repr(13, width = 8) 
# Binary representation of 242:' 
print np.binary_repr(242, width = 8)

Left_shift - Shifts bits of a binary representation to the left
# Left shift of 10 by two positions:
print np.left_shift(10,2) 
# Binary representation of 10:
print np.binary_repr(10, width = 8) 
#Binary representation of 40:
print np.binary_repr(40, width = 8)  
# Two bits in '00001010' are shifted to left and two 0s appended from right.

right_shift - Shifts bits of binary representation to the right
#Right shift 40 by two positions:
print np.right_shift(40,2) 
#Binary representation of 40:
print np.binary_repr(40, width = 8) 
# Binary representation of 10
print np.binary_repr(10, width = 8)  
# Two bits in '00001010' are shifted to right and two 0s appended from left.

Post a Comment

أحدث أقدم