Machine Learning: Precision, Recall, F1-Score

Mudgalvivek
3 min readJun 24, 2020

--

Precision , Recall are the component mainly used for Information Retrieval . As like Google search we enter any statement so based on higher precision the data is shown in Google search .

In pattern recognition, information retrieval and classification, precision is the fraction of relevant instances among the retrieved instances, while recall is the fraction of the total amount of relevant instances that were actually retrieved.

Precision is the percentage of actual positive value in Predicted Positive value.

Recall is the percentage of predicted positive value for actual positive value.

This will be confusing please refer below image :

so for +ve :
Recall = TP/(TP+FN)
Precision = TP/(TP+FP)
so for -ve :
Recall = TN/(TN+FP)
Precision = TN/(TN+FN)

F1-Score :

The F1 score is the harmonic mean of the precision and recall, where an F1 score reaches its best value at 1 (perfect precision and recall). The F1 score is also known as the Sørensen–Dice coefficient or Dice similarity coefficient (DSC).

f1-Score = 2 * (Precision * Recall)/(Precision + Recall)

In [1]:

from sklearn.metrics import classification_reportfrom sklearn.datasets import load_iris

In [2]:

data = load_iris()
data.keys()

Out[2]:

dict_keys(['data', 'target', 'target_names', 'DESCR', 'feature_names', 'filename'])

In [3]:

import pandas as pd
x = pd.DataFrame(data['data'],columns=data['feature_names'])
x.head()

Out[3]:

sepal length (cm)sepal width (cm)petal length (cm)petal width (cm)05.13.51.40.214.93.01.40.224.73.21.30.234.63.11.50.245.03.61.40.2

In [4]:

y = data['target']
y

Out[4]:

array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])

In [5]:

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x=sc.fit_transform(x)
x_train,x_test,y_train,y_test = train_test_split(x,y,train_size=0.8,random_state=43)

In [6]:

from sklearn.neighbors import KNeighborsClassifierknn = KNeighborsClassifier(n_neighbors=5)
knn.fit(x_train,y_train)
y_pred = knn.predict(x_test)

In [7]:

from sklearn.metrics import confusion_matrixconfusion_matrix(y_test,y_pred)

Out[7]:

array([[13,  0,  0],
[ 0, 8, 0],
[ 0, 2, 7]], dtype=int64)

In [9]:

print(classification_report(y_test,y_pred))

Out [9]:

precision    recall  f1-score   support0       1.00      1.00      1.00        13
1 0.80 1.00 0.89 8
2 1.00 0.78 0.88 9
micro avg 0.93 0.93 0.93 30
macro avg 0.93 0.93 0.92 30
weighted avg 0.95 0.93 0.93 30

Thanks for Reading.
I hope I’ve given you some understanding on what exactly Precision , Recall and F1-Score. If you like this post, a tad of extra motivation will be helpful by giving this post some claps 👏. I am always open for your questions and suggestions. You can share this on Facebook, Twitter, Linkedin, so someone in need might stumble upon this.

Follow me :

Facebookhttps://www.facebook.com/vivek.mudgal.71
Linkedin www.linkedin.com/in/vivek-mudgal-7696a77a
Twitterhttps://twitter.com/vivekmudgal4

--

--

Mudgalvivek

Software Engineer By Profession, Data Science & AI Educator, Writer. Interests: Data Science, Machine Learning, AI, Python , Predictive Analytics.