# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
# Reading the train data-set
df_train = pd.read_csv('../input/tabular-playground-series-jul-2021/train.csv')
df_train.head()
# Reading the test data-set

df_test = pd.read_csv('../input/tabular-playground-series-jul-2021/test.csv')
df_test.head()
# Reading the dimension
df_train.shape
# Reading the train data-set
df_train.info()
#Convert to date-time format
df_train['date_time'] = pd.to_datetime(df_train['date_time'])
df_train.info()
"""
From the above output, We found that all the above variables are continuous except date_time column

"""
df_train.describe()
df_train_features = ['deg_C','relative_humidity','sensor_1','sensor_2','sensor_3','sensor_4','sensor_5']
X = df_train[df_train_features]
# Choosing the target variables to be predicted
y = df_train[['target_carbon_monoxide','target_benzene','target_nitrogen_oxides']]
"""
### <u>Building the Model</u>
"""
from sklearn.tree import DecisionTreeRegressor
# Define model. Specify a number for random_state to ensure same results each run
first_model = DecisionTreeRegressor(random_state=1)
# Fit model
first_model.fit(X, y)
print("Making predictions for the following 3 target variables:")
print(X.head())
print("The predictions are")
print(pd.DataFrame(first_model.predict(X.head())))
"""
### <u>Model Validation</u>
"""
from sklearn.metrics import mean_absolute_error

predicted_target_values_train = first_model.predict(X)
mean_absolute_error(y, predicted_target_values_train)
from sklearn.model_selection import train_test_split
# split data into training and validation data, for both features and target
# The split is based on a random number generator. Supplying a numeric value to
# the random_state argument guarantees we get the same split every time we
# run this script.
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state = 0)
# Define model
first_model = DecisionTreeRegressor()
# Fit model
first_model.fit(train_X, train_y)

# get predicted prices on validation data
val_predictions = first_model.predict(val_X)
print(mean_absolute_error(val_y, val_predictions))
"""
### <u>Parameter-Tuning and check Overfitting</u>
"""

# We can use utility function to help compare MAE scores from different values for max_leaf_nodes:
def get_mae(max_leaf_nodes, train_X, val_X, train_y, val_y):
    model = DecisionTreeRegressor(max_leaf_nodes=max_leaf_nodes, random_state=0)
    model.fit(train_X, train_y)
    preds_val = model.predict(val_X)
    mae = mean_absolute_error(val_y, preds_val)
    return(mae)
# compare MAE with differing values of max_leaf_nodes
for max_leaf_nodes in [5, 50, 500, 5000]:
    my_mae = get_mae(max_leaf_nodes, train_X, val_X, train_y, val_y)
    print("Max leaf nodes: %d  \t\t Mean Absolute Error:  %d" %(max_leaf_nodes, my_mae))
"""
The Error in prediction of max_leaf nodes falls abruptly when the max_leaf node is 50. Beyond that the error change is not very large.So the optimum number of leaf_nodes would be 50.
"""
"""
### <u>Applying the model on Test-set</u>
"""
df_test.head()
# Reading the dimension
df_test.shape
 # Reading the train data-set
df_test.info()
# Converting to date-format
df_test['date_time'] = pd.to_datetime(df_test['date_time'])
# Re-inspect Summary
df_test.info()
df_test_features = ['deg_C','relative_humidity','sensor_1','sensor_2','sensor_3','sensor_4','sensor_5']
X_test = df_test[df_test_features]
Final_model = DecisionTreeRegressor(max_leaf_nodes=50,random_state=1)
Final_model.fit(X, y)
predictions = Final_model.predict(X_test)
predictions
output = pd.DataFrame(predictions,
                 columns=['target_carbon_monoxide', 'target_benzene','target_nitrogen_oxides'])
output.head()
output.to_csv('my_submission.csv', index=False)
print("Your submission was successfully saved!")
"""
# References:

Kaggle - Microcourses
"""