Lab: Predicting Life Expectancy from BMI in Countries using Linear Regression

In this lab, you'll be working with data on the average life expectancy at birth and the average BMI for males across the world. The data comes from Gapminder.

The data file can be found in the "bmi_and_life_expectancy.csv" file. It includes three columns, containing the following data:

  • Country – The country the person was born in.
  • Life expectancy – The average life expectancy at birth for a person in that country.
  • BMI – The mean BMI of males in that country.

You'll need to complete each of the following steps:

1. Load the data

2. Build a linear regression model

3. Predict using the model

1. Load and plot the data

  • The data is in the file called "bmi_and_life_expectancy.csv".
  • Use pandas read_csv to load the data into a dataframe.
  • Assign the dataframe to the variable bmi_life_data.

In [ ]:
import numpy as np
import pandas as pd

In [ ]:
# TODO: Load the data in Pandas

bmi_life_data = None

# Print the data
bmi_life_data

Some helper functions:

  • One to plot the data.
  • One to plot any line, given the slope $m$ and the y-intercept $b$.

In [ ]:
import matplotlib.pyplot as plt

x = np.array(bmi_life_data[["BMI"]])
y = np.array(bmi_life_data["Life expectancy"])

def draw_data(x, y):
    for i in range(len(x)):
        plt.scatter(x[i], y[i], color='blue', edgecolor='k')
    plt.xlabel('BMI')
    plt.ylabel('Life expectancy')

def display(m, b, color='g'):
    r = np.arange(min(x), max(x), 0.1)
    plt.plot(r, m*r+b, color)

Plotting the data


In [ ]:
draw_data(x, y)
plt.show()

2. Build a Linear Regression Model

  • Create a regression model and assign the weights to the array bmi_life_model.
  • Fit the model to the data.

In [ ]:
epochs = 1000
learning_rate = 0.001

# TODO: Finish the code for this function
def linear_regression(x, y):
    # Initialize m and b
    m=1
    b=0
    # TODO: Use the square trick to update the weights
    # and run it for a number of epochs
    return(m, b)
m, b = linear_regression(x, y)

In [ ]:
linear_regression(x,y)
draw_data(x, y)
display(m[0], b[0])
plt.show()

3. Predict using the Model

  • Predict using a BMI of 21.07931 and assign it to the variable laos_life_exp.

In [ ]:
# TODO: Write the prediction function
def predict(m, b, bmi):
    pass

In [ ]: