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:
1. Load the data
2. Build a linear regression model
3. Predict using the model
read_csv
to load the data into a dataframe.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:
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()
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()
In [ ]:
# TODO: Write the prediction function
def predict(m, b, bmi):
pass
In [ ]: