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.
إرسال تعليق