Insurance cost prediction using linear regression
In this assignment we're going to use information like a person's age, sex, BMI, no. of children and smoking habit to predict the price of yearly medical bills. This kind of model is useful for insurance companies to determine the yearly insurance premium for a person. The dataset for this problem is taken from: https://www.kaggle.com/mirichoi0218/insurance
We will create a model with the following steps:
- Download and explore the dataset
- Prepare the dataset for training
- Create a linear regression model
- Train the model to fit the data
- Make predictions using the trained model
This assignment builds upon the concepts from the first 2 lectures. It will help to review these Jupyter notebooks:
- PyTorch basics: https://jovian.ml/aakashns/01-pytorch-basics
- Linear Regression: https://jovian.ml/aakashns/02-linear-regression
- Logistic Regression: https://jovian.ml/aakashns/03-logistic-regression
- Linear regression (minimal): https://jovian.ml/aakashns/housing-linear-minimal
- Logistic regression (minimal): https://jovian.ml/aakashns/mnist-logistic-minimal
As you go through this notebook, you will find a ??? in certain places. Your job is to replace the ??? with appropriate code or values, to ensure that the notebook runs properly end-to-end . In some cases, you'll be required to choose some hyperparameters (learning rate, batch size etc.). Try to experiment with the hypeparameters to get the lowest loss.
# Uncomment and run the commands below if imports fail
#!conda install numpy torchvision cpuonly -c pytorch -y
#!pip install matplotlib
!pip install jovian --upgrade
Requirement already up-to-date: jovian in /home/gufopc/anaconda3/envs/Pytorch/lib/python3.7/site-packages (0.2.16)
Requirement already satisfied, skipping upgrade: click in /home/gufopc/anaconda3/envs/Pytorch/lib/python3.7/site-packages (from jovian) (7.1.2)
Requirement already satisfied, skipping upgrade: requests in /home/gufopc/anaconda3/envs/Pytorch/lib/python3.7/site-packages (from jovian) (2.23.0)
Requirement already satisfied, skipping upgrade: uuid in /home/gufopc/anaconda3/envs/Pytorch/lib/python3.7/site-packages (from jovian) (1.30)
Requirement already satisfied, skipping upgrade: pyyaml in /home/gufopc/anaconda3/envs/Pytorch/lib/python3.7/site-packages (from jovian) (5.3.1)
Requirement already satisfied, skipping upgrade: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /home/gufopc/anaconda3/envs/Pytorch/lib/python3.7/site-packages (from requests->jovian) (1.25.9)
Requirement already satisfied, skipping upgrade: chardet<4,>=3.0.2 in /home/gufopc/anaconda3/envs/Pytorch/lib/python3.7/site-packages (from requests->jovian) (3.0.4)
Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in /home/gufopc/anaconda3/envs/Pytorch/lib/python3.7/site-packages (from requests->jovian) (2020.4.5.1)
Requirement already satisfied, skipping upgrade: idna<3,>=2.5 in /home/gufopc/anaconda3/envs/Pytorch/lib/python3.7/site-packages (from requests->jovian) (2.9)
import torch
import jovian
import seaborn as sns
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
project_name='02-insurance-linear-regression' # will be used by jovian.commit
Step 1: Download and explore the data
Let us begin by downloading the data. We'll use the download_url
function from PyTorch to get the data as a CSV (comma-separated values) file.