- Neural Networks with Keras Cookbook
- V Kishore Ayyadevara
- 267字
- 2021-07-02 12:46:34
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):
- Import the relevant packages and the dataset:
import pandas as pd
data2 = pd.read_csv('/content/stock_data.csv')
- 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)
- 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.
- Web應(yīng)用系統(tǒng)開(kāi)發(fā)實(shí)踐(C#)
- JavaScript前端開(kāi)發(fā)模塊化教程
- Reporting with Visual Studio and Crystal Reports
- Java Web及其框架技術(shù)
- Network Automation Cookbook
- UI智能化與前端智能化:工程技術(shù)、實(shí)現(xiàn)方法與編程思想
- 區(qū)塊鏈底層設(shè)計(jì)Java實(shí)戰(zhàn)
- Arduino家居安全系統(tǒng)構(gòu)建實(shí)戰(zhàn)
- 零基礎(chǔ)入門(mén)學(xué)習(xí)Python(第2版)
- Kubernetes源碼剖析
- SQL Server 2008 R2數(shù)據(jù)庫(kù)技術(shù)及應(yīng)用(第3版)
- R語(yǔ)言:邁向大數(shù)據(jù)之路(加強(qiáng)版)
- Android嵌入式系統(tǒng)程序開(kāi)發(fā)(基于Cortex-A8)
- Windows 10 for Enterprise Administrators
- HoloLens Blueprints