Multiple Linear Regression (MLR) is a statistical technique used to represent the relationship between one dependent variable and two or more independent variables. MLR is similar to simple linear regression, but it incorporates multiple independent variables rather than just one.
Regression | Independent variables | Dependent variables |
---|---|---|
Simple linear regression | 1 | 1 |
Multiple linear regression | >= 2 | 1 |
Suppose we have three independent variables x1
, x2
and x3
, and one dependent variable y
:
x1 | -2 | -1 | 0 | 1 | 2 | 3 | 4 |
x2 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
x3 | -5 | -4 | -3 | -2 | -1 | 0 | 1 |
y | -6 | 0 | 6 | 12 | 18 | 24 | 30 |
The relationship between these variables is expressed by the formula y = 2 * x1 + 3 * x2 + x3
.
This tutorial demonstrates how to build and train a model that predicts the value of y
based on given values of x1
, x2
, and x3
using PyTorch.
To train the model, we first create tensors for x1s
, x2s
, x3s
, and ys
. Since the model expects all features in a single input, we merge x1s
, x2s
, and x3s
into one tensor using torch.cat
.
The model is simple, containing just one linear layer that maps three input values to a single output. We define the loss using mean squared error (MSE) and update the model parameters with stochastic gradient descent (SGD). The training runs for 400 epochs.
import torch
x1s = torch.Tensor([[-2.0], [-1.0], [0.0], [1.0], [2.0], [3.0], [4.0]])
x2s = torch.Tensor([[1.0], [2.0], [3.0], [4.0], [5.0], [6.0], [7.0]])
x3s = torch.Tensor([[-5.0], [-4.0], [-3.0], [-2.0], [-1.0], [0.0], [1.0]])
x123s = torch.cat((x1s, x2s, x3s), dim=1)
ys = torch.Tensor([[-6.0], [0.0], [6.0], [12.0], [18.0], [24.0], [30.0]])
model = torch.nn.Sequential(
torch.nn.Linear(3, 1)
)
loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
epochs = 400
for epoch in range(epochs):
optimizer.zero_grad()
ys_pred = model(x123s)
loss = loss_fn(ys_pred, ys)
loss.backward()
optimizer.step()
x1 = 16.0
x2 = 19.0
x3 = 12.0
x = torch.Tensor([[x1, x2, x3]])
y = model(x).item()
print(y)
After training, we use the model to predict y
for the given values of x1
, x2
, and x3
. The model outputs y = 101.07343
, which we can verify by calculating:
y = 2 * x1 + 3 * x2 + x3 = 2 * 16 + 3 * 19 + 12 = 101
Leave a Comment
Cancel reply