使用神经网络预测股票走势

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

# 定义模拟价格
date = np.linspace(0, 15, 15)
endPrice = np.array([114.90, 113.82, 116.56, 108.38, 112.43,
                     120.23, 120.90, 121.34, 121.00, 122.64, 122.06, 119.62, 121.93, 122.15, 120.97])
beginPrice = np.array([116.43, 114.89, 114.26, 119.22, 109.47,
                       114.72, 119.93, 121.60, 120.48, 120.24, 122.66, 123.00, 121.00, 122.97, 122.87])

for i in range(15):
    # 化柱状图
    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] > 0:
        plt.plot(dateOne, priceOne, 'r', lw=8)
        # 下跌为绿色
    else:
        plt.plot(dateOne, priceOne, 'g', lw=8)

# plt.show()

# 归一化处理数据
dateNormal = np.zeros([15, 1])
priceNormal = np.zeros([15, 1])
for i in range(15):
    dateNormal[i, 0] = i / 14.0
    priceNormal[i, 0] = endPrice[i] / 150
with tf.device('/gpu:0'):
    x = tf.placeholder(tf.float32, [None, 1])
    y = tf.placeholder(tf.float32, [None, 1])
    w1 = tf.Variable(tf.random_uniform([1, 10], 0, 1))
    b1 = tf.Variable(tf.zeros([1, 10]))
    z1 = tf.matmul(x, w1) + b1
    a1 = tf.nn.relu(z1)
    w2 = tf.Variable(tf.random_uniform([10, 1], 0, 1))
    b2 = tf.Variable(tf.zeros([15, 1]))
    z2 = tf.matmul(a1, w2) + b2
    a2 = tf.nn.relu(z2)
    loss = tf.reduce_mean(tf.square(y - a2))
    train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)) as sess:
    sess.run(tf.global_variables_initializer())
    # 迭代10000次
    for i in range(10000):
        sess.run(train_step, feed_dict={x: dateNormal, y: priceNormal})
    pred = sess.run(a2, feed_dict={x: dateNormal})
    # 将数据还原回去
    predPrice = np.zeros([15, 1])
    for i in range(15):
        predPrice[i, 0] = (pred * 150)[i, 0]
    # plt.plot(date,beginPrice,'r',lw=1)
    # plt.plot(date,endPrice,'g',lw=1)
    plt.plot(date, predPrice, 'b', lw=1)
plt.show()

好久没有用tensorflow框架了,今天用起来感觉有点手生!!代码以后要长写!
数据用的是新东方的股票走势
结果

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值