Join my course at Udemy (Python Programming Bible-From beginner to advanced )

Blogger templates

Monday 26 September 2022

1. Data Analysis - NumPy Operations

 Numpy Operations  

Numpy Array Creation - 
  • X = np.array[[10,20,30,40]]
  • X = np.zeros(10)
  • X= np.ones(10)
  • X = np.empty (10)
  • X=np.arange([1,11])  ---- will create 10 element array starting from element=1 to 10
Array Creation using data type
  • X = np.array([10,20,30,40], dtype = np.floar64)
Changing data type
  • X = X.astype(np.int32)
Arithmetic operation
  • X = np.array([[10,20,30,40],[50,60.70,80])
Indexing and slicing 
  • One dimension 
    • X[1] ----- Will return X[1]
    • X[2:4] ------ will return X[2[, X[3]

  • Multiple dimension
    • X[0] ------- Will return 0th row
    • X[0][1] -- Will return 0th row and 1st column.
Propagation and Broadcast
  • X[2:4] = 100   ---------- X[2], X[3] will be assigned 100.
Row slicing & Column slicing using booleans
  • Y = np.array(["True", "True", "False", "False", "True"]),     
  • X[Y=="True"]      -- row slicing
  • X[:, Y="True"]     -- column slicing
Manipulating all elements
  • X = X[X<0] = 0  --- Any element less than 0 will be assigned a value = 0
Fancy Indexing
  • Get rows
    • X[1]
    • X[[1,3,5]]

  • Get Columns
    • X[:, 1:4]
    • X[:, [1,4,5]] 

  • Get Multiple rows and columns
    • X[[1,3,4], [2,4,5]]
Fancy Indexing using pipe
  • X[[1,4,5]][:, [0,1]]
Reshape
  • X.reshape(8,4)
  • X.transpose()

Universal Function

  • One input
    • np.sqrt(X)
    • np.exp(X)
  • Multiple Input
    • np.maximum(X,Y)
    • np.add(X,Y)
  • Finding NAN value 
    • np.nan(X)  -- Ouput will be [False, False,..., True] -- in this form. -- TRUE will indicate corresponding value in NAN.
  • Any element has NAN value
    • np.nan(X).any()

Vectorization

  • Mesh Grid
    • Xaxis, Yaxis = np.meshgrid(X,Y)
  • np.where
    • K = np.where(Z, X, Y)
    • K = np.where(X>0, Y ,Z )
      • np.where(X>99, X, 100) -> combination of scaler and numpy array

Statistical Functions

  • np.mean(X) --- Mean of all element of the array.
  • np.std(X)
  • np.sum()
  • X.mean(axis=0) -- Mean of each column
  • X.mean(axis=1) -- Mean of each row.

Boolean arrays

  • X.any() - IF any value is TRUE
  • X.sum() - Count of all TRUE value
  • X.all() - If all value is TRUE
  • X>0 -> returns a boolean array.

Sorting

  • np.sort(X, axis = None) -- sort all element of array and return one dimensional array
  • np.sort(X, axis = 0) - Column wise sorting
  • np.sort(X, axis = 1 ) - Row wise sorting

Remove duplicates

  • np.unique(X) --  removes the duplicates

Check common values between two array

  • np.in1d(X,Y) -.. returns bollean array with TRUE indicating common value
  • np.union1d(X,Y) - returns union of X and Y

Saving to a file

Saving one array in one file

  • np.save("OneDimension", X)
  • np.load("OneDimension.npy")

Save multiple array using key/value

  • np.savez("Onedimension", key1=X, key2=Y)
  • k = np.load("Onedimension")
  • k[key1], k[key2]

Other functionality

  • np.dot(X,Y)
  • k= inv(y) ---- from numpy.linalg import inv




Share:

0 comments:

Post a Comment

Feature Top (Full Width)

Pageviews

Search This Blog