Python Tensorflow神经网络实现股票预测
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Python Tensorflow神经网络实现股票预测
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                神經網絡(NN)它是一種模仿動物神經網絡行為特征,進行分布式并行信息處理的算法數學模型。這種網絡依靠系統的復雜程度,通過調整內部大量節點之間相互連接的關系,從而達到處理信息的目的。在提供數據量足夠大情況下,神經網絡可以擬合出輸入到輸出之間的任意函數關系。
Tensorflow是一個優秀的深度學習框架,具體有啥好處,可以百度了解哈。
本文分享使用Tensorflow神經網絡進行股市的預測
?
1、數據來源
首先找到一組股票數據,數據可以網絡上爬蟲,東方財富、大智慧都有。爬蟲方法參看以前的文章。
date = np.linspace(1, 30, 30) #beginPrice = np.array([2923.19, 2928.06, 2943.92, 2946.26, 2944.40, 2920.85, 2861.33, 2854.58, 2776.69, 2789.02, 2784.18, 2805.59, 2781.98, 2798.05, 2824.49, 2762.34, 2817.57, 2835.52, 2879.08, 2875.47, 2887.66, 2885.15, 2851.02, 2879.52, 2901.63, 2896.00, 2907.38, 2886.94, 2925.94, 2927.75])endPrice = np.array([2937.36, 2944.54, 2941.01, 2952.34, 2932.51, 2908.77, 2867.84, 2821.50, 2777.56, 2768.68, 2794.55, 2774.75, 2814.99, 2797.26, 2808.91, 2815.80, 2823.82, 2883.10, 2880.00, 2880.33, 2883.44, 2897.43, 2863.57, 2902.19, 2893.76, 2890.92, 2886.24, 2924.11, 2930.15, 2957.41])2、數據展示
基于matplotlib可視化庫,建立一個30行2列的矩陣存儲股票數據,矩陣的第一列是股票開盤價格,第二列是股票的收盤價格,如果股票的收盤價格高于開盤價格則用紅色顯示,反之則用綠色顯示,可視化股票數據如下圖所示。
for i in range(0, 30): # 畫柱狀圖 dateOne = np.zeros([2]) dateOne[0] = i dateOne[1] = i priceOne = np.zeros([2]) priceOne[0] = beginPrice[i] priceOne[1] = endPrice[i] if endPrice[i] > beginPrice[i]: plt.plot(dateOne, priceOne, 'r', lw=6) else: plt.plot(dateOne, priceOne, 'g', lw=6)plt.xlabel("date")plt.ylabel("price")plt.show()?
3、Tensorflow預測
基于Tensorflow神經網絡框架,設計了三層神經網絡,其中隱含層包括25個節點,設計的神經網絡用來預測股票的收盤價。
dateNormal = np.zeros([30, 1])priceNormal = np.zeros([30, 1])# 歸一化for i in range(0, 30): dateNormal[i, 0] = i / 29.0 priceNormal[i, 0] = endPrice[i] / 3000.0?x = tf.placeholder(tf.float32, [None, 1])y = tf.placeholder(tf.float32, [None, 1])# X->hidden_layerw1 = tf.Variable(tf.random_uniform([1, 25], 0, 1))b1 = tf.Variable(tf.zeros([1, 25]))wb1 = tf.matmul(x, w1) + b1layer1 = tf.nn.relu(wb1) # 激勵函數# hidden_layer->outputw2 = tf.Variable(tf.random_uniform([25, 1], 0, 1))b2 = tf.Variable(tf.zeros([30, 1]))wb2 = tf.matmul(layer1, w2) + b2layer2 = tf.nn.relu(wb2)loss = tf.reduce_mean(tf.square(y - layer2)) # y為真實數據, layer2為網絡預測結果# 梯度下降train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(0, 20000): sess.run(train_step, feed_dict={x: dateNormal, y: priceNormal}) # 預測, X w1w2 b1b2 -->layer2 pred = sess.run(layer2, feed_dict={x: dateNormal}) date1 = np.linspace(0, 29, 30) # plt.plot(date1, pred*3000, 'b', lw=3)?plt.show()?
運行以上代碼可視化神經網絡的預測結果如下圖所示
?
完整的代碼如下:
import numpy as npimport matplotlib.pyplot as pltimport?tensorflow?as?tf# import tensorflow.compat.v1 as tf# tf.disable_v2_behavior() # 如果是tensorflow2版本就取消這行注釋?date = np.linspace(1, 30, 30) #beginPrice = np.array([2923.19, 2928.06, 2943.92, 2946.26, 2944.40, 2920.85, 2861.33, 2854.58, 2776.69, 2789.02, 2784.18, 2805.59, 2781.98, 2798.05, 2824.49, 2762.34, 2817.57, 2835.52, 2879.08, 2875.47, 2887.66, 2885.15, 2851.02, 2879.52, 2901.63, 2896.00, 2907.38, 2886.94, 2925.94, 2927.75])endPrice = np.array([2937.36, 2944.54, 2941.01, 2952.34, 2932.51, 2908.77, 2867.84, 2821.50, 2777.56, 2768.68, 2794.55, 2774.75, 2814.99, 2797.26, 2808.91, 2815.80, 2823.82, 2883.10, 2880.00, 2880.33, 2883.44, 2897.43, 2863.57, 2902.19, 2893.76, 2890.92, 2886.24, 2924.11, 2930.15, 2957.41])?for i in range(0, 30): # 畫柱狀圖 dateOne = np.zeros([2]) dateOne[0] = i dateOne[1] = i priceOne = np.zeros([2]) priceOne[0] = beginPrice[i] priceOne[1] = endPrice[i] if endPrice[i] > beginPrice[i]: plt.plot(dateOne, priceOne, 'r', lw=6) else: plt.plot(dateOne, priceOne, 'g', lw=6)plt.xlabel("date")plt.ylabel("price")# plt.show()??dateNormal = np.zeros([30, 1])priceNormal = np.zeros([30, 1])# 歸一化for i in range(0, 30): dateNormal[i, 0] = i / 29.0 priceNormal[i, 0] = endPrice[i] / 3000.0?x = tf.placeholder(tf.float32, [None, 1])y = tf.placeholder(tf.float32, [None, 1])# X->hidden_layerw1 = tf.Variable(tf.random_uniform([1, 25], 0, 1))b1 = tf.Variable(tf.zeros([1, 25]))wb1 = tf.matmul(x, w1) + b1layer1 = tf.nn.relu(wb1) # 激勵函數# hidden_layer->outputw2 = tf.Variable(tf.random_uniform([25, 1], 0, 1))b2 = tf.Variable(tf.zeros([30, 1]))wb2 = tf.matmul(layer1, w2) + b2layer2 = tf.nn.relu(wb2)loss = tf.reduce_mean(tf.square(y - layer2)) # y為真實數據, layer2為網絡預測結果# 梯度下降train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(0, 20000): sess.run(train_step, feed_dict={x: dateNormal, y: priceNormal}) # 預測, X w1w2 b1b2 -->layer2 pred = sess.run(layer2, feed_dict={x: dateNormal}) date1 = np.linspace(0, 29, 30) # plt.plot(date1, pred*3000, 'b', lw=3)?plt.show()代碼中需要用到numpy、matplotlib和tensorflow三個庫,為了提高下載速度,建議切換到國內的pip源,例如豆瓣、清華等
pip?install?numpy -i https://pypi.tuna.tsinghua.edu.cn/simplepip install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simplepip install tensorflow -i https://pypi.tuna.tsinghua.edu.cn/simple?
總結
以上是生活随笔為你收集整理的Python Tensorflow神经网络实现股票预测的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: matlab plot title 包含
- 下一篇: Python面向对象---类的基本使用
