How to calculate precision recall and F1 score in R

This recipe helps you calculate precision recall and F1 score in R

Recipe Objective

How to calculate precision, recall and F1 score in R.

Logistic Regression is a classification type supervised learning model. Logistic Regression is used when the independent variable x, can be a continuous or categorical variable, but the dependent variable (y) is a categorical variable. The logistic regression makes prediction on the model and returns binary outputs of class 1 and class 0. A confusion matrix is then used to find the correctness and accuracy of the model. The confusion matrix consists of two types of values: the actual and the predicted ones. Precision: Out of all the positive classes we have predicted correctly, how many are actually positive. Sensitivity / true positive rate / Recall: It measures the proportion of actual positives that are correctly identified. F1 Score: The F1 score measures the accuracy of the models performance on the input dataset. It is the harmonic mean of precision and recall. All these measures must be as high as possible, which indicates better model accuracy. This recipe gives an example on how to calculate precision, recall and F1 score in R.

Step 1 - Define two vectors

For example, defining two vectors, consisting of actual and corresponding predicted values.

actual_values <- c(1,1,1,0,0,0,1,1,0,0) predict_value <- c(1,0,1,0,1,1,0,0,1,1)

Step 2 - Create a confusion matrix

Confusion matrix : Confusion matrix is a performance metric technique for summarizing the performance of a classification algorithm. The number of correct and incorrect predictions are summarized with count values and listed down by each class of predicted and actual values It gives you insight not only into the errors being made by your classifier but more importantly the types of errors that are being made.

TN:- The predicted and actual values are the same.
TP:- The predicted and actual values are the same.
FP:- The predicted value belongs to class 1 but actually it is in class 0.
FN:- The predicted value belongs to class 0 but actually it is in class 1.

table(ACTUAL=actual_values,PREDICTED=predict_value>0.5) # assuming thershold to be 0.5

Step 3 - Calculate the precision, recall and f1 score.

Precision : TP / (TP+FP) Recall : TP / (TP+FN) F1 Score : (2 * Precision * Recall) / (Precision+Recall)

precision <- 2/(2+4) precision

"The precision scrore is :"
0.333333333333333

recall <- 2/(2+3) recall

"The recall scrore is :"
0.4

f1_score <- (2*precision*recall)/(precision+recall) f1_score

"The f1 scrore is :"
0.363636363636364

{"mode":"full","isActive":false}

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Learn to Build an End-to-End Machine Learning Pipeline - Part 2
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, incorporating Hopsworks' feature store and Weights and Biases for model experimentation.

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.