目錄
機(jī)器學(xué)習(xí)項(xiàng)目實(shí)戰(zhàn)系列 機(jī)器學(xué)習(xí)預(yù)測(cè)股價(jià)
一、概述
二、分析數(shù)據(jù)
1.導(dǎo)入
2.數(shù)據(jù)導(dǎo)入
3.分析股票尾市數(shù)據(jù)
4.構(gòu)建模型
5.測(cè)試模型
6.展示預(yù)測(cè)結(jié)果
根據(jù)上一年的數(shù)據(jù)預(yù)測(cè)股票市場(chǎng)的未來價(jià)格
數(shù)據(jù)集:股票價(jià)格預(yù)測(cè)數(shù)據(jù)集:Two Sigma: Using News to Predict Stock Movements | Kaggle
源代碼:股票價(jià)格預(yù)測(cè)項(xiàng)目:Just a moment...
import pandas as pdimport numpy as np import matplotlib.pyplot as plt%matplotlib inline from matplotlib.pylab import rcParamsrcParams['figure.figsize']=20,10from keras.models import Sequentialfrom keras.layers import LSTM,Dropout,Dense from sklearn.preprocessing import MinMaxScaler
df['Date']=pd.to_datetime(df.Date,format='%Y-%m-%d')df.index=df['Date'] plt.figure(figsize=(16,8))plt.plot(df['Close'],label='Close Price history')
import math#Create a new dataframe with only the Close columndata = df.filter(['Close'])#Convert the dataframe to a numpy arraydataset = data.values #Get the number of rows to train the modeltraining_data_len = math.ceil( len(dataset) * .8)training_data_len
#Scale the datascaler=MinMaxScaler(feature_range=(0,1))scaled_data=scaler.fit_transform(dataset) scaled_data
#Create the training data set#Create the scaled training data settrain_data = scaled_data[0:training_data_len , :]#Split the data into x_train and y_train data setsx_train = []y_train = [] for i in range(60,len(train_data)): x_train.append(train_data[i-60:i,0]) y_train.append(train_data[i,0]) if i<= 60: print(x_train) print(y_train) print()
#Build the LSTM modelmodel = Sequential()model.add(LSTM(50,return_sequences=True,input_shape=(x_train.shape[1],1)))model.add(LSTM(50,return_sequences=False))model.add(Dense(25))model.add(Dense(1))
#Create the testing data set#Create a new array containing scaled values from index 1543 to 2003test_data = scaled_data[training_data_len - 60: , :]#Create the data sets x_test and y_testx_test = []y_test = dataset[training_data_len: , :]for i in range(60, len(test_data)): x_test.append(test_data[i-60:i,0])
#Get the models predicted price valuespredictions = model.predict(x_test)predictions = scaler.inverse_transform(predictions)
#Plot the datatrain = data[:training_data_len]valid = data[training_data_len:]valid['Predictions'] = predictions#Visualize the dataplt.figure(figsize=(16,8))plt.title('Model')plt.xlabel('Date', fontsize=18)plt.ylabel('Close Prise USD ($)', fontsize=18)plt.plot(train['Close'])plt.plot(valid[['Close', 'Predictions']])plt.legend(['Train','Val','Predictions'], loc='lower right')plt.show()
聯(lián)系客服