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, 9, 17, 54), 'pandas.read_csv', 'pd.read_csv', ({(17, 21, 17, 53): '"""../input/training/training.csv"""'}, {}), "('../input/training/training.csv')", True, 'import pandas as pd\n'), ((18, 34, 18, 92), 'pandas.to_numeric', 'pd.to_numeric', (), '', True, 'import pandas as pd\n'), ((25, 9, 25, 43), 'sklearn.preprocessing.MinMaxScaler', 'MinMaxScaler', (), '', False, 'from sklearn.preprocessing import StandardScaler, MinMaxScaler, Normalizer\n'), ((30, 13, 30, 30), 'keras.layers.Input', 'Input', (), '', False, 'from keras.layers import Input, Dense\n'), ((33, 12, 33, 46), 'keras.models.Model', 'Model', (), '', False, 'from keras.models import Model\n'), ((34, 10, 34, 32), 'keras.optimizers.SGD', 'keras.optimizers.SGD', ({}, {}), '()', False, 'import keras\n'), ((38, 4, 38, 65), 'keras.models.save_model', 'save_model', ({(38, 15, 38, 20): 'model', (38, 22, 38, 64): '"""state/keras_theano_linear_model_state.h5"""'}, {}), "(model, 'state/keras_theano_linear_model_state.h5')", False, 'from keras.models import save_model, load_model\n'), ((31, 12, 31, 40), 'keras.layers.Dense', 'Dense', (), '', False, 'from keras.layers import Input, Dense\n')]