"""
Welcome to the IEEE PES BDC Datathon 2020. In this competition, your task is to predict [Global Horizontal Irradiance (GHI)](https://www.3tier.com/en/support/solar-prospecting-tools/what-global-horizontal-irradiance-solar-prospecting/#:~:text=Global%20Horizontal%20Irradiance%20(GHI)%20is,Diffuse%20Horizontal%20Irradiance%20(DIF).). This notebook provides a basic data processing and model training pipeline. Feel free to `Copy and Edit` this notebook.  
"""
"""
It is a known fact that the GHI score depends on several parameters such as precipitation,	atmospheric pressure, relative humidity, air temperature, wind direction and wind speed. But their proper relation is unknown. Our objective is to find a relation between **GHI** and these parameters. 

The `train.csv` file comes with an ID for each **GHI** recording events and 6 other parameters. You have to prepare an algorithm that can predict GHI with minimum error possible. You'll then predict over an test set for which the GHI scores are not provided. Your predictions will be compared against actual scores. The competition metric is RMS score. Lower RMS score implies better and more accurate prediction. 
"""
"""
# Loading Necessary Libraries
"""
# 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)
from sklearn.ensemble import RandomForestClassifier as RFC
from sklearn.linear_model import LinearRegression as LR
from sklearn.neural_network import MLPRegressor as MLPR
"""
# Loading Data
"""
data_dir = '../input/ieee-pes-bdc-datathon-year-2020'
df = pd.read_csv(f'{data_dir}/train.csv')
test_df = pd.read_csv(f'{data_dir}/test.csv')
df.head(5)
"""
# Train-Validation Split
"""
"""
Train validation split is required for finding the optimum solution which will yield better performance on unseen test data. For now, I'm not using
any data for validation. In the cell below, you may select a suitable value for `pct` to split your dataset into train and test. `pct=1.0` means 100% data is being used for training and `0%` is kept for validation.  
"""
data_len = len(df)
pct = 1.0 # change it to 0.8~0.9
train_len = int(1.0*data_len)
train_df = df[:train_len]
val_df = df[train_len:]
"""
# Creating Train, Validation and Test data
"""
X_train = train_df.drop(['ID', 'global_horizontal_irradiance'], axis=1).values.reshape(-1, 6)
y_train = train_df['global_horizontal_irradiance'].values.reshape(len(train_df))
X_val = val_df.drop(['ID', 'global_horizontal_irradiance'], axis=1).values.reshape(-1, 6)
y_val = val_df['global_horizontal_irradiance'].values.reshape(len(val_df))
X_test = test_df.drop(['ID'], axis=1).values.reshape(-1, 6)
test_ID = test_df['ID'].values.reshape(len(test_df))
"""
## Linear Regression
"""
"""
For our first experiment, we are assuming that **GHI** has a linear dependency over the given set of parameters. For a better understanding about how linear regression works, look at this Diagram:![](https://miro.medium.com/max/1600/1*xz_haBuJRSI2DaveNk-3gw.gif) Courtesy: [Introduction To Linear Regression and Polynomial Regression](https://towardsdatascience.com/introduction-to-linear-regression-and-polynomial-regression-f8adc96f31cb) 


We are going to use `Sklearn` library for building a linear regression model.
"""
reg = LR(normalize=True)
reg.fit(X_train, y_train)
reg.coef_
"""
# Prediction over test set
"""
preds = reg.predict(X_test)
"""
# Neural Network
"""
"""
Neural Network is a widely used algorithm these days. It helps us to find complex underlying features within data and differentiate between different data types based on those features. The __gif__ below shows an example of how neural networks work. ![](https://blog.floydhub.com/content/images/2018/06/playground.gif) You can play with this tool at tensorflow [playground](https://playground.tensorflow.org/).

We are going to use `Sklearn`'s built in `Multi Layer Perceptron Regressor` for training our model.
"""
regr = MLPR(random_state=1, hidden_layer_sizes = (32, 8, 2), max_iter=5, validation_fraction=0.1, learning_rate_init=0.02, verbose=True)
regr.fit(X_train, y_train)
"""
# Generating Prediction
"""
preds = regr.predict(X_test)
preds = [0 if p<0 else p for p in preds] # Since GHI can not be less than 0
"""
# Creating Submission File
"""
zippedList =  list(zip(test_ID, preds))
submission = pd.DataFrame(zippedList, columns = ['ID','global_horizontal_irradiance'])
submission.to_csv('submission.csv', index=False)
submission.head(5)
"""
You can now press the `Save Version` button and then select `Save & Run All (Commit)`. It will take around 15~20 mins for this notebook to run. After that, select `submission.csv` from the `Output` and then submit.
"""