原题目如上,本文只是针对题目简单应用了LSTM、SVM,未做深入探究。
LSTM的Python代码如下:
# -*- coding:utf-8 -*-
import os
import time
import warnings
import numpy as np
import matplotlib.pyplot as plt
from numpy import newaxis
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
import pandas as pd
%matplotlib inline
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
warnings.filterwarnings("ignore")
#处理数据使之符合LSTM的要求+划分train和test
#interval 时间间隔(每隔1分钟取一次数据,或是隔5分钟取一次数据)
#start_time 所用数据集的起始时间
def load_data(seq_len, normalise_window, interval ,start_time , end_time):
f = open(r'D:\chenxinyi\信息系统项目实践\EURUSD1.csv')
# 将日期作为index,顺序排列,出现KeyError!!!!解决:在read_csv中加index_col实现同样效果
df = pd.read_csv(f, encoding='gbk', index_col=0)
# 按给定时间间隔提取数据
select_min = np.linspace(0, len(df) - 1, int(len(df) / interval))
df = df.iloc[select_min, :]
data = df.loc[start_time:end_time]
print(data.head())
# 将DataFrame 转化成array
data = data.values
sequence_length = seq_len + 1
result = []
for index in range(len(data) - sequence_length):#为了最后剩一个sequence长度的数据
result.append(data[index: index + sequence_length,3])
#数据归一化处理
# print(result.head())
if normalise_window:
result = normalise_windows(result)
result = np.array(result)
row = round(0.8 * result.shape[0])
train = result[:int(row), :]
np.random.shuffle(train)
x_train = train[:, :-1]#取出train中的每一行,但是对于列,最后的一列不取
y_train = train[:, -1] #此处指的是行列的划分取值(行,列)
x_test = result[int(row):, :-1]
y_test = result[i