手把手教你9步完成Python加密货币的价格预测

作者:CQITer小编 时间:2018-05-12 09:37

字号
有奖调研 | 1TB硬盘等你拿 AI+区块链的发展趋势及应用调研

手把手教你9步完成Python加密货币的价格预测

作 者 | 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 

首先,要对数据进行归一化处理。

手把手教你9步完成Python加密货币的价格预测

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)  

责任编辑:CQITer新闻报料:400-888-8888   本站原创,未经授权不得转载
继续阅读
热新闻
推荐
关于我们联系我们免责声明隐私政策 友情链接