Javatpoint Logo
Javatpoint Logo

Multivariate Linear Regression in Python

"Multivariate Linear Regression" and "Multiple Linear Regression" usually confer the same concept in the context of linear regression modeling. Both terms describe a linear regression version in which you have multiple impartial variables (features) used to expect a single structured variable (target). In different phrases, each phrase means a linear regression version with more than one predictor variable.

Linear regression is a crucial device getting to know the method used for predicting a non-stop purpose variable based totally on one or more unbiased features. When we've got more than one unbiased capability, it is called multivariate linear regression. In this article, we will delve into the area of multivariate linear regression and enforce it in Python.

Understanding Multivariate Linear Regression

Multivariate linear regression extends clean linear regression to a couple of unbiased variables. Instead of getting in reality one feature (X) to predict a goal (Y), we have numerous capabilities (X1, X2, ..., Xn). The purpose stays the same: to locate the exceptional linear dating of a number of the independent variables and the goal variable.

Multivariate linear regression's general formula is:

Y = b0 + b1 * X1 + b2 * X2 +b3 * X3 + ……………bn * Xn + ?

The goal variable here is Y and X1 , X2 , X3 , X4 , …………Xn are the independent variables, b0 is the intercept, b1 , b2, b3 , b4 , ……… are the coefficients, and ? represents the error term.

Assumption of Regression Model :

  1. Homoscedasticity: Constant variance of the mistakes must be maintained.
  2. Linearity: The relationship between based and unbiased variables ought to be linear.
  3. Lack of Multicollinearity: It is believed that there is very little multicollinearity within the records.
  4. Multivariate normality: Multiple Regression assumes that the residuals are commonly disbursed.

Input code:


Coefficients: [0.58539281 1.99996142 2.97306189]
Mean Squared Error: 0.09732995265403607
R-squared: 0.9976043742393531

Multivariate Linear Regression in Python

In this code:

  1. We generate artificial facts with two independent variables, X1 and X2, and a goal variable, y.
  2. We create a layout matrix X with the aid of stacking X1 and X2 horizontally and upload a column of ones for the intercept term.
  3. We cut up the facts into training and testing sets.
  4. We calculate the coefficients of the usage of the normal equation for linear regression.
  5. We make predictions at the take a look at set and calculate Mean Squared Error (MSaE) and R-squared (R2)
  6. We visualize the statistics factors and the regression plane using a 3-D scatter plot with matplotlib.

This code performs Multivariate Linear Regression the use of numpy and visualizes the outcomes of the usage of matplotlib.

Understanding the Outcomes

  • Mean Squared Error (MSE): A decreased MSE indicates a better fit of the model to the facts. It measures the average squared distinction between predicted and actual values.
  • R-Squared (R2): R-squared measures the share of the variance within the dependent variable (goal) this is predictable from the independent variables. A better value shows a better suit, with 1.0 being an excellent suit.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA