6. Arrays Operations#

6.1. Manipulating the data in array#

import numpy as np
a=np.array([[1,2,3],[4,5,6]])
a[1][1]=500
a
array([[  1,   2,   3],
       [  4, 500,   6]])

6.2. Filter the array so that data>20#

a=np.random.randint(1,50,(5,4))
a
array([[29, 24, 20, 10],
       [48, 10, 44, 49],
       [45, 31, 25, 24],
       [42, 25, 41, 43],
       [ 7, 39, 39, 40]])
a[a>20]
array([29, 24, 48, 44, 49, 45, 31, 25, 24, 42, 25, 41, 43, 39, 39, 40])

6.3. Broadcasting#

  • Brodcasting lets us perform vectorization operations

    • Scalers,arrays of differnet lengths can be added/operated to entire dataset

    • Adding something to entire dataset

    • array(1d) + matrix(nd)

      • Supports Vectorization

        • Better than you own custom for loop which won’t use vectorization

      • If you add array and matrix, by default array is row so its added to every row of matrix

      • If you want to add array to matrix column wise, take transpose of the array first and then add to matrix

        • You can’t take transpose of 1d array so instead of np.arr([1,2,3]) , add two square brackets first ie, np.arr([[1,2,3]])

6.4. Examples of Brodcasting#

  • arr1+arr2 # Index wise Addition

  • arr1-arr2 # Index wise Subtraction

  • arr1/arr2 # Index wise Division

  • arr1*arr2 # Index wise Multiplication

  • arr1@arr2 # Matrix Multiplication

a=np.random.randint(1,4,(3,4))
a
array([[3, 2, 2, 1],
       [3, 2, 1, 3],
       [3, 2, 1, 2]])
np.sqrt(a)
array([[1.73205081, 1.41421356, 1.41421356, 1.        ],
       [1.73205081, 1.41421356, 1.        , 1.73205081],
       [1.73205081, 1.41421356, 1.        , 1.41421356]])
np.exp(a)
array([[20.08553692,  7.3890561 ,  7.3890561 ,  2.71828183],
       [20.08553692,  7.3890561 ,  2.71828183, 20.08553692],
       [20.08553692,  7.3890561 ,  2.71828183,  7.3890561 ]])
np.log10(a)
array([[0.47712125, 0.30103   , 0.30103   , 0.        ],
       [0.47712125, 0.30103   , 0.        , 0.47712125],
       [0.47712125, 0.30103   , 0.        , 0.30103   ]])

6.5. Flattening an array#

  • Converts n dimensional data into 1 dimensional

a=np.random.randint(1,4,(3,4))
a
array([[3, 1, 1, 1],
       [3, 2, 1, 3],
       [2, 1, 3, 2]])
b=a.flatten()
b
array([3, 1, 1, 1, 3, 2, 1, 3, 2, 1, 3, 2])

6.6. Expanding an array#

np.expand_dims(b,axis=1) # change dimension from 1 to 2 and axis(cols)
array([[3],
       [1],
       [1],
       [1],
       [3],
       [2],
       [1],
       [3],
       [2],
       [1],
       [3],
       [2]])
np.expand_dims(b,axis=0)
array([[3, 1, 1, 1, 3, 2, 1, 3, 2, 1, 3, 2]])

6.7. Squeezing an array#

  • combine data points from array of array ( if only one element is present in list)

c=np.array([[1],[5]])
c
array([[1],
       [5]])
np.squeeze(c)
array([1, 5])

6.8. Repeating an array#

  • Repeat everything 2 times

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

6.9. Rolling an array#

  • rotating and array by n positions

np.roll(d,2) # 2 is step
array([3, 4, 1, 2])

6.10. Creating diagonal array#

  • places 1d data into diagonals and creates matrix

np.diag(d)
array([[1, 0, 0, 0],
       [0, 2, 0, 0],
       [0, 0, 3, 0],
       [0, 0, 0, 4]])

6.11. Negate the array#

-d
array([-1, -2, -3, -4])

6.12. Or operation#

d=np.array([1,2,3,4])
e=np.array([1,2,3,4])
d|e
array([1, 2, 3, 4])

6.13. Comparison#

d>e
array([False, False, False, False])

6.14. String Operations on Array#

a=np.array(['aA','bD','cD'])
# np.char.__ all string functions
## np has char class so .class
np.char.upper(a)
np.char.title(a)
array(['Aa', 'Bd', 'Cd'], dtype='<U2')

6.15. Trignometry Operations on Array#

e=np.array([1,2,3,4])
e
array([1, 2, 3, 4])
np.sin(e)
array([ 0.84147098,  0.90929743,  0.14112001, -0.7568025 ])
np.cos(e)
array([ 0.54030231, -0.41614684, -0.9899925 , -0.65364362])

6.16. Stats operations on arrays#

np.mean(e)
np.median(e)
np.std(e)
np.var(e)
# You can't find mode in numpy
1.25
np.min(e)
np.max(e)
4

6.17. Sorting Searching on arrays#

a=np.array([4,3,6,7,1])
a
array([4, 3, 6, 7, 1])
np.sort(a) # ascending by default
array([1, 3, 4, 6, 7])
np.searchsorted(a,34)
# Tells where the number 34 will be placed in sorted order
5

6.18. Counting on arrays#

a=np.array([4,0,0,3,6,7,1])
a
array([4, 0, 0, 3, 6, 7, 1])
np.count_nonzero(a)
5

6.19. Finding Index in array#

np.where(a>2) # Tells at which index we have data > 2
(array([0, 3, 4, 5]),)

6.20. Extract data from array#

np.extract(a>3,a)
array([4, 6, 7])