CoolFace
Apppublic

MHVZ08/predicting-life-expectancy

sourceHugging Facemitupdated 4mo agoView on Hugging Face
0likes
model.py45 linesDownload Raw Back to root
1#importing libraries
2import numpy as np
3import pandas as pd
4from sklearn.model_selection import train_test_split
5from sklearn.ensemble import RandomForestRegressor
6import pickle
7
8#importing dataset
9le_df = pd.read_csv('Life Expectancy Data.csv')
10
11#dropping unwanted columns
12le_df.drop(['Year', 'Status'], axis=1, inplace=True)
13
14#renaming columns
15le_df.rename(columns={'Life expectancy':'Life Expectancy', 'infant deaths':'Infant Deaths',
16                      'percentage expenditure':'Percentage Expenditure',
17                      'under-five deaths':'Under-Five Deaths',
18                     'thinness  1-19 years':'Thinness 10-19 years',
19                      'thinness 5-9 years':'Thinness 5-9 years'}, inplace=True)
20
21#le_df.isnull().head()
22#total = le_df.isnull().sum()
23#total
24numeric_data = le_df.select_dtypes(include=np.number)
25numeric_col = numeric_data.columns
26for i in numeric_col:
27    mean = le_df[i].mean()
28    le_df[i].fillna(mean,inplace = True)
29le_df = le_df.groupby('Country').mean()
30#le_df
31
32#splitting into dependant & independant variables
33life = le_df['Life Expectancy']
34features = le_df.drop(['Life Expectancy'], axis=1)
35
36#splitting into train & test
37X_train, X_test, y_train, y_test = train_test_split(features, life, test_size = 0.2, random_state = 0)
38
39#training
40ran_forest_reg = RandomForestRegressor()
41ran_forest_reg.fit(X_train, y_train)
42
43#Saving model to disk
44pickle.dump(ran_forest_reg, open('model.pkl','wb'))
45