官术网_书友最值得收藏!

How to do it...

The above strategy is coded as follows (please refer to Chapter 3  - stock price prediction.ipynb file in GitHub while implementing the code and for the recommended dataset):

  1.  Import the relevant packages and the dataset:
import pandas as pd
data2 = pd.read_csv('/content/stock_data.csv')
  1. Prepare the dataset where the input is the previous five days' stock price value and the output is the stock price value on the sixth day:
x= []
y = []
for i in range(data2.shape[0]-5):
x.append(data2.loc[i:(i+4)]['Close'].values)
y.append(data2.loc[i+5]['Close'])
import numpy as np
x = np.array(x)
y = np.array(y)
  1. Prepare the train and test datasets, build the model, compile it, and fit it:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.30,random_state=10)

Build the model and compile it:


from keras.layers import Dense
from keras.models import Sequential, Model
model = Sequential()
model.add(Dense(100, input_dim = 5, activation = 'relu'))
model.add(Dense(1,activation='linear'))
model.compile(optimizer='adam', loss='mean_squared_error')

The previous code results in a summary of model as follows:

model.fit(X_train, y_train, epochs=100, batch_size=64, validation_data=(X_test, y_test), verbose = 1)

Once we fit the model, we should note that the mean squared error value ~$360 in predicting the stock price or ~$18 in predicting the stock price.

Note that there is a pitfall in predicting a stock price this way. However, that will be dealt with in the chapter on RNN applications.

For now, we will focus on learning how neural networks can be useful in a variety of different scenarios.

In the next section, we will understand the ways in which we can integrate the numeric data with the text data of news headlines in a single model.

主站蜘蛛池模板: 大姚县| 绥芬河市| 卫辉市| 出国| 都安| 墨竹工卡县| 台州市| 南乐县| 新平| 靖西县| 濮阳市| 丘北县| 依兰县| 中方县| 昌黎县| 赫章县| 乐安县| 望奎县| 潜山县| 扶绥县| 桑日县| 景谷| 巴楚县| 肥东县| 仁布县| 巧家县| 锡林郭勒盟| 北辰区| 双桥区| 鄱阳县| 黔西县| 梁平县| 香格里拉县| 安岳县| 辽宁省| 澄城县| 松阳县| 远安县| 额尔古纳市| 清苑县| 青铜峡市|