已知股票的「开盘价」和「收盘价」,利用神经网络来预测「收盘均价」html
日期(data):[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.]
开盘价(beginPrice):[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]
收盘价(endPrice):[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]python
神经网络介绍:https://blog.csdn.net/leiting_imecas/article/details/60463897
激励函数relu()介绍:https://www.cnblogs.com/neopenx/p/4453161.htmlapi
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
date = np.linspace(1,15,15)
# 开始是1,结束是15,有15个数的等差数列
print(date)
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])
# 开盘价
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]
)
# 收盘价
复制代码
结果:数组
[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.]
复制代码
函数解析:网络
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)dom
- 解析:
该函数返回一组具备相同间隔的数据/采样值,数据的间隔经过计算得到(经常使用与来建立等差数列)- 参数:
start:序列的起始值
stop:序列的终止值,除非endpoint被设置为False。当endpoint为True时,数据的间隔:(stop-start)/num。当endpoint为False时,数据的间隔:(stop-start)/(num+1)。
num:采样的数目,默认值为50
endpoint:为真则stop为最后一个采样值,默认为真。
retstep:为真则返回(samples, step),step为不一样采样值的间距
dtype:输出序列的类型。
返回:
samples:n维的数组
step:采样值的间距
plt.figure()
for i in range(0,15):
dateOne = np.zeros([2])
# 建一个两列值为0的矩阵
dateOne[0] = i;
# 第一列的 0-14
dateOne[1] = i;
# 第二列的 0-14
priceOne = np.zeros([2])
# 建一个两列值为0的矩阵
priceOne[0] = beginPrice[i]
# 把开盘价输入第一列
priceOne[1] = endPrice[i]
# 把收盘价输入第二列
if endPrice[i] > beginPrice[i]:
# 若是收盘价 大于 开盘价
plt.plot(dateOne,priceOne,'r',lw=8)
# 条形是红色,宽度为8
else:
plt.plot(dateOne,priceOne,'g',lw=8)
# 条形是绿色,宽度为8
复制代码
结果: ide
函数解析:函数
一、range(start,stop,step)ui
- 只给一个参数 s,表示 从0到s
例如:range(5)
结果:[0,1,2,3,4]- 两个参数,s,e,表示从s到e
例如:range(5,10)
结果:5,6,7,8,9- 三个参数 s,e,i 表示从s到e,间隔i取数
例如:range(0,10,2)
结果:[0,2,4,6,8]
dateNormal = np.zeros([15,1])
# 建立一个15行,1列的矩阵
priceNormal = np.zeros([15,1])
for i in range(0,15):
dateNormal[i,0] = i/14.0;
# 日期的值,最大值为14
priceNormal[i,0] = endPrice[i]/3000.0;
# 价格的值,最大值为3000
x = tf.placeholder(tf.float32,[None,1])
y = tf.placeholder(tf.float32,[None,1])
复制代码
函数解析:google
tf.placeholder(dtype, shape=None, name=None)
- 解析:此函数能够理解为形参,用于定义过程,在执行的时候再赋具体的值
- 参数:
dtype:数据类型。经常使用的是tf.float32,tf.float64等数值类型
shape:数据形状。默认是None,就是一维值,也能够是多维,好比[2,3], [None, 3]表示列是3,行不定
name:名称。- 返回:
Tensor 类型
w1 = tf.Variable(tf.random_uniform([1,10],0,1))
# 建立一个1行10列的矩阵,最小值为0,最大值为1
b1 = tf.Variable(tf.zeros([1,10]))
# 建立一个1行10列的矩阵,值都为0
wb1 = tf.matmul(x,w1)+b1
# wb1 = x * w1 + b1
layer1 = tf.nn.relu(wb1)
# 激励函数的类型:https://tensorflow.google.cn/api_guides/python/nn#Activation_Functions
# 激励函数的做用:https://zhuanlan.zhihu.com/p/25279356
复制代码
w2 = tf.Variable(tf.random_uniform([10,1],0,1))
b2 = tf.Variable(tf.zeros([15,1]))
wb2 = tf.matmul(layer1,w2)+b2
# wb2 = wb1 * w2 + b2
layer2 = tf.nn.relu(wb2)
loss = tf.reduce_mean(tf.square(y-layer2))
# 计算真实值y和计算值layer2的标准差
# 方差 s^2=[(x1-x)^2+(x2-x)^2+......(xn-x)^2]/(n) (x为平均数)
# 标准差=方差的算术平方根
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
# 每次梯度降低0.1,目的是缩小真实值y和计算值layer2的差值
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(0,10000):
sess.run(train_step,feed_dict={x:dateNormal,y:priceNormal})
# feed_dict是一个字典,在字典中须要给出每个用到的占位符的取值,每次迭代选取的数据只会拥有占位符这一个结点。
# 训练出w一、w二、b一、b2,可是还须要检测是否有效
pred = sess.run(layer2,feed_dict={x:dateNormal})
# 训练完的预测结果值
predPrice = np.zeros([15,1])
for i in range(0,15):
predPrice[i,0]=(pred*3000)[i,0]
# pred须要乘以3000是由于前面 priceNormal[i,0] = endPrice[i]/3000.0;
plt.plot(date,predPrice,'b',lw=1)
plt.show()
复制代码
结果:
函数解析:
tf.reduce_mean(input_tensor,axis=None,keepdims=None,name=None,reduction_indices=None,keep_dims=None)
- 解析:计算张量维度上元素的平均值。
- 参数: input_tensor:张量减小。应该有数字类型。 axis:要减少的尺寸。若是None(默认)缩小全部尺寸。必须在范围内 [ rank(input_tensor), rank(input_tensor) )。 keepdims:若是为true,则保留长度为1的缩小尺寸。 name:操做的名称(可选)。 reduction_indices:轴的旧(已弃用)名称。 keep_dims:已过期的别名keepdims。