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)

[((441, 486), 'pandas.read_csv', 'pd.read_csv', (['"""../input/training/training.csv"""'], {}), "('../input/training/training.csv')\n", (452, 486), True, 'import pandas as pd\n'), ((521, 580), 'pandas.to_numeric', 'pd.to_numeric', (["df['People per Television']"], {'errors': '"""coerce"""'}), "(df['People per Television'], errors='coerce')\n", (534, 580), True, 'import pandas as pd\n'), ((782, 817), 'sklearn.preprocessing.MinMaxScaler', 'MinMaxScaler', ([], {'feature_range': '(-1, 1)'}), '(feature_range=(-1, 1))\n', (794, 817), False, 'from sklearn.preprocessing import StandardScaler, MinMaxScaler, Normalizer\n'), ((890, 907), 'keras.layers.Input', 'Input', ([], {'shape': '(1,)'}), '(shape=(1,))\n', (895, 907), False, 'from keras.layers import Input, Dense\n'), ((970, 1005), 'keras.models.Model', 'Model', ([], {'inputs': 'inputs', 'outputs': 'preds'}), '(inputs=inputs, outputs=preds)\n', (975, 1005), False, 'from keras.models import Model\n'), ((1015, 1037), 'keras.optimizers.SGD', 'keras.optimizers.SGD', ([], {}), '()\n', (1035, 1037), False, 'import keras\n'), ((1160, 1221), 'keras.models.save_model', 'save_model', (['model', '"""state/keras_theano_linear_model_state.h5"""'], {}), "(model, 'state/keras_theano_linear_model_state.h5')\n", (1170, 1221), False, 'from keras.models import save_model, load_model\n'), ((920, 949), 'keras.layers.Dense', 'Dense', (['(1)'], {'activation': '"""linear"""'}), "(1, activation='linear')\n", (925, 949), False, 'from keras.layers import Input, Dense\n')]