Skip to main content

Machine Learning-Cross Validation & ROC curve


Another post starts with you beautiful people!
Hope you enjoyed my previous post about improving your model performance by confusion metrix.
Today we will continue our performance improvement journey and will learn about Cross Validation (k-fold cross validation) & ROC in Machine Learning.

A common practice in data science competitions is to iterate over various models to find a better performing model. However, it becomes difficult to distinguish whether this improvement in score is coming because we are capturing the relationship better or we are just over-fitting the data. To find the right answer of this question, we use cross validation technique. This method helps us to achieve more generalized relationships.

What is Cross Validation?
Cross Validation is a technique which involves reserving a particular sample of a data set on which we do not train the model. Later, we test the model on this sample before finalizing the model.
Here are the steps involved in cross validation:

  • We reserve a sample data set.
  • Train the model using the remaining part of the data set.
  • Use the reserve sample of the data set test (validation) set. This will help us to know the effectiveness of model performance. It our model delivers a positive result on validation data, go ahead with current model.
The k-fold cross validation method of cross validation technique takes care of below three requirements-

  • We should train model on large portion of data set. Else, we’d fail every time to read the underlying trend of data sets. Eventually, resulting in higher bias.
  • We also need a good ratio testing data points. As, we have seen that lower data points can lead to variance error while testing the effectiveness of model.
  • We should iterate on training and testing process multiple times. We should change the train and test data set distribution. This helps to validate the model effectiveness well.
Here are the steps to implement k-fold validation method:-
  • Randomly split our entire dataset into k”folds”.
  • For each k folds in our dataset, build our model on k – 1 folds of the data set. Then, test the model to check the effectiveness for kth fold.
  • Record the error we see on each of the predictions.
  • Repeat this until each of the k folds has served as the test set.
  • The average of our k recorded errors is called the cross-validation error and will serve as our performance metric for the model.
In this exercise will train our model with the same dataset and continue our step after random forest step as we did in last post of Confusion Metrix. Please revise those steps from previous post.

Comparing above result with Random forest:-


We can see that the accuracy has been increased when performed Cross-Validation in random forest classifier as well as for logistic regression.

Now train the model on whole data and predict the future data points:-


From above results it is quite clear that-
The accuracy scores for
        Random Forest on train/test split : 75                   
        Logistic Regression on train/test split: 75.5  
        Random Forest on Cross Validation : 77.09         
        Logistic Regression on Cross Validation : 76.8

So with cross-validataion there is high probability of increasing model accuracy.

Adjusting the classification threshold:-


From the above graph we find following result for our dataset:-
  • Decrease the threshold for predicting diabetes in order to increase the sensitivity of the classifier
  • Threshold of 0.5 is used by default (for binary problems) to convert predicted probabilities into class predictions.
  • Threshold can be adjusted to increase sensitivity or specificity Sensitivity and specificity have an inverse relationship.
Wouldn't it be nice if we could see how sensitivity and specificity are affected by various thresholds, without actually changing the threshold?
Yes, we can and answer is by plotting ROC curve.
For more details about this curve please visit here- what is ROC?
  • ROC curve tries to evaluate how well the model has achieved the seperation between the classes at all threshold values.
  • ROC curve can help us to choose a threshold that balances sensitivity and specificity in a way that makes sense for our particular context.

Result:-

Define a function that accepts a threshold and prints sensitivity and specificity:-


Conclusion of this exercise:
In this way business can understand where should the threshold be set so as to maximize Sensitivity or Specificity.

In my next post we will learn about Principal component analysis or PCA.

Comments

  1. Nice post ! Thanks for sharing valuable information with us. Keep sharing Data Science online Course

    ReplyDelete
    Replies
    1. Very Impressive ROC Curve Data Science tutorial. The content seems to be pretty exhaustive and excellent and will definitely help in learning ROC Curve Data Science course. I'm also a learner taken up ROC Curve Data Science training and I think your content has cleared some concepts of mine. While browsing for ROC Curve Data Science tutorials on YouTube i found this fantastic video on ROC Curve Data Science.

      Machine Learning Final Year Projects

      Delete
  2. Very Impressive ROC Curve Data Science tutorial. The content seems to be pretty exhaustive and excellent and will definitely help in learning ROC Curve Data Science course. I'm also a learner taken up ROC Curve Data Science training and I think your content has cleared some concepts of mine. While browsing for ROC Curve Data Science tutorials on YouTube i found this fantastic video on ROC Curve Data Science. Do check it out if you are interested to know more.:-https://www.youtube.com/watch?v=G_pvQYUm8Ik

    ReplyDelete
  3. Thank you for sharing such great information very useful to us.
    Python Training in Noida

    ReplyDelete
  4. Here is the investors contact Email details,_   lfdsloans@lemeridianfds.com  Or Whatsapp +1 989-394-3740 that helped me with loan of 90,000.00 Euros to startup my business and I'm very grateful,It was really hard on me here trying to make a way as a single mother things hasn't be easy with me but with the help of Le_Meridian put smile on my face as i watch my business growing stronger and expanding as well.I know you may surprise why me putting things like this here but i really have to express my gratitude so anyone seeking for financial help or going through hardship with there business or want to startup business project can see to this and have hope of getting out of the hardship..Thank You.

    ReplyDelete
  5. This is most informative and also this post most user friendly and super navigation to all posts. Thank you so much for giving this information to me.python training in bangalore

    ReplyDelete
  6. This video helps me to understand Matplotlib whats your opinion guys.

    ReplyDelete
  7. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
    Register for a free Demo Sessions

    RPA Ui Path Online Training
    Best Python Online Training
    Online AWS Training
    Online Data Science Training

    ReplyDelete
  8. Great blog. All posts have something to learn. Your work is very good and I appreciate you and hopping for some more informative posts. Chat with Amateur Models

    ReplyDelete
  9. Through meticulous animation, Product Animation Services offer immersive presentations, allowing viewers to explore products from various angles and perspectives.

    ReplyDelete

Post a Comment

Popular posts from this blog

Relational Database in Python-CRUD operations

Another post starts with you beautiful people! Hope you have enjoyed and learn from my previous post about  Relational Database In Python-sqlite Previously, we used the Table object to reflect a table from an existing database , but what if you wanted to create a new table? We'd still use the Table object; however, we'd need to replace the autoload and autoload_with parameters with Column objects . The Column object takes a name, a SQLAlchemy type with an optional format, and optional keyword arguments for different constraints. After defining the table, we can create the table in the database by using the .create_all() method on metadata and supplying the engine as the only parameter. You can find more details about metadata here-  tell me more about metadata! Output:- Well done! When creating a table, it's important to carefully think about what data types each column should be. Constraints and Data Defaults- We're now going to practice creating a t...

My solution to HackerEarth's Identify the dance form challenge

Another post starts with you beautiful people! Today an interesting deep learning challenge is finished in  HackerEarth  and I got 91.17026 mAP score in the leader board. One drawback I see in HackerEarth is due to small dataset many participants manually prepare the submission files and show 100% score in the leader board. Many aspiring data scientists see this and become nervous. Even with getting score 75+, they become demotivated and leave their experiments in between the challenge. Also the winning approach is not disclosed after the challenge. With this post I will try to motivate my all aspiring data scientists and I will share my solution so that in their next challenge they can easily get 85+ score or even 92+ score :) Problem statement An event management company organized an evening of Indian classical dance performances to celebrate the rich, eloquent, and elegant art of dance. After the event, the company plans to create a micro site to promote and raise aw...