0 前言
?? 優(yōu)質(zhì)競賽項目系列,今天要分享的是
?? **基于Django與深度學(xué)習(xí)的股票預(yù)測系統(tǒng) **
該項目較為新穎,適合作為競賽課題方向,學(xué)長非常推薦!
??學(xué)長這里給一個題目綜合評分(每項滿分5分)
- 難度系數(shù):3分
- 工作量:3分
- 創(chuàng)新點:5分
?? 更多資料, 項目分享:文章來源:http://www.zghlxwxcb.cn/news/detail-670608.html
https://gitee.com/dancheng-senior/postgraduate文章來源地址http://www.zghlxwxcb.cn/news/detail-670608.html
1 課題背景
隨著經(jīng)濟的發(fā)展,我國的股票市場建設(shè)正不斷加強,社會直接融資正獲得重要發(fā)展。股票市場行情的漲落與國民經(jīng)濟的發(fā)展密切相關(guān)。股票作為一種資本融資和投資的工具,是一種資本的代表形式,股票市場可以讓上市公司便捷地在國內(nèi)和國際市場融資。個人投資者、投資機構(gòu)期望通過技術(shù)手段進行投資分析,能夠從股票市場獲得一定相對高額的投資收益。
2 實現(xiàn)效果
主界面
詳細(xì)數(shù)據(jù)查看
股票切換
相關(guān)html
?
DOCTYPE html>
3 Django框架
Django是一個基于Web的應(yīng)用框架,由python編寫。Web開發(fā)的基礎(chǔ)是B/S架構(gòu),它通過前后端配合,將后臺服務(wù)器的數(shù)據(jù)在瀏覽器上展現(xiàn)給前臺用戶的應(yīng)用。Django本身是基于MVC模型,即Model(模型)+View(視圖)+
Controller(控制器)設(shè)計模式,View模塊和Template模塊組成了它的視圖部分,這種結(jié)構(gòu)使動態(tài)的邏輯是剝離于靜態(tài)頁面處理的。
Django框架的Model層本質(zhì)上是一套ORM系統(tǒng),封裝了大量的數(shù)據(jù)庫操作API,開發(fā)人員不需要知道底層的數(shù)據(jù)庫實現(xiàn)就可以對數(shù)據(jù)庫進行增刪改查等操作。Django強大的QuerySet設(shè)計能夠?qū)崿F(xiàn)非常復(fù)雜的數(shù)據(jù)庫查詢操作,且性能接近原生SQL語句。Django支持包括PostgreSQL、My
Sql、SQLite、Oracle在內(nèi)的多種數(shù)據(jù)庫。Django的路由層設(shè)計非常簡潔,使得將控制層、模型層和頁面模板獨立開進行開發(fā)成為可能。基于Django的Web系統(tǒng)工程結(jié)構(gòu)示意圖如圖所示。
從圖中可以看到,一個完整的Django工程由數(shù)個分應(yīng)用程序組成,每個分應(yīng)用程序包括四個部分:
urls路由層 :決定Web系統(tǒng)路由結(jié)構(gòu),控制頁面間的跳轉(zhuǎn)和數(shù)據(jù)請求路徑
views視圖層
:業(yè)務(wù)層,主要進行邏輯操作和運算,是前端頁面模板和后端數(shù)據(jù)庫之間的橋梁。Django框架提供了大量的數(shù)據(jù)庫操作API,開發(fā)人員甚至不需要使用SQL語句即可完成大部分的數(shù)據(jù)庫操作。
models模型層
:Web應(yīng)用連接底層數(shù)據(jù)庫的關(guān)鍵部分,封裝了數(shù)據(jù)庫表結(jié)構(gòu)和實現(xiàn)。開發(fā)人員可以在Model層按照Django的指令要求進行建表,無須使用SQL語句或者第三方建表工具進行建表。建表的過程類似于定義變量和抽象編程語言中的類,非常方便。
templates模板層
:HTML模板文件,后端數(shù)據(jù)會填充HTML模板,渲染之后返回給前端請求??紤]到項目周期盡可能小,盡快完成平臺的搭建,項目決定采用開源的Django框架開發(fā)整個系統(tǒng)的Web應(yīng)用層。
關(guān)鍵代碼
?
def main():
os.environ.setdefault(‘DJANGO_SETTINGS_MODULE’, ‘ExamOnline.settings’)
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn’t import Django. Are you sure it’s installed and "
"available on your PYTHONPATH environment variable? Did you "
“forget to activate a virtual environment?”
) from exc
execute_from_command_line(sys.argv)
?
?
4 數(shù)據(jù)整理
對于LSTM來說,至少需要兩步整理過程:
- 歸一化
- 變成3D樣本(樣本,時間步,特征數(shù))
對于神經(jīng)網(wǎng)絡(luò)來說,歸一化至關(guān)重要。如果缺失,會無法順利訓(xùn)練和學(xué)習(xí),俗稱:Train不起來。對于LSTM來說,更為重要,因為LSTM內(nèi)部包含tanh函數(shù)使得輸出范圍在-1到1之間。這就需要我們將預(yù)測值也進行歸一化,常見的做法就是直接歸一化到0和1之間。
將一般的特征X和目標(biāo)y變成3D,我這里提供了一個函數(shù),輸入為原始的X_train_raw,X_test_raw,y_train_raw,y_test_raw。?n_input
為需要多少步歷史數(shù)據(jù),n_output為預(yù)測多少步未來數(shù)據(jù)。
?
def transform_dataset(train_set, test_set, y_train, y_test, n_input, n_output):
all_data = np.vstack((train_set, test_set))
y_set = np.vstack((y_train, y_test))[:,0]
X = np.empty((1, n_input, all_data.shape[1]))
y = np.empty((1, n_output))
for i in range(all_data.shape[0] - n_input - n_output):
X_sample = all_data[i:i + n_input, :]
y_sample = y_set[i + n_input:i + n_input + n_output]
if i == 0:
X[i] = X_sample
y[i] = y_sample
else:
X = np.append(X, np.array([X_sample]), axis=0)
y = np.append(y, np.array([y_sample.T]), axis=0)
train_X = X[:train_set.shape[0] - n_input, :, :]
train_y = y[:train_set.shape[0] - n_input, :]
test_X = X[train_set.shape[0] -
n_input:all_data.shape[0] -
n_input -
n_output, :, :]
test_y = y[train_set.shape[0] -
n_input:all_data.shape[0] -
n_input -
n_output, :]
return train_X, train_y, test_X, test_y
5 模型準(zhǔn)備和訓(xùn)練
Keras已經(jīng)包含LSTM
網(wǎng)絡(luò)層,調(diào)用方式和普通的神經(jīng)網(wǎng)絡(luò)沒有特別大的區(qū)別,僅僅需要指定輸入數(shù)據(jù)的shape。這里我們設(shè)計一個簡單的神經(jīng)網(wǎng)絡(luò),輸入層為LSTM,包含20個節(jié)點,輸出層為普通的Dense,損失函數(shù)采用mean_absolute_error。
?
n_timesteps, n_features, n_outputs = train_X.shape[1], train_X.shape[2], train_y.shape[1]
# create a model
model = Sequential()
model.add(LSTM(10, input_shape=(n_timesteps, n_features),kernel_initializer=‘glorot_uniform’,
kernel_regularizer=regularizers.l2(0.0),return_sequences=False))
#model.add(LSTM(20, input_shape=(n_timesteps, n_features),kernel_initializer=‘glorot_uniform’,
# kernel_regularizer=regularizers.l2(0.0)))
model.add(Dense(n_outputs,kernel_initializer='glorot_uniform',
kernel_regularizer=regularizers.l2(0.0)))
model.compile(optimizer='adam', loss='mean_absolute_error')
print(model.summary())
調(diào)用fit函數(shù)對訓(xùn)練集進行學(xué)習(xí)。由于時間序列具有很明顯的趨勢,因此有必要將樣本打亂。這里需要說明:我們打亂的是“樣本”,不影響每個樣本內(nèi)在的序列關(guān)系。LSTM只會根據(jù)樣本內(nèi)在的序列關(guān)系(時間步)來更新自己的隱狀態(tài)。
?
from sklearn.utils import shuffle
train_X,train_y = shuffle(train_X,train_y,random_state=42)
plt.plot(train_y)
# fit the RNN model
history = model.fit(
train_X,
train_y,
epochs=300,
batch_size=512,
validation_split=0.3)
figure = plt.Figure()
plt.plot(history.history[‘loss’],
‘b’,
label=‘Training loss’)
plt.plot(history.history[‘val_loss’],
‘r’,
label=‘Validation loss’)
plt.legend(loc=‘upper right’)
plt.xlabel(‘Epochs’)
plt.show()
查看loss曲線,確保訓(xùn)練已經(jīng)穩(wěn)定。
6 最后
?? 更多資料, 項目分享:
https://gitee.com/dancheng-senior/postgraduate
到了這里,關(guān)于計算機競賽 基于Django與深度學(xué)習(xí)的股票預(yù)測系統(tǒng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!