import os
os.environ['KERAS_BACKEND'] = 'theano'
os.environ['THEANO_FLAGS'] = 'floatX=float32,device=cpu'

import cloudpickle as pickle
import pipeline_invoke
import pandas as pd
import numpy as np
import keras
from keras.layers import Input, Dense
from keras.models import Model
from keras.models import save_model, load_model

from sklearn.preprocessing import StandardScaler, MinMaxScaler, Normalizer

if __name__ == '__main__':
    df = pd.read_csv("../input/training/training.csv")
    df["People per Television"] = pd.to_numeric(df["People per Television"],errors='coerce')
    df = df.dropna()

    x = df["People per Television"].values.reshape(-1,1).astype(np.float64)
    y = df["People per Physician"].values.reshape(-1,1).astype(np.float64)

    # min-max -1,1
    sc = MinMaxScaler(feature_range=(-1,1))

    x_ = sc.fit_transform(x)
    y_ = sc.fit_transform(y)

    inputs = Input(shape=(1,))
    preds = Dense(1,activation='linear')(inputs)

    model = Model(inputs=inputs,outputs=preds)
    sgd = keras.optimizers.SGD()
    model.compile(optimizer=sgd ,loss='mse')
    model.fit(x_,y_, batch_size=1, verbose=1, epochs=10, shuffle=False)

    save_model(model, 'state/keras_theano_linear_model_state.h5')

#    model_pkl_path = 'model.pkl' 

#    with open(model_pkl_path, 'wb') as fh:
#        pickle.dump(pipeline_invoke, fh)

[(17, 'pandas.read_csv', 'pd.read_csv', (['"""../input/training/training.csv"""'], {}), True, 'import pandas as pd\n'), (18, 'pandas.to_numeric', 'pd.to_numeric', (["df['People per Television']"], {'errors': '"""coerce"""'}), True, 'import pandas as pd\n'), (25, 'sklearn.preprocessing.MinMaxScaler', 'MinMaxScaler', ([], {'feature_range': '(-1, 1)'}), False, 'from sklearn.preprocessing import StandardScaler, MinMaxScaler, Normalizer\n'), (30, 'keras.layers.Input', 'Input', ([], {'shape': '(1,)'}), False, 'from keras.layers import Input, Dense\n'), (33, 'keras.models.Model', 'Model', ([], {'inputs': 'inputs', 'outputs': 'preds'}), False, 'from keras.models import Model\n'), (34, 'keras.optimizers.SGD', 'keras.optimizers.SGD', ([], {}), False, 'import keras\n'), (38, 'keras.models.save_model', 'save_model', (['model', '"""state/keras_theano_linear_model_state.h5"""'], {}), False, 'from keras.models import save_model, load_model\n'), (31, 'keras.layers.Dense', 'Dense', (['(1)'], {'activation': '"""linear"""'}), False, 'from keras.layers import Input, Dense\n')]