import numpy as np
import matplotlib as mpl
%matplotlib inline
import time

import pandas as pd
import seaborn as sns

from mnist import MNIST  # public package for making arrays out of MINST data.

import sys
sys.path.append('../code/')

from ridge_regression import RidgeMulti
from hyperparameter_explorer import HyperparameterExplorer

from mnist_helpers import mnist_training, mnist_testing

import matplotlib.pyplot as plt
from pylab import rcParams
rcParams['figure.figsize'] = 4, 3

"""
Explanation: Question_1-1-3_Multiclass_Ridge
Janet Matsen
Code notes:
* Indivudal regressions are done by instinces of RidgeRegression, defined in rige_regression.py.
    * RidgeRegression gets some methods from ClassificationBase, defined in classification_base.py.
* The class HyperparameterExplorer in hyperparameter_explorer is used to tune hyperparameters on training data.
End of explanation
"""


train_X, train_y = mnist_training()
test_X, test_y = mnist_testing()

"""
Explanation: Prepare MNIST training data
End of explanation
"""


hyper_explorer = HyperparameterExplorer(X=train_X, y=train_y, 
                                        model=RidgeMulti, 
                                        validation_split=0.1, score_name = 'training RMSE', 
                                        use_prev_best_weights=False,
                                        test_X=test_X, test_y=test_y)

hyper_explorer.train_model(lam=1e10, verbose=False)

hyper_explorer.train_model(lam=1e+08, verbose=False)
hyper_explorer.train_model(lam=1e+07, verbose=False)

hyper_explorer.train_model(lam=1e+06, verbose=False)

hyper_explorer.train_model(lam=1e5, verbose=False)
hyper_explorer.train_model(lam=1e4, verbose=False)
hyper_explorer.train_model(lam=1e03, verbose=False)
hyper_explorer.train_model(lam=1e2, verbose=False)

hyper_explorer.train_model(lam=1e1, verbose=False)

hyper_explorer.train_model(lam=1e0, verbose=False)
hyper_explorer.train_model(lam=1e-1, verbose=False)
hyper_explorer.train_model(lam=1e-2, verbose=False)
hyper_explorer.train_model(lam=1e-3, verbose=False)
hyper_explorer.train_model(lam=1e-4, verbose=False)
hyper_explorer.train_model(lam=1e-5, verbose=False)

hyper_explorer.summary

hyper_explorer.plot_fits()

t = time.localtime(time.time())

hyper_explorer.plot_fits(filename = "Q-1-1-3_val_and_train_RMSE_{}-{}".format(t.tm_mon, t.tm_mday))

hyper_explorer.plot_fits(ylim=(.6,.7),
                         filename = "Q-1-1-3_val_and_train_RMSE_zoomed_in{}-{}".format(t.tm_mon, t.tm_mday))

hyper_explorer.best('score')

hyper_explorer.best('summary')

hyper_explorer.best('best score')

hyper_explorer.train_on_whole_training_set(lam=1e7)

hyper_explorer.final_model.results_row()

hyper_explorer.evaluate_test_data()

"""
Explanation: Explore hyperparameters before training model on all of the training data.
End of explanation
"""


