<jupyter_start><jupyter_text>Predicting Life Expectancy using Machine Learning A typical Regression Machine Learning project leverages historical data to predict insights into the future. This problem statement is aimed at predicting Life Expectancy rate of a country given various features.Life expectancy is a statistical measure of the average time a human being is expected to live, Life expectancy depends on various factors: Regional variations, Economic Circumstances, Sex Differences, Mental Illnesses, Physical Illnesses, Education, Year of their birth and other demographic factors. This problem statement provides a way to predict average life expectancy of people living in a country when various factors such as year, GDP, education, alcohol intake of people in the country, expenditure on healthcare system and some specific disease related deaths that happened in the country are given.Skills required:Python,IBM Cloud,IBM Watson<jupyter_code>#Let's get started<jupyter_output><empty_output><jupyter_text>Installing watson studio client for model building<jupyter_code>!pip install watson_machine_learning_client
#import basic libraries for preprocessing and EDA
import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt
import seaborn as sns
pd.options.display.float_format='{:.5f}'.format
import warnings
import math

#import libraries for pipelining
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
from sklearn.compose import ColumnTransformer

#import libraries for train and test
from sklearn.model_selection import train_test_split

#import ExtraTreesRegressor for model fit and prediction
from sklearn.ensemble import ExtraTreesRegressor

#import libraries for accuracy and error calculation
from sklearn.metrics import mean_squared_error, r2_score

#import libraries for model building and deployment
from watson_machine_learning_client import WatsonMachineLearningAPIClient<jupyter_output><empty_output><jupyter_text>Importing Dataset<jupyter_code>import types
import types
import pandas as pd
from botocore.client import Config
import ibm_boto3

def __iter__(self): return 0

# @hidden_cell
# The following code accesses a file in your IBM Cloud Object Storage. It includes your credentials.
# You might want to remove those credentials before you share the notebook.
client_e4707596b92a4ff98466fe0798510979 = ibm_boto3.client(service_name='s3',
    ibm_api_key_id='<KEY>',
    ibm_auth_endpoint="https://iam.cloud.ibm.com/oidc/token",
    config=Config(signature_version='oauth'),
    endpoint_url='https://s3.eu-geo.objectstorage.service.networklayer.com')

body = client_e4707596b92a4ff98466fe0798510979.get_object(Bucket='mlpredictinglifeaccuracy-donotdelete-pr-wy1nywsmz65trf',Key='Life Expectancy Data.csv')['Body']
# add missing __iter__ method, so pandas accepts body as file-like object
if not hasattr(body, "__iter__"): body.__iter__ = types.MethodType( __iter__, body )

df=pd.read_csv(body)
df.head()<jupyter_output><empty_output><jupyter_text>Data Preprocessing<jupyter_code>df.columns
df=df.rename(columns={'Life expectancy ':'Life expectancy','Measles ':'Measles',' BMI ':'BMI','Diphtheria ':'Diphtheria',' HIV/AIDS':'HIV/AIDS',' thinness  1-19 years':'thinness  1-19 years',' thinness 5-9 years':'thinness 5-9 years'})
df.isnull().sum()
#fill the null values with mean to avoid test and train error
df=df.fillna(df.mean())
#checking for null values
df.isnull().sum()<jupyter_output><empty_output><jupyter_text>Exploratory data analysis<jupyter_code>#Ploting heat map to find similar values
df_kor=df.corr()
plt.figure(figsize=(10,10))
sns.heatmap(df_kor,vmin=-1,vmax=1,annot=True,linewidth=0.1)<jupyter_output><empty_output><jupyter_text>Train and Test<jupyter_code>#splitting the dataset
Y=df['Life expectancy']
X=df[df.columns.difference(['Life expectancy'])]
#check for numerical columns
df.select_dtypes(include=['int64', 'float64']).columns
df.select_dtypes(include=['object', 'bool']).columns
#identify the categorical values for column transform
categorical_features = ['Country', 'Status']
categorical_feature_mask = X.dtypes==object
categorical_features = X.columns[categorical_feature_mask].tolist()
#defne categorical pipeline
categorical_transformer = Pipeline(steps=[
    ('onehot', OneHotEncoder(handle_unknown='ignore')),])
#identify the numerical values for column transform 
numeric_features = ['Year','Adult Mortality','infant deaths','Alcohol','percentage expenditure', 'Hepatitis B',
       'Measles', 'BMI', 'under-five deaths ', 'Polio', 'Total expenditure','Diphtheria', 'HIV/AIDS', 'GDP', 'Population',
       'thinness  1-19 years', 'thinness 5-9 years','Income composition of resources', 'Schooling']
numeric_feature_mask = X.dtypes!=object
numeric_features = X.columns[numeric_feature_mask].tolist()
#define numeric pipeline
numeric_transformer = Pipeline(steps=[
    ('imputer', SimpleImputer(strategy='median')),
    ('scaler', StandardScaler()),
])
#pipelining using columntransform 
preprocessor = ColumnTransformer(
    transformers=[
        ('num', numeric_transformer, numeric_features),
        ('cat', categorical_transformer, categorical_features)
    ]
)

#define a regressor model using pipeline function
ExtraTreeRegressor = Pipeline([
     ('preprocessor', preprocessor),
     ('ExtraTreeRegressor', ExtraTreesRegressor(n_estimators=100, random_state=0))
])
#train-test split
X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.2)
#fit the training model
reg = ExtraTreeRegressor.fit(X_train, Y_train)
#predict the test data value
test_pred=reg.predict(X_test)
print(test_pred)
#error estimation and accuracy
print('Mean squared error: ',mean_squared_error(Y_test, test_pred))
print('R2 score: ',r2_score(Y_test, test_pred)*100)<jupyter_output>Mean squared error:  2.406955514357432
R2 score:  97.25509622378229<jupyter_text>Model Building and Deployment<jupyter_code>wml_credentials={ **********************}
client = WatsonMachineLearningAPIClient(wml_credentials)
print(client.service_instance.get_url())
model_props = {client.repository.ModelMetaNames.AUTHOR_NAME: "********", 
               client.repository.ModelMetaNames.AUTHOR_EMAIL: "**********", 
               client.repository.ModelMetaNames.NAME: "LifeExpectancyPrediction"}
#STORE THE MACHINE LEARNING MODEL
model_artifact=client.repository.store_model(ExtraTreeRegressor, meta_props=model_props)
#GET MODEL UID
model_uid = client.repository.get_model_uid(model_artifact)
#DEPLOY THE MODEL
create_deployment = client.deployments.create(model_uid, name="Predicting Life Expectancy using Machine Learning")
#GET SCORING END-POINT URL
scoring_endpoint = client.deployments.get_scoring_url(create_deployment)
print(scoring_endpoint)

#removed my scoring end point url because of privacy so getting this error.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09860371383330935, 'fraction_numerical': 0.018857060601698575, 'mean_word_length': 7.330935251798561, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 2, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '464833', 'license': 'Apache-2.0', 'n_tokens_mistral': 2202, 'n_tokens_neox': 2066, 'n_words': 566, 'path': 'Predicting Life Expectancy using Maching Learning.ipynb', 'repo_name': 'Samarasimhareddy369/llSPS-INT-1983-Predicting-Life-Expectancy-using-Machine-Learning', 'size': '6986'}