<jupyter_start><jupyter_text>Data<jupyter_code>!wget https://raw.githubusercontent.com/miguelgfierro/sciblog_support/master/Time_Series_Forecasting_of_Stock_Price/data/MSFT_2012_2017.csv
DATA = os.path.join('./', 'MSFT_2012_2017.csv')
EPOCHS = 10
TEST_SIZE = 0.3
TIME_AHEAD = 1 #prediction step
BATCH_SIZE = 1
UNITS = 10
SEQUENCE = 10
df = pd.read_csv(DATA)
df = df.drop(['Adj Close', 'Volume'], axis=1)
print(df.shape)
df.head()
# helper functions

import matplotlib.pyplot as plt
from IPython.display import SVG, display

def plot_series(values, xlabel=None, ylabel=None, color='b', legend=None):
    xx = np.arange(1, len(values) + 1, 1)
    plt.plot(xx, values, color, label=legend)
    plt.legend(loc = 'upper left')
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.show()
    

def plot_series_prediction(true_values, train_predict, test_predict, time_ahead=1, title=None,
                           xlabel=None, ylabel=None, color=['green','red','blue'], legend=[None,None,None]):    
    TOOLS = 'pan,wheel_zoom,box_zoom,reset,save,box_select'
    #x axis
    xx = np.array(range(true_values.shape[0]))
    xx1 = np.array(range(time_ahead,len(train_predict)+time_ahead))
    xx2 = np.array(range(len(train_predict)+(time_ahead*2)+1,len(true_values)-1))
    
    #figure
    plt.plot(xx, true_values.squeeze())
    plt.plot(xx1, train_predict.squeeze())
    plt.plot(xx2, test_predict.squeeze())
    
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.legend(['true', 'training set', 'test predict'], loc='upper left')
    plt.show()

    
def to_1dimension(df, step_size):
    X, y = [], []
    for i in range(len(df)-step_size-1):
        data = df[i:(i+step_size), 0]
        X.append(data)
        y.append(df[i + step_size, 0])
    X, y = np.array(X), np.array(y)
    X = np.reshape(X, (X.shape[0], 1, X.shape[1]))
    return X, y
mean_price = df.mean(axis = 1)
print(mean_price.shape)
plot_series(mean_price, xlabel='Days', ylabel='Mean value of Microsoft Stock', color='b', legend='Mean price')
scaler = MinMaxScaler(feature_range=(0, 1)) #other typical scale values are -1,1
mean_price = scaler.fit_transform(np.reshape(mean_price.values, (len(mean_price),1)))
train, test = train_test_split(mean_price, test_size=TEST_SIZE, shuffle=False)
print(train.shape)
print(test.shape)
X_train, y_train = to_1dimension(train, TIME_AHEAD)
X_test, y_test = to_1dimension(test, TIME_AHEAD)
print(X_train.shape, y_train.shape)
print(X_test.shape, y_test.shape)
# (1054, 10, 1)-> (10,1)<jupyter_output>(1054, 1, 1) (1054,)
(451, 1, 1) (451,)<jupyter_text>LSTM<jupyter_code>def create_symbol(X, model_name='LSTM', units=10, activation='linear', time_ahead=1, dropout=True):
    # with tf.variable_scope("RNN"):
    if model_name == 'LSTM':
        if dropout:
            cell = tf.contrib.rnn.DropoutWrapper(
                              # tf.contrib.rnn.BasicLSTMCell(units, forget_bias=1.0), #LSTMCell(units),
                              tf.contrib.rnn.LSTMCell(units),
                              output_keep_prob=0.8)
        else:
            cell = tf.contrib.rnn.BasicLSTMCell(units, forget_bias=1.0) #LSTMCell(units)
        
        outputs, states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)

    elif model_name == 'GRU':
        cell = tf.contrib.rnn.GRUCell(units)
        outputs, states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)
    else:
        raise ValueError("Wrong model name")
    sym = tf.layers.dense(outputs[-1], 1, activation=None, name='output') #activation=None (linear activation)
    return sym
tf.reset_default_graph()

# Place-holders
X = tf.placeholder(tf.float32, shape=[None, 1, TIME_AHEAD])
y = tf.placeholder(tf.float32, shape=[None])

# with sess.as_default():
sym = create_symbol(X, model_name='GRU', units=UNITS, time_ahead=TIME_AHEAD, dropout=False)
loss = tf.reduce_mean(tf.squared_difference(sym, y)) #mse
optimizer = tf.train.AdamOptimizer()
model = optimizer.minimize(loss)

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

# training
for i in range(EPOCHS):
    ii = 0
    while(ii + BATCH_SIZE) <= len(X_train):
        X_batch = X_train[ii:ii+BATCH_SIZE,:,:]
        y_batch = y_train[ii:ii+BATCH_SIZE]
        sess.run(model, feed_dict={X:X_batch, y:y_batch})
        ii += BATCH_SIZE
    loss_train = sess.run(loss, feed_dict={X: X_batch, y: y_batch})
    print('Epoch {}/{}'.format(i+1, EPOCHS), ' Current loss: {}'.format(loss_train))

y_guess = np.zeros(y_test.shape[0], dtype=np.float32)
ii = 0
while(ii + BATCH_SIZE) <= len(X_test):
    X_batch = X_test[ii:ii+BATCH_SIZE,:,:]
    output = sess.run(sym, feed_dict={X:X_batch})
    y_guess[ii:ii+BATCH_SIZE] = output
    ii += BATCH_SIZE
y_test_inv = scaler.inverse_transform([y_test])
pred_test = scaler.inverse_transform([y_guess])
score = math.sqrt(mean_squared_error(y_test_inv, pred_test))
print('Test RMSE: %.2f' % (score))
mean_price_inv = scaler.inverse_transform(mean_price)
train_set = mean_price_inv[:X_train.shape[0]]
plot_series_prediction(mean_price_inv, train_set, pred_test.T, time_ahead=TIME_AHEAD,
                       title='LSTM prediction', xlabel='Days', ylabel='Value of Microsoft Stock', 
                       legend=['True value','Training set','Test prediction'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1516300227445034, 'fraction_numerical': 0.02084912812736922, 'mean_word_length': 4.113372093023256, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '59442', 'license': 'MIT', 'n_tokens_mistral': 2075, 'n_tokens_neox': 1970, 'n_words': 345, 'path': 'Microsoft_stock_price_forecast.ipynb', 'repo_name': 'weixianz/Stock-price-Forecasting-using-Tensorflow', 'size': '5276'}