5. Deep Learning for Sequences
Activity 5.01: Using a Plain RNN Model to Predict IBM Stock Prices
Solution
- Import the necessary libraries, load the
.csv
file, reverse the index, and plot the time series (theClose
column) for visual inspection:import pandas as pd, numpy as np import matplotlib.pyplot as plt inp0 = pd.read_csv("IBM.csv") inp0 = inp0.sort_index(ascending=False) inp0.plot("Date", "Close") plt.show()
The output will be as follows, with the closing price plotted on the Y-axis:
- Extract the values for
Close
from the DataFrame as anumpy
array and plot them usingmatplotlib
:ts_data = inp0.Close.values.reshape(-1,1) plt.figure(figsize=[14,5]) plt.plot(ts_data) plt.show()
The resulting trend is as follows, with the index plotted on the X-axis:
- Assign the final 25% data as test data and the first 75% as train data:
train_recs = int(len(ts_data...