from sklearn.externals import joblib
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
from sklearn.neural_network import MLPRegressor
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
from sklearn.model_selection import learning_curve
from sklearn.model_selection import ShuffleSplit

# For further use, use this line to import trained model
gs = joblib.load('model/mlmodel.pkl')

df = pd.read_csv("../dataset/data_036.csv")

# Choose features
X = df.drop(["dIStrain", "dX", "dR", "Time", "TStrain", "EStrain"], axis=1)
# Choose targets
y = df.drop(["IStrain", "TStrain", "X", "R", "Time", "Stress",
             "EStrain"], axis=1)

Xtime = df.drop(["dIStrain", "dX", "dR", "IStrain", "TStrain",
                 "X", "R", "Stress"], axis=1)

scaler_x = preprocessing.StandardScaler()
scaler_y = preprocessing.StandardScaler()

scaler_x.fit(X)
scaler_y.fit(y)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.3,
                                                    random_state=42)

X_train = scaler_x.transform(X_train)
X_test = scaler_x.transform(X_test)
y_train = scaler_y.transform(y_train)
y_test = scaler_y.transform(y_test)

X = scaler_x.transform(X)
y = scaler_y.transform(y)


def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None,
                        n_jobs=1, train_sizes=np.linspace(.1, 1.0, 5)):
    """
    Generate a simple plot of the test and training learning curve.

    Parameters
    ----------
    estimator : object type that implements the "fit" and "predict" methods
        An object of that type which is cloned for each validation.

    title : string
        Title for the chart.

    X : array-like, shape (n_samples, n_features)
        Training vector, where n_samples is the number of samples and
        n_features is the number of features.

    y : array-like, shape (n_samples) or (n_samples, n_features), optional
        Target relative to X for classification or regression;
        None for unsupervised learning.

    ylim : tuple, shape (ymin, ymax), optional
        Defines minimum and maximum yvalues plotted.

    cv : int, cross-validation generator or an iterable, optional
        Determines the cross-validation splitting strategy.
        Possible inputs for cv are:
          - None, to use the default 3-fold cross-validation,
          - integer, to specify the number of folds.
          - An object to be used as a cross-validation generator.
          - An iterable yielding train/test splits.

        For integer/None inputs, if ``y`` is binary or multiclass,
        :class:`StratifiedKFold` used. If the estimator is not a classifier
        or if ``y`` is neither binary nor multiclass, :class:`KFold` is used.

        Refer :ref:`User Guide <cross_validation>` for the various
        cross-validators that can be used here.

    n_jobs : integer, optional
        Number of jobs to run in parallel (default 1).
    """
    plt.figure()
    plt.title(title)
    if ylim is not None:
        plt.ylim(*ylim)
    plt.xlabel("Training examples")
    plt.ylabel("Score")
    train_sizes, train_scores, test_scores = learning_curve(
        estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
    train_scores_mean = np.mean(train_scores, axis=1)
    train_scores_std = np.std(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)
    test_scores_std = np.std(test_scores, axis=1)
    plt.grid()

    plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
                     train_scores_mean + train_scores_std, alpha=0.1,
                     color="r")
    plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
                     test_scores_mean + test_scores_std, alpha=0.1, color="g")
    plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
             label="Training score")
    plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
             label="Test score")

    plt.legend(loc="best")
    return plt


estimator = MLPRegressor(solver='lbfgs', hidden_layer_sizes=(4,),
                         activation='relu', learning_rate='adaptive',
                         alpha=1, random_state=1, verbose=True)

estimator1 = MLPRegressor(solver='sgd', hidden_layer_sizes=(4,),
                          activation='relu', learning_rate='adaptive',
                          alpha=1, random_state=1, verbose=True,
                          early_stopping=True)


# estimator = MLPRegressor(solver='lbfgs', hidden_layer_sizes=(15, 10, 20),
#                          activation='relu', learning_rate='invscaling',
#                          alpha=1, random_state=1)

estimator1.fit(X_train, y_train)


title = "Learning Curve"
# Cross validation with 100 iterations to get smoother mean test and train
# score curves, each time with 20% data randomly selected as a validation set.
cv = ShuffleSplit(n_splits=2, test_size=0.2, random_state=42)
plot_learning_curve(estimator, title, X, y, cv=cv, ylim=(0.0, 1.01), n_jobs=1)
plt.savefig('graphs/learning_curve_1d', bbox_inches='tight')
# plt.show()

from sklearn.externals import joblib
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
from sklearn.neural_network import MLPRegressor
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
from sklearn.model_selection import learning_curve
from sklearn.model_selection import ShuffleSplit

# For further use, use this line to import trained model
gs = joblib.load('model/mlmodel.pkl')

df = pd.read_csv("../dataset/data_036.csv")

# Choose features
X = df.drop(["dIStrain", "dX", "dR", "Time", "TStrain", "EStrain"], axis=1)
# Choose targets
y = df.drop(["IStrain", "TStrain", "X", "R", "Time", "Stress",
             "EStrain"], axis=1)

Xtime = df.drop(["dIStrain", "dX", "dR", "IStrain", "TStrain",
                 "X", "R", "Stress"], axis=1)

scaler_x = preprocessing.StandardScaler()
scaler_y = preprocessing.StandardScaler()

scaler_x.fit(X)
scaler_y.fit(y)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.3,
                                                    random_state=42)

X_train = scaler_x.transform(X_train)
X_test = scaler_x.transform(X_test)
y_train = scaler_y.transform(y_train)
y_test = scaler_y.transform(y_test)

X = scaler_x.transform(X)
y = scaler_y.transform(y)


def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None,
                        n_jobs=1, train_sizes=np.linspace(.1, 1.0, 5)):
    """
    Generate a simple plot of the test and training learning curve.

    Parameters
    ----------
    estimator : object type that implements the "fit" and "predict" methods
        An object of that type which is cloned for each validation.

    title : string
        Title for the chart.

    X : array-like, shape (n_samples, n_features)
        Training vector, where n_samples is the number of samples and
        n_features is the number of features.

    y : array-like, shape (n_samples) or (n_samples, n_features), optional
        Target relative to X for classification or regression;
        None for unsupervised learning.

    ylim : tuple, shape (ymin, ymax), optional
        Defines minimum and maximum yvalues plotted.

    cv : int, cross-validation generator or an iterable, optional
        Determines the cross-validation splitting strategy.
        Possible inputs for cv are:
          - None, to use the default 3-fold cross-validation,
          - integer, to specify the number of folds.
          - An object to be used as a cross-validation generator.
          - An iterable yielding train/test splits.

        For integer/None inputs, if ``y`` is binary or multiclass,
        :class:`StratifiedKFold` used. If the estimator is not a classifier
        or if ``y`` is neither binary nor multiclass, :class:`KFold` is used.

        Refer :ref:`User Guide <cross_validation>` for the various
        cross-validators that can be used here.

    n_jobs : integer, optional
        Number of jobs to run in parallel (default 1).
    """
    plt.figure()
    plt.title(title)
    if ylim is not None:
        plt.ylim(*ylim)
    plt.xlabel("Training examples")
    plt.ylabel("Score")
    train_sizes, train_scores, test_scores = learning_curve(
        estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
    train_scores_mean = np.mean(train_scores, axis=1)
    train_scores_std = np.std(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)
    test_scores_std = np.std(test_scores, axis=1)
    plt.grid()

    plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
                     train_scores_mean + train_scores_std, alpha=0.1,
                     color="r")
    plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
                     test_scores_mean + test_scores_std, alpha=0.1, color="g")
    plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
             label="Training score")
    plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
             label="Test score")

    plt.legend(loc="best")
    return plt


estimator = MLPRegressor(solver='lbfgs', hidden_layer_sizes=(4,),
                         activation='relu', learning_rate='adaptive',
                         alpha=1, random_state=1, verbose=True)

estimator1 = MLPRegressor(solver='sgd', hidden_layer_sizes=(4,),
                          activation='relu', learning_rate='adaptive',
                          alpha=1, random_state=1, verbose=True,
                          early_stopping=True)


# estimator = MLPRegressor(solver='lbfgs', hidden_layer_sizes=(15, 10, 20),
#                          activation='relu', learning_rate='invscaling',
#                          alpha=1, random_state=1)

estimator1.fit(X_train, y_train)


title = "Learning Curve"
# Cross validation with 100 iterations to get smoother mean test and train
# score curves, each time with 20% data randomly selected as a validation set.
cv = ShuffleSplit(n_splits=2, test_size=0.2, random_state=42)
plot_learning_curve(estimator, title, X, y, cv=cv, ylim=(0.0, 1.01), n_jobs=1)
plt.savefig('graphs/learning_curve_1d', bbox_inches='tight')
# plt.show()

# For further use, use this line to import trained model # Choose features # Choose targets Generate a simple plot of the test and training learning curve.      Parameters     ----------     estimator : object type that implements the "fit" and "predict" methods         An object of that type which is cloned for each validation.      title : string         Title for the chart.      X : array-like, shape (n_samples, n_features)         Training vector, where n_samples is the number of samples and         n_features is the number of features.      y : array-like, shape (n_samples) or (n_samples, n_features), optional         Target relative to X for classification or regression;         None for unsupervised learning.      ylim : tuple, shape (ymin, ymax), optional         Defines minimum and maximum yvalues plotted.      cv : int, cross-validation generator or an iterable, optional         Determines the cross-validation splitting strategy.         Possible inputs for cv are:           - None, to use the default 3-fold cross-validation,           - integer, to specify the number of folds.           - An object to be used as a cross-validation generator.           - An iterable yielding train/test splits.          For integer/None inputs, if ``y`` is binary or multiclass,         :class:`StratifiedKFold` used. If the estimator is not a classifier         or if ``y`` is neither binary nor multiclass, :class:`KFold` is used.          Refer :ref:`User Guide <cross_validation>` for the various         cross-validators that can be used here.      n_jobs : integer, optional         Number of jobs to run in parallel (default 1). # estimator = MLPRegressor(solver='lbfgs', hidden_layer_sizes=(15, 10, 20), #                          activation='relu', learning_rate='invscaling', #                          alpha=1, random_state=1) # Cross validation with 100 iterations to get smoother mean test and train # score curves, each time with 20% data randomly selected as a validation set. # plt.show()