コード例 #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))
コード例 #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()
コード例 #3
0
ファイル: LSTM_model.py プロジェクト: yangjim/AI-project
import tensorflow as tf
from preprocessing import DataProcessing
import pandas_datareader.data as pdr
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))
コード例 #4
0
import matplotlib.pyplot as plt
import quandl as quandl
import keras
from preprocessing import DataProcessing

# live data extraction
TICKER = "WGC/GOLD_DAILY_USD"
data = quandl.get("WGC/GOLD_DAILY_USD", authtoken="FUw5PPY7UX5AW-svaskC")
data = data[37:]  # before index 37, prices were not daily

# optimizable variables
PERCENTAGE_OF_DATA_FOR_TRAINING = 0.8
SEQ_LENGTH = 30
N_DAY_FORECAST = 7

data_processor = DataProcessing(
    data, PERCENTAGE_OF_DATA_FOR_TRAINING, N_DAY_FORECAST)
data_processor.generate_validation_set(SEQ_LENGTH)
data_processor.generate_training_set(SEQ_LENGTH)

# typically normalize by substracting average and dividing by standard deviation
# but we want to use the output and check against live trade data thus not entirely feasible
# hence simply divide by a constant to ensure weights in network do not become too large
NORMALIZATION_CONSTANT = 500

# "normalization" of training data
X_training = data_processor.X_training.reshape(
    len(data_processor.X_training), SEQ_LENGTH, 1) / NORMALIZATION_CONSTANT
Y_training = data_processor.Y_training / NORMALIZATION_CONSTANT

# "normalization" of validation data
X_validation = data_processor.X_validation.reshape(
コード例 #5
0
import get_prices as hist
import tensorflow as tf
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")