Predicting the life expectancy using TensorFlow

Hey all! In this tutorial, we will be predicting the life expectancy using TensorFlow. Here we will pre-process the data, do some data analysis and then predict the average life expectancy of a person using the TensorFlow regression model when we are given the other factors that influence the life expectancy.

Start Python program to Predict life expectancy using TensorFlow

First, let us import the necessary libraries.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
%matplotlib inline


Now let us now read the file and put it into a DataFrame. To access the dataset:

df = pd.read_csv("C:/Users/madhumitha/Downloads/LifeExpectancyData.csv")


Let us also see how many null values are there in this dataset.



Country                           0
Year                              0
Status                            0
LifeExpectancy                   10
AdultMortality                   10
InfantDeaths                      0
Alcohol                         194
PercentageExpenditure             0
HepatitisB                      553
Measles                           0
BMI                              34
underfivedeaths                   0
Polio                            19
TotalExpenditure                226
Diphtheria                       19
HIV/AIDS                          0
GDP                             448
Population                      652
thinness1-19years                34
thinness5-9years                 34
IncomeCompositionOfResources    167
Schooling                       163
dtype: int64

We have so many null values in the dataset that needs to be imputed or dropped. But dropping the rows with null values is not a good option because it leads to the loss in the data. Thus we will try some methodology to impute the values.


Let’s impute the null values in certain columns with the corresponding column’s mean value.

df["LifeExpectancy"].fillna(df["LifeExpectancy"].mean(), inplace=True)
df["AdultMortality"].fillna(df["AdultMortality"].mean(), inplace=True)
df.dropna(subset = ["HepatitisB"], inplace=True)


import seaborn as sns

Adult mortality rate- box plot


This way, the box plots can be plotted for the other column variables to easily find the outliers and remove them from the dataset.


Let us also remove the identified outliers in the dataset in order to improve the accuracy of the model.



Imputing the null values

Further, let us do more of data cleaning. Here we are going to impute the null values with the respective column mean and also we are going to drop the “year” column because it is not going to influence our results much.

df["Population"].fillna(df["Population"].mean(), inplace=True)
df.dropna(subset = ["Alcohol"], inplace=True)
df["GDP"].fillna(df["GDP"].mean(), inplace=True)
df["BMI"].fillna(df["BMI"].mean(), inplace=True)
df["TotalExpenditure"].fillna(df["TotalExpenditure"].mean(), inplace=True)
df["Polio"].fillna(df["Polio"].mean(), inplace=True)
df["Diphtheria"].fillna(df["Diphtheria"].mean(), inplace=True)


For better accuracy, let us adopt a different method of imputing the values in the “Schooling” column which is nothing but “imputing using categorical mean”.

def impute_schooling(cols):
    if pd.isnull(s):
        if l<= 40:
            return 8.0
        elif 40<l<=44:
            return 7.5
        elif 44<l<50:
            return 8.1
        elif 50<l<=60:
            return 8.2
        elif 60<l<=70:
            return 10.5
        elif 70<l<=80:
            return 13.4
        elif l>80:
            return 16.5
        return s


Here we have actually considered the “Schooling” column and we have imputed it depending on it’s mean value in the life expectancy column. We are simply substituting the null values depending on their values in the “Life Expectancy” columns. We have chosen the “Life Expectancy” column values to impute because the two column variables are in strong correlation with one another in the heatmap.

Let us have a look at the heatmap also!

Heatmap to show the correlation of the features


Now let us drop the very few remaining rows which still have the null values in some columns.


Now that we have arrived at a cleaned dataset.


We will now convert the strings into either 0 or 1 because there are only two categories in that column.

for ind in df.index:
    if df['Status'][ind] == 'Developed':
        df['Status'][ind] = 1
        df['Status'][ind]= 0


Let us drop the “country” and “Life Expectancy” column from the x_train set and have only the “Life Expectancy” column in y_train set.

sample = df.drop(['Country','LifeExpectancy'], axis = 1)
x = sample[['Year','Status', 'AdultMortality', 'InfantDeaths', 'Alcohol',
       'PercentageExpenditure', 'HepatitisB', 'Measles', 'BMI',
       'underfivedeaths', 'Polio', 'TotalExpenditure', 'Diphtheria',
       'HIV/AIDS', 'GDP', 'Population', 'thinness1-19years',
       'thinness5-9years', 'IncomeCompositionOfResources', 'Schooling']]
y = df['LifeExpectancy']

Just to know the shape of x and y to have a better idea, let us see the output.

(1497, 20)


Building the model

Let us normalize the values to get better accuracy.

sample.columnsdf = sample.iloc[:,:]
sample_norm = (sample - sample.mean()) / sample.std()
y_norm = (y - y.mean()) / y.std()

Let us also write a function that can convert the values after prediction back from normalized form.

y_mean = df['LifeExpectancy'].mean()
y_std = df['LifeExpectancy'].std()

def convert_label_value(pred):
    return int(pred * y_std + y_mean)


Let us have a look at how the x and y values are like after normalization

X = sample_norm.iloc[:,:]


    Year	Status	AdultMortality	InfantDeaths	Alcohol	PercentageExpenditure	HepatitisB	Measles	BMI	underfivedeaths	Polio	TotalExpenditure	Diphtheria	HIV/AIDS	GDP	Population	thinness1-19years	thinness5-9years	IncomeCompositionOfResources	Schooling
0	1.738179	-0.428108	0.832587	0.417127	-1.11651	-0.351539	-1.464045	-0.082418	-0.995570	0.407639	-3.933121	0.948385	-1.297232	-0.323326	-0.432582	0.784003	2.838166	2.825139	-0.864523	-0.756515
1	1.490459	-0.428108	0.900689	0.440653	-1.11651	-0.350274	-1.673835	-0.154376	-1.021040	0.433666	-1.356216	0.957084	-1.475067	-0.323326	-0.430132	-0.369733	2.906474	2.870293	-0.881089	-0.792789
2	1.242738	-0.428108	0.875151	0.464178	-1.11651	-0.350446	-1.533975	-0.161116	-1.046510	0.459692	-1.157992	0.935335	-1.356511	-0.323326	-0.428491	0.714769	2.952012	2.915447	-0.914221	-0.829063
3	0.995018	-0.428108	0.909202	0.499467	-1.11651	-0.347647	-1.324185	0.095086	-1.071980	0.494393	-0.910213	1.104980	-1.178676	-0.323326	-0.425198	-0.253376	2.997551	2.983178	-0.952874	-0.865337
4	0.747298	-0.428108	0.934740	0.522992	-1.11651	-0.387717	-1.254255	0.119652	-1.092356	0.529095	-0.860657	0.822238	-1.119398	-0.323326	-0.477446	-0.278183	3.065858	3.028332	-1.002572	-0.974158


Y = y_norm

The “y” value will be as follows:

0   -0.548800
1   -1.152274
2   -1.152274
3   -1.199605
4   -1.235103
Name: LifeExpectancy, dtype: float64


Now, we will split the train and test set and see the shape of the train and test data.

X_train, X_test, y_train, y_test = train_test_split(X_arr, Y_arr, test_size = 0.25, shuffle = True, random_state=0)

X_train = np.asarray(X_train).astype(np.float32)
X_test = np.asarray(X_test).astype(np.float32)
y_train = np.asarray(y_train).astype(np.float32)
y_test = np.asarray(y_test).astype(np.float32)

print('X_train shape: ', X_train.shape)
print('y_train shape: ', y_train.shape)
print('X_test shape: ', X_test.shape)
print('y_test shape: ', y_test.shape)


X_train shape:  (1122, 20)
y_train shape:  (1122,)
X_test shape:  (375, 20)
y_test shape:  (375,


Now let us build the model inside a get_model() and call the function.

def get_model():
    model = Sequential([
        Dense(15, input_shape = (20,), activation = 'relu'),
        Dense(10, activation = 'relu'),
        #Dense(5, activation = 'relu'),

    return model

model = get_model()


Model: "sequential_122"
Layer (type)                 Output Shape              Param #   
dense_431 (Dense)            (None, 15)                315       
dense_432 (Dense)            (None, 10)                160       
dense_433 (Dense)            (None, 1)                 11        
Total params: 486
Trainable params: 486
Non-trainable params: 0


Now we will fit the model. Let us take just 15 epochs so that the model does not overfit.

early_stopping = EarlyStopping(monitor='val_loss', patience = 5)

model = get_model()

preds_on_untrained = model.predict(X_test)

history =
    X_train, y_train,
    validation_data = (X_test, y_test),
    epochs = 15,
    callbacks = [early_stopping]


Let us write a function to plot the model history:

def plot_history(history):
    loss_list = [s for s in history.history.keys() if 'loss' in s and 'val' not in s]
    val_loss_list = [s for s in history.history.keys() if 'loss' in s and 'val' in s]
    acc_list = [s for s in history.history.keys() if 'acc' in s and 'val' not in s]
    val_acc_list = [s for s in history.history.keys() if 'acc' in s and 'val' in s]
    if len(loss_list) == 0:
        print('Loss is missing in history')
    ## As loss always exists
    epochs = range(1,len(history.history[loss_list[0]]) + 1)
    ## Loss
    for l in loss_list:
        plt.plot(epochs, history.history[l], 'b', label='Training loss (' + str(str(format(history.history[l][-1],'.5f'))+')'))
    for l in val_loss_list:
        plt.plot(epochs, history.history[l], 'g', label='Validation loss (' + str(str(format(history.history[l][-1],'.5f'))+')'))

For every epoch, the model loss is appended to an empty list and at last it is plotted.


For better understanding of how the model is fitted, let us see how the training and validation loss has decreased with increase in epochs.


The graph looks like the following:

training los validation loss chart

model history


But how can we know that our model is better than an untrained model?  The answer is : by plotting the untrained model’s prediction and our trained model’s predictions.


def plot_predictions(preds, y_test):
    plt.figure(figsize=(8, 8))
    plt.plot(preds, y_test, 'ro')
    plt.xlim([-0.5, 0.5])
    plt.ylim([-0.5, 0.5])
    plt.plot([-0.5, 0.5], [-0.5, 0.5], 'b--')

def compare_predictions(preds1, preds2, y_test):
    plt.figure(figsize=(8, 8))
    plt.plot(preds1, y_test, 'ro', label='Untrained Model')
    plt.plot(preds2, y_test, 'go', label='Trained Model')
    y_min = min(min(y_test), min(preds1), min(preds2))
    y_max = max(max(y_test), max(preds1), max(preds2))
    plt.xlim([y_min, y_max])
    plt.ylim([y_min, y_max])
    plt.plot([y_min, y_max], [y_min, y_max], 'b--')

We have defined the functions to plot the predictions of an untrained model and a trained model against the ground truth (y_test).


preds_on_trained = model.predict(X_test)
compare_predictions(preds_on_untrained, preds_on_trained, y_test)


untrained model vs trained model graph

The green points represent the trained model’s predictions and the red points represent the untrained model’s predictions.


Now let us take any random row from the dataset and give the other column values (after normalisation) as input to get the life expectancy prediction from the model.

example =pd.DataFrame(sample.iloc[27,:])
example_norm= (example - example.mean()) / example.std()


Let us reshape the values of that row’s data.

example_norm= np.asarray(example_norm).astype(np.float32)
value = np.array(example_norm).reshape(1,20)


array([[0.21137413]], dtype=float32)


Now let us convert the value into the un normalised form.

ans = [convert_label_value(hi)]




Let us print the value of 27th row and see what it’s value of life expectancy is.



LifeExpectancy    73
Name: 27, dtype: object


Our predictions are close!! Thus we have predicted the average life expectancy of a person, given the other influencing factors for life expectancy using TensorFlow regression.

Leave a Reply

Your email address will not be published. Required fields are marked *