Linear Regression

Using linear regression to predict life expectancy from body mass index(BMI).


In [1]:
# Import necessary packages
from sklearn.linear_model import LinearRegression
import pandas as pd
import matplotlib.pyplot as plt

In [2]:
# Assign the data frame to this variable.
bmi_life_data = pd.read_csv('bmi_and_life_expectancy.csv')
x_values = bmi_life_data[['BMI']]
y_values = bmi_life_data[['Life expectancy']]

In [3]:
bmi_life_data.head()


Out[3]:
Country Life expectancy BMI
0 Afghanistan 52.8 20.62058
1 Albania 76.8 26.44657
2 Algeria 75.5 24.59620
3 Andorra 84.6 27.63048
4 Angola 56.7 22.25083

In [4]:
# Plot data
plt.scatter(x_values, y_values)
plt.show()



In [5]:
# Make and fit the linear regression model
bmi_life_model = LinearRegression()
bmi_life_model.fit(x_values, y_values)


Out[5]:
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)

In [6]:
# Make a prediction using the model
# Predict life expectancy for a BMI value of 21.07931
laos_life_exp = bmi_life_model.predict(21.07931)
print(laos_life_exp)


[[ 60.31564716]]

In [7]:
# plot data and prediction
plt.scatter(x_values, y_values, c='r')
plt.plot(x_values, bmi_life_model.predict(x_values))
plt.xlabel('BMI')
plt.ylabel('Life Expectancy')
plt.show()