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]:
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]:
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)
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()