In this article, I am going to walk you through how we can train a model that will help us predict car prices with Machine Learning using PyTorch. The dataset Iâ€™m going to use here to predict car prices is tabular data with the prices of different cars regarding the other variables, the dataset has 258 rows and 9 columns, the variable we want to predict is the selling price of the cars.

## What is PyTorch?

PyTorch is a library in Python which provides tools to build deep learning models. What python does for programming PyTorch does for deep learning. Python is a very flexible language for programming and just like python, the PyTorch library provides flexible tools for deep learning. If you are learning deep learning or looking to start with it, then the knowledge of PyTorch will help you a lot in creating your deep learning models.

**Also, Read â€“ Maths for Machine Learning.**

## Predict Car Prices using PyTorch

Now, letâ€™s start with the task of machine learning to predict car prices using PyTorch. I will start by importing all the necessary libraries that we need for this task. The dataset I use in this task can be easily downloaded from **here**:

Now, letâ€™s read the data:

You can see what the data looks like, but before using it we need to customize it, sort the arrows and remove the columns that donâ€™t help the prediction, here we drop the car names, and to do this customization, we use the following function:

In this function above as we see it needs a word to use as a random string to sort data randomly, I used my name as a random string. After that we can use the custom dataset, for simplicity we can create variables containing the number of rows, columns and variables containing the numeric, categorical or output columns:

input_cols = ["Year","Present_Price","Kms_Driven","Owner"] categorical_cols = ["Fuel_Type","Seller_Type","Transmission"] output_cols = ["Selling_Price"]

## Data Preparation

As stated at the beginning of the article, I will be using PyTorch to predict car prices using machine learning, so to use the data for training we need to convert it from dataframe to PyTorch Tensors, the first step is to convert to NumPy arrays:

The above function converts the input and output columns to NumPy arrays, to check can display the result and as you can see how the data is turned into arrays. Now having these arrays, we can convert them to PyTorch tensors, and use those tensors to create a variable dataset that contains them:

## Creating PyTorch Model

Now, I am going to create a linear regressing model using PyTorch to predict car prices:

In this above function, I used the nn.Linear function which will allow us to use linear regression so now we can calculate the predictions and the loss with the F.l1_loss function can see the weight parameter one bias, with this model we will get the predictions, but will still have to undergo training.

## Training Model to Predict Car Prices

Now we need to assess the loss and see how much is, and after doing the training, see how much the loss decreases with training:

**{'val_loss': 2300.039306640625}**

Epoch [20], val_loss: 1692.0131 Epoch [40], val_loss: 1119.7253 Epoch [60], val_loss: 638.9708 Epoch [80], val_loss: 357.3529 Epoch [90], val_loss: 317.1693

**Epoch [20], val_loss: 7.9774**

As you can see, for evaluation and fit model functions are used, to do training we use optimization functions, in this case specifically SGD optimization, using train loader calculate the loss and gradients, to optimize it afterwards and evaluate the result of each iteration to see the loss.

## Using the Model to Predict Car Prices

Finally, we need to test the model with specific data, to predict it is necessary to use the input which will be the input values â€‹â€‹that we see in the dataset, and the model is the Cars model that we do, for the passing in the model is necessary to flatten, so with all this, predict the selling prices:

Input: tensor([1.9565e+03, 1.7800e+00, 4.0000e+03, 0.0000e+00]) Target: tensor([1.7985]) Prediction: tensor([1.4945])

As you can see, the predictions are very close to the expected target, not accurate but are similar to expected. With this now can test different results and see how good the model is:

Input: tensor([1.9555e+03, 8.4000e+00, 1.2000e+04, 0.0000e+00]) Target: tensor([6.9760]) Prediction: tensor([-0.4069])

I hope you liked this article on how to predict car prices with Machine Learning by using the Linear Regression model trained using PyTorch. Feel free to ask your valuable questions in the comments section below.

**Also, Read â€“ Common Python Interview Questions.**