Ejemplo n.º 1
0
def main():
    start = "2003-01-01"
    end = "2018-01-01"

    hist.get_stock_data("AAPL", start_date=start, end_date=end)
    process = DataProcessing("stock_prices.csv", 0.9)
    process.gen_test(10)
    process.gen_train(10)

    X_train = process.X_train / 200
    Y_train = process.Y_train / 200

    X_test = process.X_test / 200
    Y_test = process.Y_test / 200

    model = tf.keras.models.Sequential()
    model.add(tf.keras.layers.Dense(100, activation=tf.nn.relu))
    model.add(tf.keras.layers.Dense(100, activation=tf.nn.relu))
    model.add(tf.keras.layers.Dense(1, activation=tf.nn.relu))

    model.compile(optimizer="adam", loss="mean_squared_error")

    model.fit(X_train, Y_train, epochs=100)

    print(model.evaluate(X_test, Y_test))
Ejemplo n.º 2
0
def main():
    process = DataProcessing("sales.csv", 0.9)
    process.gen_test(10)
    process.gen_train(10)

    X_train = process.X_train.reshape((239, 10, 1)) / 200
    Y_train = process.Y_train / 200

    X_test = process.X_test.reshape(9, 10, 1) / 200
    Y_test = process.Y_test / 200

    model = tf.keras.Sequential()
    model.add(KL.LSTM(20, input_shape=(10, 1), return_sequences=True))
    model.add(KL.LSTM(20))
    model.add(KL.Dense(1, activation=tf.nn.relu))

    model.compile(optimizer="adam", loss="mean_squared_error")
    history = model.fit(X_train, Y_train, validation_data=(X_test, Y_test), epochs=50, verbose=0)
    print(model.evaluate(X_test, Y_test))

    data = pandas.read_csv('sales_with_header.csv', sep=',').sample(n=10)
    prices = data["Price"]
    X_predict = np.array(prices).reshape((1, 10, 1)) / 200
    print("Predictions" + str(model.predict(X_predict) * 200))

    # If instead of a full backtest, you just want to see how accurate the model is for a particular prediction, run this:
    # data = pdr.get_data_yahoo("AAPL", "2017-12-19", "2018-01-03")
    # stock = data["Adj Close"]
    # X_predict = np.array(stock).reshape((1, 10)) / 200
    # print(model.predict(X_predict)*200)

    pyplot.title('Loss / Mean Squared Error')
    pyplot.plot(history.history['loss'], label='train')
    pyplot.plot(history.history['val_loss'], label='test')
    pyplot.legend()
    pyplot.show()
Ejemplo n.º 3
0
import yfinance as fix
import matplotlib.pyplot as plt
import WXBizSendMsg

fix.pdr_override()

start = "2000-01-01"
end = "2019-08-22"

stock = "000001.SS"

hist.get_stock_data(stock, start_date=start, end_date=end)
process = DataProcessing("stock_prices.csv", 0.9)

process.gen_test(10)
process.gen_train(10)

X_train = process.X_train / np.array([process.value_max, process.volume_max
                                      ])  # 归一化, 包括Adj Close 和 Volume
Y_train = process.Y_train / process.value_max

X_test = process.X_test / np.array([process.value_max, process.volume_max])
Y_test = process.Y_test / process.value_max

model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(20, input_shape=(10, 2), return_sequences=True))
model.add(tf.keras.layers.LSTM(20))
model.add(tf.keras.layers.Dense(1))

model.compile(optimizer="adam", loss="mean_squared_error")
Ejemplo n.º 4
0
from preprocessing import DataProcessing
# import pandas_datareader.data as pdr if using the single test below
import pandas_datareader.data as pdr
import yfinance as fix
import numpy as np
#import matplotlib.pyplot as plt

fix.pdr_override()

start = "2003-01-01"
end = "2018-01-01"

hist.get_stock_data("AAPL", start_date=start, end_date=end)
process = DataProcessing("stock_prices.csv", 0.9)
process.gen_test(10)  #滑动窗口构建测试样本
process.gen_train(10)  #滑动窗口构建训练样本

X_train = process.X_train / 200  #归一化方法?是否比x' = (x-min/max-min)更靠谱?min=0,max=200
Y_train = process.Y_train / 200

X_test = process.X_test / 200
Y_test = process.Y_test / 200

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(32, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(64, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(1, activation=tf.nn.relu))

model.compile(optimizer="adam", loss="mean_squared_error")

model.fit(X_train, Y_train, epochs=100)