【发布时间】:2021-07-13 22:45:24
【问题描述】:
如果你可以将第27行的变量'epoch'的值从10改为300,并在今天00:00:00运行算法(例如),你会看到除了values之外的所有值在数据帧的最后一行将非常接近算法试图预测的值。 在实际案例中,数据框的最后一行将用于向市场发送买卖订单,但由于它们的值不会接近真实值,因此算法不会达到其目标。 此问题出现在示例算法的第 124 行,因为在 00:00:00,将使用的值 (y_test) 尚未达到峰值。 根据传递的内容(y_test),它将是正确的,但它不会是所需的值,因为为此我需要等待 24 小时,直到数据帧的最后一行变成倒数第二行(00:00:00第二天),因此算法实际上可以正确预测它们的值。 在数据框的新最后一行中遇到同样的问题。
#Import libraries and dependencies
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='3'
import pandas as pd
import requests
import json
import numpy as np
from keras.models import Sequential
from keras.layers import LSTM,Dropout,Dense,Activation
import keras
from sklearn.metrics import mean_absolute_error
#Configure dataframe visualization
pd.set_option('display.precision',8)
#Configure some parameters to be used later
symbol='BTCUSDT'
test_size=0.2
window_len=5
lstm_neurons=100
dropout=0.2
output_size=2
activ_func='linear'
loss='mse'
optimizer='adam'
metrics=['mae','accuracy']
epochs=10
batch_size=1
verbose=1
shuffle=True
validation_split=0.25
#Collect exchange data
def collect_data():
endpoint='https://api.binance.com/api/v3/klines?symbol='+symbol+'&interval=1d&limit=5000'
res=requests.get(endpoint)
#Organize exchange data
l=[]
for r in json.loads(res.content):
k=[]
k.append(int(str(r[0])[0:10]))
for i in r[1:]:
k.append(float(i))
l.append(k)
#Create dataframe with exchange data
hist=pd.DataFrame(l,columns=['Time','Open','High','Low','Close','Volume','Closetime','Quotevolume','Trades','Takerbuyquotevolume','Takerbuyassetvolume','Ignore'])
hist=hist.drop(['Ignore'],axis=1)
#Index data by time
hist=hist.set_index('Time')
#Convert the date time string into a date time object
hist.index=pd.to_datetime(hist.index,unit='s')
return hist
#Select the columns to be predicted with the neural network
def select_target_columns():
target_col=['Low','High']
return target_col
#Split data into 80% for training and 20% for testing
def train_test_split(df):
split_row=len(df)-int(test_size*len(df))
train_data=df.iloc[:split_row]
test_data=df.iloc[split_row:]
return train_data,test_data
#Normalize data, change the values of numeric columns in the dataset to a common scale
def normalise_min_max(df):
return(df-df.min())/(df.max()-df.min())
#Extract data from windows
def extract_window_data(df):
tmp=df.copy()
tmp=normalise_min_max(tmp)
window_data=tmp.values
return np.array(window_data)
#Prepare data in format to be later fed into the neural network, 80% for training and 20% for testing
def prepare_data(df,target_col):
train_data,test_data=train_test_split(df)
X0=train_data.drop(target_col,axis=1)
X1=test_data.drop(target_col,axis=1)
X_train=extract_window_data(X0)
X_test=extract_window_data(X1)
X_train=np.reshape(X_train,(X_train.shape[0],1,X_train.shape[1]))
X_test=np.reshape(X_test,(X_test.shape[0],1,X_test.shape[1]))
y_train=train_data[target_col].values
y_test=test_data[target_col].values
y_train=y_train/y_train
y_test=y_test/y_test
return test_data,X_train,X_test,y_train,y_test
#Build sequential model, to stack all layers (input, hidden and output).
#The neural network is composed of an LSTM layer followed by a 20% exclusion layer and a dense layer with linear activation function.
#Completed the model using Adam as the optimizer and mean squared error as the loss function.
def build_lstm_model(input_data):
model=Sequential()
model.add(LSTM(lstm_neurons,input_shape=(input_data.shape[1],input_data.shape[2])))
model.add(Dropout(dropout))
model.add(Dense(units=output_size))
model.add(Activation(activ_func))
model.compile(loss=loss,optimizer=optimizer,metrics=metrics,run_eagerly=False)
return model
#Train model using x_train inputs and y_train labels
def train_model(model,X_train,y_train,X_test,y_test):
print('\nNeural Network Training\n')
history=model.fit(X_train,y_train,epochs=epochs,batch_size=batch_size,verbose=verbose,shuffle=shuffle,validation_split=validation_split)
return model
#Measure the average magnitude of errors in a set of predictions, without regard to their direction.
#Is the test sample mean of the absolute differences between actual and predicted observations, where all individual differences are of equal weight.
def mae(model,X_test,y_test):
preds=model.predict(X_test).squeeze()
mean_absolute_error(preds,y_test)
return preds
#Graphically represent actual and forecast prices
def graph(test,target_col,preds):
targets=test[target_col]
preds=test[target_col].values*preds
preds=pd.DataFrame(index=targets.index,data=preds)
return targets,preds
#Create a new dataframe
def create_new_dataframe(df,preds):
new_df=pd.DataFrame({
'Low':df['Low'],
'Buy':preds[0],
'High':df['High'],
'Sell':preds[1],
})
for i in new_df.index:
new_df['Buy'][i]='{:.2f}'.format(new_df['Buy'][i])
new_df['Sell'][i]='{:.2f}'.format(new_df['Sell'][i])
return new_df
#Call all functions
while True:
hist=collect_data()
target_col=select_target_columns()
train,test=train_test_split(hist)
test,X_train,X_test,y_train,y_test=prepare_data(hist,target_col)
model=build_lstm_model(X_train)
model=train_model(model,X_train,y_train,X_test,y_test)
preds=mae(model,X_test,y_test)
targets,preds=graph(test,target_col,preds)
new_df=create_new_dataframe(hist,preds)
print('\n')
print(new_df)
【问题讨论】:
-
对我来说没有意义的是你的问题。太混乱了尽量清楚一点。
-
@Abhishek Prajapat 我想通过将 X_test 乘以 y_preds 来获得最终结果。 y_preds 不做 y_test。
-
所以让我把它弄清楚。 1)您对代码没有任何问题。 2)问题似乎是关于“如何实施......”。 3) 你希望你的 y_pred 是与 y_test 的百分比差异。
-
x_test 和 y_test 之间的距离或百分比差异没有任何意义,您需要在此处寻求帮助之前考虑这一点。因为我们只接受编程问题,而你不是。
-
再一次,这毫无意义,你从哪里得到这个?是什么让你认为这个 y_preds 与 y_test 相乘?这不是计算神经网络预测的方式。您甚至没有考虑到由于维度非常不同,这些乘法毫无意义。
标签: python tensorflow keras deep-learning lstm