# 如何从技术上成功预测比特币价格？

LSTM是一种特殊的RNN（递归神经网络），十分适合带有时间序列的问题。因此，在预测加密货币价格和股市方面LSTM十分流行。

• http://colah.github.io/posts/2015-08-Understanding-LSTMs/

• http://blog.echen.me/2017/05/30/exploring-lstms/

• https://github.com/neocortex/lstm-bitcoin-prediction

import jsonimport requestsimport pandas as pdendpoint = 'https://min-api.cryptocompare.com/data/histoday'res = requests.get(endpoint + '?fsym=BTC&tsym=USD&limit=2000')hist = pd.DataFrame(json.loads(res.content)['Data'])hist = hist.set_index('time')hist.index = pd.to_datetime(hist.index, unit='s')hist.head()

def train_test_split(df, test_size=0.1):    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_datadef line_plot(line1, line2, label1=None, label2=None, title=''):    fig, ax = plt.subplots(1, figsize=(16, 9))    ax.plot(line1, label=label1, linewidth=2)    ax.plot(line2, label=label2, linewidth=2)    ax.set_ylabel('price [USD]', fontsize=14)    ax.set_title(title, fontsize=18)    ax.legend(loc='best', fontsize=18)train, test = train_test_split(hist, test_size=0.1)line_plot(train.close, test.close, 'training', 'test', 'BTC')

def normalise_zero_base(df):    """ Normalise dataframe column-wise to reflect changes with        respect to first entry.    """    return df / df.iloc[0] - 1def extract_window_data(df, window=7, zero_base=True):    """ Convert dataframe to overlapping sequences/windows of        length window.    """    window_data = []    for idx in range(len(df) - window):        tmp = df[idx: (idx + window)].copy()        if zero_base:            tmp = normalise_zero_base(tmp)        window_data.append(tmp.values)    return np.array(window_data)def prepare_data(df, window=7, zero_base=True, test_size=0.1):    """ Prepare data for LSTM. """    # train test split    train_data, test_data = train_test_split(df, test_size)    # extract window data    X_train = extract_window_data(train_data, window, zero_base)    X_test = extract_window_data(test_data, window, zero_base)    # extract targets    y_train = train_data.close[window:].values    y_test = test_data.close[window:].values    if zero_base:        y_train = y_train / train_data.close[:-window].values - 1        y_test = y_test / test_data.close[:-window].values - 1    return train_data, test_data, X_train, X_test, y_train, y_testtrain, test, X_train, X_test, y_train, y_test = prepare_data(hist)

def build_lstm_model(input_data, output_size, neurons=20,                     activ_func='linear', dropout=0.25,                     loss='mae', optimizer='adam'):    model = Sequential()    model.add(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)    return modelmodel = build_lstm_model(X_train, output_size=1)history = model.fit(X_train, y_train, epochs=50, batch_size=4)

targets = test[target_col][window:]preds = model.predict(X_test).squeeze()# convert change predictions back to actual pricepreds = test.close.values[:-window] * (preds + 1)preds = pd.Series(index=targets.index, data=preds)n = 30line_plot(targets[-n:], preds[-n:], 'actual', 'prediction')

line_plot(targets[-n:][:-1], preds[-n:].shift(-1))

actual_returns = targets.pct_change()[1:]predicted_returns = preds.pct_change()[1:]

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(18, 9))# actual correlationcorr = np.corrcoef(actual_returns, predicted_returns)[0][1]ax1.scatter(actual_returns, predicted_returns, color='k')ax1.set_title('r = {:.2f}'.format(corr), fontsize=18)# shifted correlationshifted_actual = actual_returns[:-1]shifted_predicted = predicted_returns.shift(-1).dropna()corr = np.corrcoef(shifted_actual, shifted_predicted)[0][1]ax2.scatter(shifted_actual, shifted_predicted, color='k')ax2.set_title('r = {:.2f}'.format(corr));

征稿啦！

CSDN 公众号秉持着「与千万技术人共成长」理念，不仅以「极客头条」、「畅言」栏目在第一时间以技术人的独特视角描述技术人关心的行业焦点事件，更有「技术头条」专栏，深度解读行业内的热门技术与场景应用，让所有的开发者紧跟技术潮流，保持警醒的技术嗅觉，对行业趋势、技术有更为全面的认知。

————— 推荐阅读 —————

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120