Are you aiming at a position in the field of machine learning? Excellent choice! A job in ML guarantees a challenging yet satisfying career where you can evolve professionally and get rewarded generously. And the best news – the job market is ‘hungry’, if not ‘starving’ for professionals. No wonder that machine learning engineer is one of the most wanted jobs in data science for 2023.
So how to prepare for a machine learning interview?
If you are fresh out of school or switching from a non-related career and want to break into the field, there’s no need to rush – you will first have to gain the necessary skills required for the job. So, before you get started, you can check out this comprehensive career guide on how to become a machine learning engineer. Based on more than 500 job postings, it answers the most common questions every machine learning engineer enthusiast needs to know.
If you, on the other hand, already have some ML experience, start by revising the theory behind your daily ML operations. One way to do that is to take the dust off the books or maybe just take a machine learning course to identify knowledge gaps and refresh your skills.
Then, after you have also polished your resume and project portfolio, comes an equally important part of your interview preparation – investigating the popular ML interview questions which will most likely come forth during your interview.
Starting out, a good thing to remember is that machine learning questions brought up in a data science interview generally fall into three major categories:
- conceptual questions which test whether you have a solid theoretical machine learning background or not
- resume-driven questions based on your data science resume projects
- end-to-end modeling questions which test whether you can apply machine learning to the real business problem related to the company you are applying to
In this article, we will focus on the first category, namely, the conceptual questions.
For a data scientist role, conceptual machine learning questions usually center around the different ML terms and how popular algorithms operate.
However, if you are applying for a machine learning engineer job, you should expect to be asked about deeper and more advanced ML-related concepts and issues.
Here you will find a list of the most common ones, and, to make things better, the respective answers.
Top 10 Machine Learning Interview Questions – Table of Contents:
- Explain the linear regression model and discuss its assumption.
- Describe the motivation behind random forests.
- What are the differences and similarities between gradient boosting and random forest?
- Briefly explain the K-Means clustering and how can we find the best value of K.
- What is dimensionality reduction?
- What are L1 and L2 regularization?
- What is the difference between overfitting and underfitting?
- What are the bias and variance in a machine learning model?
- Define precision, recall, and F1 and discuss the trade-off between them.
- Mention three ways to handle missing or corrupted data in a dataset.
Bonus Question: Discuss how to make your model robust to outliers.
1. Explain the linear regression model and discuss its assumption.
Linear regression is a form of supervised learning, where the model is trained on labeled input data. In linear regression, the goal is to estimate a function f(x) sо that each feature has a linear relationship to the target variable y where y= X*beta. X is a matrix of predictor variables and beta is a vector of parameters that determines the weight of each variable when predicting the target variable.
Since linear regression is one of the most commonly used models, it has the honor of also being one of the most misapplied ones. So before running it, you must validate its four main assumptions to prevent false results:
- Linearity: The relation between the feature set and the target variable is linear.
- Homoscedasticity: The variance of the residuals is constant.
- Independence: All observations are independent of one another.
- Normality: The distribution of Y is assumed to be normal.
The widespread empirical application of linear regression means that the interview questions will assure you have more knowledge than just blindly importing it from scikit-learn and using it. Interviewers will try to determine whether you have a deep understanding of how the model works, its assumption, and the different evaluation metrics. They will be addressing edge cases that come up in real-life scenarios and challenge your ability to put theory into practice.
2. Describe the motivation behind random forests and mention two reasons why they are better than individual decision trees.
The motivation behind random forest or ensemble models can be explained easily by using the following example: Let’s say we have a question to solve. We gather 100 people, ask each of them this question, and record their answers. After we combine all the replies we have received, we will discover that the aggregated collective opinion will be close to the actual solution to the problem. This is known as the “Wisdom of the crowd” which is, in fact, the motivation behind random forests. We take weak learners (ML models) specifically, decision trees in the case of random forest, and aggregate their results to get good predictions by removing dependency on a particular set of features. In regression, we take the mean and for classification, we take the majority vote of the classifiers.
Generally, you should note that no algorithm is better than the other. It always depends on the case and the dataset used (Check the No Free Lunch Theorem). Still, there are reasons why random forests often allow for stronger prediction than individual decision trees:
- Decision trees are prone to overfit whereas random forest generalizes better on unseen data as it uses randomness in feature selection and during sampling of the data. Therefore, random forests have lower variance compared to that of the decision tree without substantially increasing the error due to bias.
- Generally, ensemble models like random forests perform better as they are aggregations of various models (decision trees in the case of a random forest), using the concept of the “Wisdom of the crowd.”
3. What are the differences and similarities between gradient boosting and random forest? And what are the advantages and disadvantages of each when compared to each other?
The similarities between gradient boosting and random forest can be summed up like this:
- Both these algorithms are decision-tree based.
- Both are also ensemble algorithms - they are flexible models and do not need much data preprocessing.
There are two main differences we can mention here:
- Random forest uses Bagging. This means that trees are arranged in a parallel fashion, where the results from all of them are aggregated at the end through averaging or majority vote. On, the other hand, gradient boosting uses Boosting, where trees are arranged in a series sequential fashion, where every tree tries to minimize the error of the previous one.
- In random forests, every tree is constructed independently of the others, whereas, in gradient boosting, every tree is dependent on the previous one.
When we discuss the advantages and disadvantages between the two it is only fair to juxtapose them both with their weaknesses and with their strengths. We need to keep in mind that each one of them is more applicable in certain instances than the other and vice versa. It depends, on the outcome we want to reach and the task we need to solve.
So, the advantages of gradient boosting over random forests include:
- Gradient boosting can be more accurate than random forests because we train them to minimize the previous tree’s error.
- It can also capture complex patterns in the data.
- Gradient boosting is better than random forest when used on unbalanced data sets.
On the other hand, we have the advantages of random forest over gradient boosting as well:
- Random forest is less prone to overfitting compared to gradient boosting.
- It has faster training as trees are created in parallel and independent of each other.
Moreover, gradient boosting also exhibits the following weaknesses:
- Due to the focus on mistakes during training iterations and the lack of independence in tree building, gradient boosting is indeed more susceptible to overfitting. If the data is noisy, the boosted trees might overfit and start modeling the noise.
- In gradient boosting, training might take longer because every tree is created sequentially.
- Additionally, tunning the hyperparameters of gradient boosting is more complex than those of random forest.
4. Briefly explain the K-Means clustering and how can we find the best value of K.
K-means is a well-known clustering algorithm. It is often used because of its ease of interpretation and implementation. The algorithm starts by partitioning a set of data into K distinct clusters and then arbitrary selects centroids of each of these clusters. It iteratively updates partitions by first assigning the points to the closet cluster and then updating the centroid, repeating this process until convergence. The process essentially minimizes the total inter-cluster variation across all clusters.
The elbow method is well-known when finding the best value of K in K-means clustering. The intuition behind this technique is that the first few clusters will explain a lot of the variation in the data. However, past a certain point, the amount of information added is diminishing. Looking at the graph below (figure 1.) of the explained variation (on the y-axis) versus the number of cluster K (on the x-axis), there should be a sharp change in the y-axis at some level of K. In this particular case, the drop off is at k=3.
Figure 1. The elbow diagram to find the best value of K in K-Means clustering
The explained variation is quantified by the within-cluster sum of squared errors. To calculate this error notice, we look for each cluster at the total sum of squared errors using Euclidean distance.
Another popular alternative method to find the value of K is to apply the silhouette method, which aims to measure how similar points are in its cluster compared to other clusters. It can be calculated with this equation: (x-y)/max(x,y), where x is the mean distance to the examples of the nearest cluster, and y is the mean distance to other examples in the same cluster. The coefficient varies between -1 and 1 for any given point. A value of 1 implies that the point is in the right cluster and the value of -1 implies that it is in the wrong cluster. By plotting the silhouette coefficient on the y-axis versus each K we can get an idea of the optimal number of clusters. However, it is worth noting that this method is more computationally expensive than the previous one.
5. What is dimensionality reduction? Can you discuss one method of it?
Dimensionality reduction decreases the complexity or the dimension of your data with a minimal loss of important information. Decomposing the data into a smaller set of variables is also useful for summarizing and visualizing datasets. For example, dimensionality reduction methods can be used to project a large dataset into 2D or 3D space for easier visualization.
One of the most common methods used for dimensionality reduction is principal component analysis (PCA). PCA combines highly correlated variables into a new smaller set of constructs called principal components that capture most of the variance in the data. The algorithm looks for a small number of independent linear combinations for each row vector to explain the variance. So, the algorithm proceeds first by finding the component having maximal variance. Then, the second one found is uncorrelated with the first and has the second-highest variance, and so on for the other components. Generally, their number depends on your threshold for the percent of variance your principal components can explain.
6. What are L1 and L2 regularizations? What are the differences between the two?
Regularization is a technique used to avoid overfitting by trying to make the model simpler. One way to apply regularization is by adding the weights to the loss function. This is done to consider minimizing unimportant weights. In L1 regularization, we add the sum of the absolute of the weights to the loss function. In L2 regularization, we add the sum of the squares of the weights to the loss function.
So, both L1 and L2 regularization are ways to reduce overfitting, but to understand the difference it’s better to know how they are calculated:
Loss (L2): Cost function + L * weights ²
Loss (L1): Cost function + L * |weights|
Where L is the regularization parameter.
L2 regularization penalizes huge parameters preventing any of the single parameters to get too large. But weights never become zeros. It adds parameters square to the loss. Averting the model from overfitting any single feature.
L1 regularization penalizes weights by adding a term to the loss function which is the absolute value of the loss. This leads to removing small values of the parameters until the parameter hits zero and stays there for the rest of the epochs. Removing completely this specific variable from our calculation. So, it helps for simplifying our model and for feature selection as it shrinks the coefficient to zero which is not significant in the model.
7. What is the difference between overfitting and underfitting, and how can you avoid them?
Overfitting means that the model is doing well on the training data, but it does not generalize well on the test/validation data. It could be noticed when the training error is small, and the validation and test error is large. Overfitting happens when the model is too complex relative to the size of the data and its quality. This will result either in learning more about the pattern in the data noise or in very specific patterns in the data, which the model will not be able to generalize for new instances.
Here are possible solutions for overfitting:
- Simplify the model by decreasing the number of features or using regularization parameters.
- Collect more representative training data.
- Reduce the noise in the training data using data cleaning techniques.
- Decrease the data mismatch using data preprocessing techniques.
- Use a validation set to detect when overfitting begins and stop the training.
Underfitting is, respectively, the opposite of overfitting. The model in this instance is too simple to learn any of the patterns in the training data. This could be seen when the training error is large, and the validation and test error is large.
Here are several possible solutions:
- Select a more complex model with more parameters.
- Reduce the regularization parameter if you are using it.
- Feed better features to the learning algorithm using feature engineering.
8. What are the bias and variance in a machine learning model and explain the bias-variance trade-off?
The goal of any supervised machine learning model is to estimate the mapping function (f) that predicts the target variable (y) given input (x). The prediction error can be broken down into three parts:
- Bias: The bias is the simplifying assumption made by the model to make the target function easy to learn. Low bias suggests fewer assumptions made about the form of the target function. High bias suggests more assumptions made about the form of the target data. The smaller the bias error the better the model is. If, however, it is high, this means that the model is underfitting the training data.
- Variance: Variance is the amount that the estimate of the target function will change if different training data was used. The target function is estimated from the training data by a machine learning algorithm, so we should expect the algorithm to have some variance. Ideally, it should not change too much from one training dataset to the next. This means that the algorithm is good at picking out the hidden underlying mapping between the inputs and the output variables. If the variance error is high this indicates that the model overfits the training data.
- Irreducible error: It is the error introduced from the chosen framing of the problem and may be caused by factors like unknown variables that influence the mapping of the input variables to the output variable. The irreducible error cannot be reduced regardless of what algorithm is used.
Supervised machine learning algorithms aim at achieving low bias and low variance. In turn, the algorithm should also attain good prediction performance. The parameterization of such ML algorithms is often a battle to balance out bias and variance. For example, if you want to predict the housing prices given a large set of potential predictors, a model with high bias but low variance, such as linear regression, will be easy to implement. However, it will oversimplify the problem, and, in this context, the predicted house prices will be frequently off from the market value but the value of the variance of these predicted prices will be low. On the other hand, a model with low bias and high variance, such as a neural network, will lead to predicted house prices closer to the market value, but with predictions varying widely based on the input features.
9. Define precision, recall, and F1 and discuss the trade-off between them.
Precision and recall are two classification evaluation metrics that are used beyond accuracy.
Consider a classification task with many classes. Both metrics are defined for a particular class, not the model in general. Precision of class, let’s say, A, indicates the ratio of correct predictions of class A to the total predictions classified as class A. It is similar to accuracy but applied to a single class. Therefore, precision may help you judge how likely a given prediction is to be correct. Recall is the percentage of correctly classified predictions of class A out of all class A samples present in the test set. It indicates how well our model can detect the class in question.
In the real world, there is always a trade-off between optimizing for precision and recall. Consider you are working on a task for classifying cancer patients from healthy people. Optimizing the model to have only high recall will mean that the model will catch most of the people with cancer but at the same time, the number of cancer misdiagnosed people will increase. This will subject healthy people to dangerous and costly treatments. On the other hand, optimizing the model to have high precision will make the model confident about the diagnosis, in favor of missing some people who truly have the disease. This will lead to fatal outcomes as they will not be treated. Therefore, it is important to optimize both precision and recall and the percentage of importance of each of them will depend on the application you are working on.
This leads us to the last point of the question. F1 score is the harmonic mean of precision and recall, and it is calculated using the following formula: F1 = 2* (precision*recall) / (precision + recall). The F1 score is used when the recall and the precision are equally important.
10. Mention three ways to handle missing or corrupted data in a dataset.
In general, real-world data often has a lot of missing values. The cause of this can be data corruption or failure to record the data. The handling of missing data is very important during the preprocessing of the dataset as many machine learning algorithms do not support missing values.
There are several different ways to handle these but here, the focus will be on the most common ones.
- Deleting the row with missing values
The first method is to delete the rows or columns that have null values. This is an easy and fast way and leads to a robust model. However, it will cause the loss of a lot of information depending on the amount of missing data. Therefore, it can only be applied if the missing data represents a small percentage of the whole dataset.
- Using learning algorithms that support missing values
Some machine learning algorithms are quite efficient when it comes to missing values in the dataset. The K-NN algorithm can ignore a column from a distance measure when there are missing values. Naive Bayes can also support missing values when making a prediction. Another algorithm that can handle a dataset with missing values or null values is the random forest model, as it can work on non-linear and categorical data. The problem with this method is that these models' implementation in the scikit-learn library does not support handling missing values, so you will have to implement it yourself.
- Missing value imputation
Data imputation implies the substitution of estimated values for missing or inconsistent data in your dataset. There are different ways of determining these replacement values. The simplest one is to change the missing value with the most repeated one in the row or the column. Another simple solution is accommodating the mean, median, or mode of the rest of the row, or the column. The advantage here is that this is an easy and quick fix to missing data, but it might result in data leakage and does not factor the covariance between features. A better option is to use an ML model to learn the pattern between the data and predict the missing values without any data leakage, and the covariance between the features will be factored. The only drawback here is the computational complexity, especially for large datasets.
Bonus Question: Discuss how to make your model robust to outliers.
There are several options when it comes to strengthening your model in terms of outliers. Investigating these outliers is always the first step in understanding how to treat them. After you recognize the nature of why they occurred, you can apply one of the several methods below:
- Add regularization that will reduce variance, for example, L1 or L2 regularization.
- Use tree-based models (random forest, gradient boosting) that are generally less affected by outliers.
- Winsorize the data. Winsorizing or winsorization is the transformation of statistics by limiting extreme values in the statistical data to reduce the effect of possibly spurious outliers. In numerical data, if the distribution is almost normal using the Z-score, we can detect the outliers and treat them by either removing or capping them with some value.
If the distribution is skewed, using IQR we can detect and treat it again either by removing or capping it with some value. In categorical data check for value counts in the percentage. If we have very few records from some category, we can either remove it or cap it with some categorical value like others.
- Transform the data. For example, you can do a log transformation when the response variable follows an exponential distribution, or when it is right-skewed.
- Use more robust error metrics such as MAE or Huber loss instead of MSE.
- Remove the outliers. However, do this if you are certain that the outliers are true anomalies not worth adding to your model. This should be your last consideration since dropping them means losing information.
Top 10 Machine Learning Interview Questions and Answers: Next Steps
Regardless of your current position, newly graduate, practicing professional, or course graduate, preparing for an interview requires time and attention. Anything from building your resume, to the actual application process and preparing for a machine learning interview needs dedication on your side.
However, if the reward awaiting you at the end is that ML dream job, then it is all worth it. As mentioned earlier, this is a field of great interest for both demand and supply. This, respectively, implies various knowledge and skill requirements, but in the end, the satisfaction rate among machine learning professionals is one of the highest in data science.
So, it is a good investment to spend some quality time preparing for your machine learning interview questions. And I hope this article is a solid starting point, as it gives you a sneak peek into the most common questions and examples of what your answers should look like.
Keep in mind that the list is not all-embracing and many other questions could be asked. The good news is they will probably be similar. To answer any of them, you should freshen up the must-have knowledge of ML, like the basic machine learning concepts, popular ML algorithms, when and where to use them, and what their advantages and disadvantages are. That said, if you feel a bit rusty when it comes to this, we’ve got you covered.
The 365 Data Science Program offers self-paced courses led by renowned industry experts. Starting from the very basics all the way to advanced specialization, you will learn by doing a myriad of practical exercises and real-world business cases. If you want to see how the training works, start with a selection of free lessons by signing up below.