Posts

KNN Algorithm

Image
KNN stands for K-Nearest Neighbour It is used to solve both Classification & Regression problems. 1. KNN FOR CLASSIFICATION Here, we have one group of Green points and one group of Red points. Now, for yellow data point, we want to know whether it will belong to Red or Green group. Let's say, k=3  We will measure the distance and get 3 nearest data points from yellow. As you can see in the figure, 3 nearest data points will include 2 Green and 1 Red.  Hence, we will put it in Green category. We measure the distance by 2 methods : 1) Eucledian Distance : 2) Manhanttan Distance : Manhattan Distance =  | x 1 − x 2 | + | y 1 − y 2 | 2. KNN FOR REGRESSION If k=4, it will be average of 4 nearest data points. How to Choose the K-factor? Have a look at the below graphs for training error and validation error for different values of k. First Fig shows error in training set and Second Fig shows the error in validation set. For smaller values of k, training error is near to 0 a...

Naive Bayes Algorithm

Image
It is used to solve classification problems. This Algorithm is based on Bayes theorem , but before understanding bayes theorem, let's revise some probability concepts:  Independent Event : ex, rolling a dice Prob. of 1 on top : 1/6 Prob. of 2 on top : 1/6 Prob. of 5 on top : 1/6 Here, all events are independent from each other. Dependent Event : ex, A box has 3 red balls and 2 blue balls. Probability of taking a Red ball : 3/5 Probability of taking a Blue ball : 2/ 4 As one ball is removed, for the next event, we reduces the total no. of balls by 1. Since, Dependent Events. P(A and B)   = P(A)P(B|A) We know that, P(A and B)   = P(B and A) P(A)P(B|A) = P(B)P(A|B) Baye's Theorem :  = events = probability of A given B  = probability of B given A  = the independent probabilities of A and B For, Dependemt Features : X1,X2,......Xn Independent Feature : y here, base is same for P(yes/Xi) and P(No/Xi) and its constant, so we can ignore it These probabilities invol...

Train-test split & Cross-Validation

Image
  Train-test split : Let's say we have a dataset with 100 samples. Now, if we train the model on all 100 samples and test the results from this 100 samples only, then even if the data is overfitted, we won't know! So, to avoid this, we use train-test split In train-test split, we divide the data into 2 parts: 1) Training Set : We use 70-80% data as a training set. So, for above example we won't train all 100 samples but we will train the model on 80 samples. 2) Test set : The test set usually contains the remaining 20-30% of the data. So, for above example we will test model for the rest 20 samples. The test set is used to evaluate how well the model generalizes to new, unseen data . The performance metrics (such as accuracy, precision, recall, etc.) are calculated based on the model's predictions on the test set . Cross-validation : Divides the dataset into multiple folds. Some of the folds as training set & some of the folds as testing set. This process is repeate...