# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tqdm import tqdm
import pickle
import time
import seaborn as sns

pd.set_option('display.max_colwidth', 3000)
pd.set_option("display.max_rows", 100000)
# -

from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
from sklearn.linear_model import ElasticNet
from sklearn.linear_model import Lasso
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor

from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import r2_score


def model_evaluator(y_truth, y_preds):
    mse = mean_squared_error(y_truth, y_preds)
    rmse = np.sqrt(mse)
    mae = mean_absolute_error(y_truth, y_preds)
    r2 = r2_score(y_truth, y_preds)
    
    print("Mean Squared Error: {:.3f}".format(mse))
    print("Root Mean Squared Error: {:.3f}".format(rmse))
    print("Mean Absolute Error: {:.3f}".format(mae))
    print("R-Squared Score: {:.3f}".format(r2))
    
    return mse, rmse, mae, r2


# +
def save_model(model, filepath):
    with open(filepath, "wb") as f:
        pickle.dump(model, f)
        
def load_model(filepath):
    with open(filepath, "rb") as f:
        return pickle.load(f)


# -

print("Reading Features Data from Disk...")
_dir = r"E:\ARSH\NEU\Fall 2021\DS 5500\Project\Data\Features_From_CNN"
test_filepath = r"{}\df_test_densenet161_hurricane_features.csv".format(_dir)
train_filepath = r"{}\df_train_densenet161_hurricane_features.csv".format(_dir)
train_df = pd.read_csv(train_filepath, index_col=0)
test_df = pd.read_csv(test_filepath, index_col=0)
print("Done!")

print(train_df.shape, test_df.shape)

train_df.head()

test_df.head()

feature_size = 2352 - 1 # number of features extracted from densenet161 - 1
X_train, y_train = train_df.iloc[:, :feature_size], train_df["wind_speed"]
X_test, y_test = test_df.iloc[:, :feature_size], test_df["wind_speed"]

# # 1. Linear Regression

print("Fitting Linear Regression Model...")
start = time.time()
linearRegression = LinearRegression()
linearRegression.fit(X_train, y_train)
print("Done!")
print("Time Taken: {:.5f}".format(time.time()-start))

model_name = "linear_regression"
filepath = r"{}\Trained Models\{}.sav".format(_dir, model_name)
save_model(linearRegression, filepath)

print("Evaluating Model on Train Set...")
y_preds = linearRegression.predict(X_train)
print("Computing Metrics...\n")
mse, rmse, mae, r2 = model_evaluator(y_train, y_preds)
print("")
print("Done!")

print("Evaluating Model on Test Set...")
y_preds = linearRegression.predict(X_test)
print("Computing Metrics...\n")
mse, rmse, mae, r2 = model_evaluator(y_test, y_preds)
print("")
print("Done!")

# # 2. Rigde Regression

print("Fitting Ridge Regression Model...")
start = time.time()
ridge = Ridge()
ridge.fit(X_train, y_train)
print("Done!")
print("Time Taken: {:.5f}".format(time.time()-start))

model_name = "ridge"
filepath = r"{}\Trained Models\{}.sav".format(_dir, model_name)
save_model(ridge, filepath)

print("Evaluating Model on Train Set...")
y_preds = ridge.predict(X_train)
print("Computing Metrics...\n")
mse, rmse, mae, r2 = model_evaluator(y_train, y_preds)
print("")
print("Done!")

print("Evaluating Model on Test Set...")
y_preds = ridge.predict(X_test)
print("Computing Metrics...\n")
mse, rmse, mae, r2 = model_evaluator(y_test, y_preds)
print("")
print("Done!")

# # 3. Lasso Regression

print("Fitting Lasso Regression Model...")
start = time.time()
lasso = Lasso()
lasso.fit(X_train, y_train)
print("Done!")
print("Time Taken: {:.5f}".format(time.time()-start))

model_name = "lasso"
filepath = r"{}\Trained Models\{}.sav".format(_dir, model_name)
save_model(lasso, filepath)

print("Evaluating Model on Train Set...")
y_preds = lasso.predict(X_train)
print("Computing Metrics...\n")
mse, rmse, mae, r2 = model_evaluator(y_train, y_preds)
print("")
print("Done!")

print("Evaluating Model on Test Set...")
y_preds = lasso.predict(X_test)
print("Computing Metrics...\n")
mse, rmse, mae, r2 = model_evaluator(y_test, y_preds)
print("")
print("Done!")

# # 4. ElasticNet Regression

print("Fitting ElasticNet Regression Model...")
start = time.time()
elastic_net = ElasticNet()
elastic_net.fit(X_train, y_train)
print("Done!")
print("Time Taken: {:.5f}".format(time.time()-start))

model_name = "elastic_net"
filepath = r"{}\Trained Models\{}.sav".format(_dir, model_name)
save_model(elastic_net, filepath)

print("Evaluating Model on Train Set...")
y_preds = elastic_net.predict(X_train)
print("Computing Metrics...\n")
mse, rmse, mae, r2 = model_evaluator(y_train, y_preds)
print("")
print("Done!")

print("Evaluating Model on Test Set...")
y_preds = elastic_net.predict(X_test)
print("Computing Metrics...\n")
mse, rmse, mae, r2 = model_evaluator(y_test, y_preds)
print("")
print("Done!")

# # 5. Decision Tree

print("Fitting Decision Tree Regressor...")
start = time.time()
dtree = DecisionTreeRegressor(min_samples_split=150, min_samples_leaf=100, max_depth=50, random_state=7)
dtree.fit(X_train, y_train)
print("Done!")
print("Time Taken: {:.5f}".format(time.time()-start))

model_name = "dtree"
filepath = r"{}\Trained Models\{}.sav".format(_dir, model_name)
save_model(dtree, filepath)

print("Evaluating Model on Train Set...")
y_preds = dtree.predict(X_train)
print("Computing Metrics...\n")
mse, rmse, mae, r2 = model_evaluator(y_train, y_preds)
print("")
print("Done!")

print("Evaluating Model on Test Set...")
y_preds = dtree.predict(X_test)
print("Computing Metrics...\n")
mse, rmse, mae, r2 = model_evaluator(y_test, y_preds)
print("")
print("Done!")

# # 6. Random Forest

print("Fitting Randon Forest Regressor...")
start = time.time()
random_forest = RandomForestRegressor(min_samples_split=150, min_samples_leaf=50,
                                      n_estimators=300, max_depth=50, random_state=7, verbose=1)
random_forest.fit(X_train, y_train)
print("Done!")
print("Time Taken: {:.5f}".format(time.time()-start))

model_name = "random_forest"
filepath = r"{}\Trained Models\{}.sav".format(_dir, model_name)
save_model(random_forest, filepath)

print("Evaluating Model on Train Set...")
y_preds = random_forest.predict(X_train)
print("Computing Metrics...\n")
mse, rmse, mae, r2 = model_evaluator(y_train, y_preds)
print("")
print("Done!")

print("Evaluating Model on Test Set...")
y_preds = random_forest.predict(X_test)
print("Computing Metrics...\n")
mse, rmse, mae, r2 = model_evaluator(y_test, y_preds)
print("")
print("Done!")



from sklearn.ensemble import BaggingRegressor

print("Fitting Linear BaggingRegressor Model...")
start = time.time()
base_estimator = LinearRegression()
linear_bagging_regressor = BaggingRegressor(base_estimator=base_estimator, random_state=7, max_samples=1.0,
                                            max_features=1.0, n_estimators=2)
linear_bagging_regressor.fit(X_train, y_train)
print("Done!")
print("Time Taken: {:.5f}".format(time.time()-start))



StarcoderdataJupyterScriptsDedupFiltered