Linear Regression is a supervised machine learning algorithm where the predicted output is continuous and has a constant slope. It’s used to predict values within a continuous range, (e.g. sales, price) rather than trying to classify them into categories (e.g. cat, dog). Follow along and check the 25 most common Linear Regression Interview Questions (EXPLAINED) before your next data analyst and machine learning interview.
Linear Regression is a supervised machine learning algorithm where the predicted output is continuous and has a constant slope. It’s used to predict values within a continuous range, (e.g. sales, price) rather than trying to classify them into categories (e.g. cat, dog).
1. Like . If these non-linear functions are graphed, they do not produce a straight line (their direction changes constantly).1. They form a straight line if it is graphed.MSE = (1/n) * Σ(actual – forecast)2
The common pattern for overfitting can be seen on learning curve plots, where model performance on the training dataset continues to improve (e.g. loss or error continues to fall) and performance on the test or validation set improves to a point and then begins to get worse.
So an overfit model will have extremely low training error but a high testing error.
Simple linear regression uses traditional slope-intercept form. 𝑥 represents our input data and 𝑦 represents our prediction.
𝑦 = 𝑚𝑥+𝑏
A more complex, multi-variable linear equation might look like this, where 𝑤 represents the coefficients, or weights, our model will try to learn.
𝑓(𝑥,𝑦,𝑧) = 𝑤1𝑥+𝑤2𝑦+𝑤3𝑧
The variables 𝑥, 𝑦, 𝑧 represent the attributes, or distinct pieces of information, we have about each observation. For sales predictions, these attributes might include a company’s advertising spend on radio, TV, and newspapers.
𝑆𝑎𝑙𝑒𝑠 = 𝑤1𝑅𝑎𝑑𝑖𝑜+𝑤2𝑇𝑉+𝑤3*𝑁𝑒𝑤𝑠
The Mean Squared Error measures the variance of the residuals and is used when we want to punish the outliers in the dataset. It's defined as:
The Mean Absolute Error measures the average of the residuals in the dataset. Is used when we don’t want outliers to play a big role. It can also be useful if we know that our distribution is multimodal, and it’s desirable to have predictions at one of the modes, rather than at the mean of them. It's defined as:
The Gradient Descent works by starting with random values for each coefficient in the linear regression model.
The constant term in regression analysis is the value at which the regression line crosses the y-axis. The constant is also known as the y-intercept.
The intercept term signifies the independent variable’s shift from the origin and ensures that the model would be unbiased.
The Constant Absorbs the Bias for the Regression Model.
If we omit the intercept term, then the model is forced to go through the origin and the slope would become steeper and biased. Hence, we should not remove the intercept term unless we are completely sure that it is 0 according to theory and expectations.
Hypothesis testing is used in a linear regression model to test if the β1 parameter in the linear equation is statistically significant. In other words, to check if the linear relationship that we obtained was not caused just by random chance.
The way to use hypothesis testing is described as follows:
We start establishing the null hypothesis (H0) that β1 is not significant, i.e. there is no linear relationship between independent variables and the dependent variable. The alternative hypothesis (H1) is then that β1 is not zero.
H0 : β1=0HA : β1≠00.05, we can reject the null hypothesis and state that β1 is indeed significant at the 95% confidence level. With this, we can validate that our model coefficients are not obtained just by random chance.
A way to perform the variable selection is trying out different models, each containing a different subset of the predictors. For instance, if the number of predictors is 2, then we can consider 4 models:
X1 only.X2 only. X1 and X2. We can then select the best model out of all of the models that we have considered by computing some statistics like Adjusted R-squared. However, if the number of predictors is high, we must use some more elaborated methods for feature selection, like:
R-squared (R2) is a statistical measure of how close the data are to the fitted regression line. It is also known as the coefficient of determination, or the coefficient of multiple determination for multiple regression.
R-squared takes values between 0 and 1, with 0 indicating that the proposed model does not improve prediction over the mean model and 1 indicating the perfect prediction. However, one drawback of R-squared is that its values can increase if we add predictors to the regression model, leading to a possible overfitting.
To address this issue, we can use Adjusted R-squared: a modified version of R-squared that has been adjusted for the number of predictors in the model. The adjusted R-squared increases when the new term improves the model more than would be expected by chance, and it decreases when a predictor improves the model by less than expected.
Mean absolute error (MAE): calculates the absolute difference between actual and predicted values. It can be used when we want that our model be robust to outliers, but this metric has the disadvantage of not being differentiable so we can't use it if we want to apply optimizers like Gradient descent.
Mean squared error (MSE): calculates the squared difference between actual and predicted value. We can use this metric if we want to give bigger penalization to outliers and apply optimizers who require differentiation. MSE is a differentiable function that makes it easy to perform mathematical operations in comparison to a non-differentiable function like MAE.
Root mean squared error (RMSE): This is simply the square root of mean squared error. This metric is not so robust to outliers as the mean absolute error but it has the advantage to be differentiable so we can use it if we want to apply gradient descent to minimize losses.
When to use one depends on your loss function:
We make a few assumptions when we use linear regression to model the relationship between a response and a predictor. These assumptions are essential conditions that should be met before we draw inferences regarding the model estimates or before we use a model to make a prediction.
There are four principal assumptions which justify the use of linear regression models for purposes of inference or prediction:
(i) Linearity and Additivity of the relationship between dependent and independent variables:
(ii) Statistical Independence of the errors (in particular, no correlation between consecutive errors in the case of time series data)
(iii) Homoscedasticity (constant variance) of the errors
(iv) Normality of the error distribution.
Ordinary least squares fit a linear model by minimizing the residual sum of squares between the observed targets in the dataset and the targets predicted by the linear approximation. Mathematically it solves a problem of the form:
The Lasso regression fits a linear model that estimates sparse coefficients. It is useful in some contexts due to its tendency to prefer solutions with fewer non-zero coefficients, effectively reducing the number of features upon which the given solution is dependent. Mathematically, it consists of a linear model with an added regularization term:
It is required for the independent and dependent variables to be linear for linear regression models, but the independent and dependent variables are not required to have a linear relationship in logistic functions.
The Linear Regression models assume that the error terms are normally distributed (bell-shaped graph) whereas there are no error terms in Logistic Regression because it is assumed to follow a Bernoulli distribution.
0 and 1. A linear regression may have an output that can go beyond 0 and 1.This depends on your loss function. In many circumstances it makes sense to give more weight to points further away from the mean:
10 is more than twice as bad as being off by 5. In such cases, RMSE is a more appropriate measure of error.10 is just twice as bad as being off by 5, then MAE is more appropriate.Another situation when you want to use (R)MSE instead of MAE: when your observations' conditional distribution is asymmetric and you want an unbiased fit. The (R)MSE is minimized by the conditional mean, the MAE by the conditional median. So if you minimize the MAE, the fit will be closer to the median and biased.
Normalization/standardization are designed to achieve a similar goal, which is to create features that have similar ranges to each other. We want that so we can be sure we are capturing the true information in a feature and that we don't overweigh a particular feature just because its values are much larger than other features.
If all of your features are within a similar range of each other then there's no real need to standardize/normalize. If, however, some features naturally take on values that are much larger/smaller than others then normalization/standardization is called to fix it.
If you're going to be normalizing at least one variable/feature, I would do the same thing to all of the others as well.
Prepare for AI developer and engineer interviews with 19 answered OpenClaw questions covering Gateway architecture, channels, agent workspaces, memory, MCP, model failover, multi-agent routing, security, sandboxing, approvals, and remote operations....
Prepare for AI agent developer interviews with 15 Model Context Protocol (MCP) questions covering tools, resources, prompts, JSON-RPC, transports, roots, sampling, security, and practical MCP server design....
Amazone runs the internet as we know it. Amazon Web Services (AWS) offers a comprehensive suite of machine learning (ML) services that cater to various needs and expertise levels. Follow along and learn the 23 most common AWS machine-learning intervi...