Learn practical skills, build real-world projects, and advance your career

Linear Regression with PyTorch

How to implement linear regression in PyTorch to predict the price of the car based on car's attributes and company.

In this article we're going to use inforamtion like brand, size, doors, engine position, fuel type of the car to predict the price of the car.
This kind of model will be useful for car retailers to determine or estimate the price of the car. The dataset used for this problem is taken from: https://archive.ics.uci.edu/ml/datasets/Automobile where the description of the data could be found.

The focus of the article is to show how linear regression could be implement in PyTorch. This article does not focus on all the pre-processing, visualization or interpretations aspects required by the linear regression algorithm.

We will create a model with the following steps:

  1. Load and explore the dataset
  2. Prepare the dataset for training
  3. Create a linear regression model
  4. Train the model to fit the data
  5. Make predictions using the trained model
import torch
!pip install jovian --upgrade -q
import jovian
import torchvision
import torch.nn as nn
import pandas as pd
import matplotlib.pyplot as plt
import torch.nn.functional as F
from torchvision.datasets.utils import download_url
from torch.utils.data import DataLoader, TensorDataset, random_split
from pandas.api.types import is_numeric_dtype
import seaborn as sns
project_name='02-regression-blog' # will be used by jovian.commit

Step 1: Load and explore the data

To load the dataset into memory, we'll use the read_csv function from the pandas library. The data will be loaded as a Pandas dataframe.