機器學(xué)習(xí)項目實戰(zhàn)系列機器學(xué)習(xí)預(yù)測股價目錄
一、概述根據(jù)上一年的數(shù)據(jù)預(yù)測股票市場的未來價格 數(shù)據(jù)集:股票價格預(yù)測數(shù)據(jù)集:Two Sigma: Using News to Predict Stock Movements | Kaggle 源代碼:股票價格預(yù)測項目:Just a moment... 二、分析數(shù)據(jù)1.導(dǎo)入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 2.數(shù)據(jù)導(dǎo)入3.分析股票尾市數(shù)據(jù)
4.構(gòu)建模型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
#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()
5.測試模型#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])
6.展示預(yù)測結(jié)果#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() |
|