<jupyter_start><jupyter_text>Linear Regression on the life expectancy of U.S. adults<jupyter_code>import pandas as pd
from sklearn.linear_model import LinearRegression as LR
import matplotlib.pyplot as plt
# load the data
flc = '/Users/pinqingkan/Desktop/DataScience/PythonDataScience/DAT210x-master/Module5/Datasets/'
fname = flc + 'life_expectancy.csv'
X = pd.read_csv(fname, sep = '\t')
X.describe()
# check the independency of the data entries
cor = X.corr()
print(cor)
plt.imshow(cor, cmap = 'bwr')
plt.colorbar()
plt.show()
# slice the data for linear regression: white male, life expectancy
indices = X.Year[X.Year < 1986].index
X_train = X.loc[indices, ['Year']]
Y_train = X.loc[indices, ['WhiteMale']]
# slice the data for linear regression: black female, life expectancy
Y_train2 = X.loc[indices, ['BlackFemale']]
# linear regression on the white male data
model = LR(fit_intercept = True)
model.fit(X_train, Y_train)
Y_WM = model.predict(X_train)
# view the results
plt.figure()
plt.scatter(X_train, Y_train, marker = 'o')
plt.plot(X_train, Y_WM, color = 'orange')
fttl = 'White Male - Score: ' + str(model.score(X_train, Y_train))
plt.title(fttl)
plt.show()
# test the prediction
iyear = 2014
year_WM = model.predict([[iyear]])
year = X.WhiteMale[X.Year == iyear].values
print('Est. Life ', iyear, ': ', year_WM[0][0])
print('Act. Life ', iyear, ': ', year[0])
# linear regression on the black female data
model = LR(fit_intercept = True)
model.fit(X_train, Y_train2)
Y_BF = model.predict(X_train)
# view the results
plt.figure()
plt.scatter(X_train, Y_train2, marker = 'o')
plt.plot(X_train, Y_BF, color = 'orange')
fttl = 'Black female - Score: ' + str(model.score(X_train, Y_train))
plt.title(fttl)
plt.show()
# test the prediction
iyear = 2014
year_BF = model.predict([[iyear]])
year = X.BlackFemale[X.Year == iyear].values
print('Est. Life ', iyear, ': ', year_BF[0][0])
print('Act. Life ', iyear, ': ', year[0])<jupyter_output>Est. Life  2014 :  88.6988993981
Act. Life  2014 :  78.4
{'fraction_non_alphanumeric': 0.15142428785607195, 'fraction_numerical': 0.0239880059970015, 'mean_word_length': 5.2367601246105915, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '596727', 'license': 'MIT', 'n_tokens_mistral': 801, 'n_tokens_neox': 732, 'n_words': 193, 'path': 'Lab5_3_Modeling_LinearRegression/assignment8.ipynb', 'repo_name': 'rongqingpin/Python_Data_Science_edX_DAT210x', 'size': '2001'}