手把手教你9步完成Python加密货币的价格预测
作者:CQITer小编 时间:2018-05-12 09:37

作 者 | Siraj Raval
翻 译 | 糖竹子、狗小白、邓子稷
预测加密货币价格其实很简单,用Python+Keras,再来一个循环神经网络(确切说是双向LSTM),只需要9步就可以了!比特币以太坊价格预测都不在话下。
这9个步骤是:
数据处理
建模
训练模型
测试模型
分析价格变化
分析价格百分比变化
比较预测值和实际数据
计算模型评估指标
结合在一起:可视化
数据处理
导入Keras、Scikit learn的metrics、numpy、pandas、matplotlib这些我们需要的库。
## Keras for deep learning
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.layers import Bidirectional
from keras.models import Sequential
## Scikit learn for mapping metrics
from sklearn.metrics import mean_squared_error
#for logging
import time
##matrix math
import numpy as np
import math
##plotting
import matplotlib.pyplot as plt
##data processing
import pandas as pd
首先,要对数据进行归一化处理。

def load_data(filename, sequence_length):
"""
Loads the bitcoin data
Arguments:
filename -- A string that represents where the .csv file can be located
sequence_length -- An integer of how many days should be looked at in a row
Returns:
X_train -- A tensor of shape (2400, 49, 35) that will be inputed into the model to train it
Y_train -- A tensor of shape (2400,) that will be inputed into the model to train it
X_test -- A tensor of shape (267, 49, 35) that will be used to test the model's proficiency
Y_test -- A tensor of shape (267,) that will be used to check the model's predictions
Y_daybefore -- A tensor of shape (267,) that represents the price of bitcoin the day before each Y_test value
unnormalized_bases -- A tensor of shape (267,) that will be used to get the true prices from the normalized ones
window_size -- An integer that represents how many days of X values the model can look at at once
"""
#Read the data file
raw_data = pd.read_csv(filename, dtype = float).values
#Change all zeros to the number before the zero occurs
for x in range(0, raw_data.shape[0]):
for y in range(0, raw_data.shape[1]):
if(raw_data[x][y] == 0):
raw_data[x][y] = raw_data[x-1][y]
#Convert the file to a list
data = raw_data.tolist()
#Convert the data to a 3D array (a x b x c)



