1.基本數據繪制成圖
數據有15天股票的開盤價格和收盤價格,可以通過比較當天開盤價格和收盤價格的大小來判斷當天股票價格的漲跌情況,紅色表示漲,綠色表示跌,測試代碼如下:
1 # encoding:utf-8 2 3 import tensorflow as tf 4 import numpy as np 5 import matplotlib.pyplot as plt 6 date = np.linspace(1, 15, 15) 7 # 當天的收盤價格 8 endPrice = np.array([2511.90,2538.26,2510.68,2591.66,2732.98,2701.69,2701.29,2678.67,2726.50,2681.50,2739.17,2715.07,2823.58,2864.90,2919.08] 9 ) 10 # 當天的開盤價格 11 beginPrice = np.array([2438.71,2500.88,2534.95,2512.52,2594.04,2743.26,2697.47,2695.24,2678.23,2722.13,2674.93,2744.13,2717.46,2832.73,2877.40]) 12 print(date) # 打印日期 13 plt.figure() 14 for i in range(0,15): 15 # 通過循環遍歷數據畫出柱狀圖 16 dateOne = np.zeros([2]) 17 dateOne[0] = i 18 dateOne[1] = i 19 print(dateOne) 20 priceOne = np.zeros([2]) 21 priceOne[0] = beginPrice[i] 22 priceOne[1] = endPrice[i] 23 if endPrice[i] > beginPrice[i]: 24 # 如果收盤價格大於開盤價格說明股票上漲 用紅色表示 lw為線條粗細 25 plt.plot(dateOne, priceOne,'r',lw=8) 26 else: 27 # 如果收盤價格小於開盤價格說明股票下跌 用綠色表示 lw為線條粗細 28 plt.plot(dateOne, priceOne,'g',lw=5) 29 plt.show()
運行后的圖如下:
2.人工神經網絡進行預測
建立一個簡單的三層人工神經網絡。
循環的終止條件可以為預先設定的循環次數或者與真實值的差異百分比
功能實現,完整的測試代碼如下:
1 # encoding:utf-8 2 3 import tensorflow as tf 4 import numpy as np 5 import matplotlib.pyplot as plt 6 date = np.linspace(1, 15, 15) 7 # 當天的收盤價格 8 endPrice = np.array([2511.90,2538.26,2510.68,2591.66,2732.98,2701.69,2701.29,2678.67,2726.50,2681.50,2739.17,2715.07,2823.58,2864.90,2919.08] 9 ) 10 # 當天的開盤價格 11 beginPrice = np.array([2438.71,2500.88,2534.95,2512.52,2594.04,2743.26,2697.47,2695.24,2678.23,2722.13,2674.93,2744.13,2717.46,2832.73,2877.40]) 12 print(date) # 打印日期 13 plt.figure() 14 for i in range(0,15): 15 # 通過循環遍歷數據畫出柱狀圖 16 dateOne = np.zeros([2]) 17 dateOne[0] = i 18 dateOne[1] = i 19 print(dateOne) 20 priceOne = np.zeros([2]) 21 priceOne[0] = beginPrice[i] 22 priceOne[1] = endPrice[i] 23 if endPrice[i] > beginPrice[i]: 24 # 如果收盤價格大於開盤價格說明股票上漲 用紅色表示 lw為線條粗細 25 plt.plot(dateOne, priceOne,'r',lw=8) 26 else: 27 # 如果收盤價格小於開盤價格說明股票下跌 用綠色表示 lw為線條粗細 28 plt.plot(dateOne, priceOne,'g',lw=5) 29 # plt.show() 30 # A(15x1)*w1(1x10)+b1(1*10) = B(15x10) 31 # B(15x10)*w2(10x1)+b2(15x1) = C(15x1) 32 # 1 A B C 33 dateNormal = np.zeros([15,1]) 34 priceNormal = np.zeros([15,1]) 35 # 日期和價格進行歸一化處理 36 for i in range(0, 15): 37 dateNormal[i, 0] = i/14.0 38 priceNormal[i, 0] = endPrice[i]/3000.0 39 print(dateNormal) 40 print(priceNormal) 41 42 x = tf.placeholder(tf.float32, [None, 1]) # 表明是N行1列的 43 y = tf.placeholder(tf.float32, [None, 1]) # 表明是N行1列的 44 45 # B 46 w1 = tf.Variable(tf.random_uniform([1, 10], 0, 1)) # 可變值 可以通過誤差修改值 范圍0-1 47 b1 = tf.Variable(tf.zeros([1, 10])) # 可變值 可以通過誤差修改值 48 wb1 = tf.matmul(x, w1)+b1 49 layer1 = tf.nn.relu(wb1) # 激勵函數 映射成另一個值 50 # 第一二層完畢 51 52 # C 53 w2 = tf.Variable(tf.random_uniform([10, 1], 0, 1)) # 可變值 可以通過誤差修改值 范圍0-1 54 b2 = tf.Variable(tf.zeros([15, 1])) 55 wb2 = tf.matmul(layer1, w2)+b2 56 layer2 = tf.nn.relu(wb2) # 激勵函數 映射成另一個值 57 # 第二三層完畢 58 59 # 誤差用loss表示 實際是一個標准差 60 loss = tf.reduce_mean(tf.square(y-layer2)) # y 真實 layer2 計算 61 # 每次調整的步長 梯度下降0.1 目的是縮小loss減小真實值與誤差值的差異 62 train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) 63 with tf.Session() as sess: 64 sess.run(tf.global_variables_initializer()) # 初始化 65 for i in range(0, 10000): # 訓練次數為10000 66 sess.run(train_step, feed_dict={x: dateNormal, y: priceNormal}) 67 # w1w2 b1b2 A + wb -->layer2 68 pred = sess.run(layer2, feed_dict={x: dateNormal}) 69 predPrice = np.zeros([15, 1]) # 預測結果 70 for i in range(0, 15): # 還原數據需要*3000 71 predPrice[i, 0] = (pred*3000)[i, 0] 72 plt.plot(date, predPrice, 'b', lw=1) 73 plt.show()
運行結果如下:(圖中藍色的線表示股票的預測值)