import keras
# -*- encoding:utf-8 -*-
import numpy as np
from keras.models import Sequential
from keras.layers import Dense,Dropout
import random
from keras.models import load_model
from imblearn.over_sampling import RandomOverSampler


def read_data(path):
    train_x=[]
    train_y=[]
    lines = []
    with open(path) as f:
        for x in range(20000):
            lines.append(eval(f.readline().strip()))

    random.shuffle(lines)
    print('读取数据完毕')

    d=int(0.95*len(lines))

    size = len(lines[0])
    train_x=[s[:size - 2] for s in lines[0:d]]
    train_y=[s[size-1] for s in lines[0:d]]
    test_x=[s[:size - 2] for s in lines[d:]]
    test_y=[s[size-1] for s in lines[d:]]

    print('转换数据完毕')

    ros = RandomOverSampler(random_state=0)
    X_resampled, y_resampled = ros.fit_sample(np.array(train_x), np.array(train_y))

    print('数据重采样完毕')

    return X_resampled,y_resampled,np.array(test_x),np.array(test_y)

train_x,train_y,test_x,test_y=read_data("D:\\data\\quantization\\stock4.log")

model = Sequential()
model.add(Dense(units=425, input_dim=166,  activation='relu'))
model.add(Dense(units=325, activation='relu'))
model.add(Dense(units=325, activation='relu'))
model.add(Dropout(0.2))
# model.add(Dense(units=325, activation='relu'))
# model.add(Dropout(0.2))
# model.add(Dense(units=325, activation='relu'))
# model.add(Dropout(0.2))
# model.add(Dense(units=325, activation='relu'))
# model.add(Dropout(0.2))
# model.add(Dense(units=325, activation='relu'))
# model.add(Dropout(0.2))
# model.add(Dense(units=225, activation='relu'))
# model.add(Dense(units=225, activation='relu'))
# model.add(Dense(units=225, activation='relu'))
model.add(Dense(units=225, activation='relu'))
model.add(Dropout(0.2))
# model.add(Dense(units=225, activation='relu'))
model.add(Dense(units=125, activation='relu'))
model.add(Dropout(0.2))
# model.add(Dense(units=125, activation='relu'))
model.add(Dense(units=166, activation='relu'))
model.add(Dense(units=8, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer="adam",metrics=['accuracy'])

print("Starting training ")
h=model.fit(train_x, train_y, batch_size=64, epochs=26, shuffle=True)
score = model.evaluate(test_x, test_y)
print(score)
print('Test score:', score[0])
print('Test accuracy:', score[1])

path="model_seq.h5"
model.save(path)
model=None


model=load_model(path)
result=model.predict(test_x)
print(result)
print(test_y)