Data Scientist Interview Questions – Explain what precision and recall are.
After the predictive model has been finished, the most important question is: How good is it? Does it predict well?
Evaluating the model is one of the most important tasks in the data science project, it indicates how good predictions are. Very often for classification problems we look at metrics called precision and recall, to define them in detail let’s quickly introduce confusion matrix first.
Confusion Matrix for binary classification is made of four simple ratios:
- True Negative(TN): case was true negative and predicted negative
- True Positive(TP): case was true positive and predicted positive
- False Negative(FN): case was true positive but predicted negative
- False Positive(FP): case was true negative but predicted positive
https://secretdatascientist.com" data-medium-file="https://i0.wp.com/secretdatascientist.com/wp-content/uploads/2017/07/Confusion-Matrix-1.png?fit=300%2C59&ssl=1" data-large-file="https://i0.wp.com/secretdatascientist.com/wp-content/uploads/2017/07/Confusion-Matrix-1.png?fit=1024%2C202&ssl=1" decoding="async" >
After fully understanding the confusion matrix, calculating precision and recall is easy.
Precision – is the ratio of correctly predicted positive observations to the total predicted positive observations, or what percent of positive predictions were correct?
Precision = TP/TP+FP
Recall – also called sensitivity, is the ratio of correctly predicted positive observations to all observations in actual class – yes, or what percent of the positive cases did you catch?
Recall = TP/TP+FN
There are also two more useful matrices coming from confusion matrix, Accuracy – correctly predicted observation to the total observations and F1 score the weighted average of Precision and Recall. Although intuitively it is not as easy to understand as accuracy, the F1 score is usually more useful than accuracy, especially if you have an uneven class distribution.
Example Python Code to get Precision and Recall:
from sklearn.linear_model import LogisticRegression from sklearn import datasets from sklearn.cross_validation import train_test_split from sklearn.metrics import precision_recall_fscore_support as score data = datasets.load_iris() X = data['data'] y = data['target'] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0) model = LogisticRegression() model.fit(X_train,y_train) preds = model.predict(X_test) precision, recall, fscore, support = score(y_test, preds) print('precision:',precision) print('recall:',recall)
Was the above useful? Please share with others on social media.
Recommended reading list:
|Data Science from Scratch: First Principles with Python
Data science libraries, frameworks, modules, and toolkits are great for doing data science, but they’re also a good way to dive into the discipline without actually understanding data science. In this book, you’ll learn how many of the most fundamental data science tools and algorithms work by implementing them from scratch.
If you have an aptitude for mathematics and some programming skills, author Joel Grus will help you get comfortable with the math and statistics at the core of data science, and with hacking skills you need to get started as a data scientist. Today’s messy glut of data holds answers to questions no one’s even thought to ask. This book provides you with the know-how to dig those answers out.
Get a crash course in Python
Learn the basics of linear algebra, statistics, and probability—and understand how and when they're used in data science
Collect, explore, clean, munge, and manipulate data
Dive into the fundamentals of machine learning
Implement models such as k-nearest Neighbors, Naive Bayes, linear and logistic regression, decision trees, neural networks, and clustering
Explore recommender systems, natural language processing, network analysis, MapReduce, and databases