• 神经网络逼近股票价格


    #!/usr/bin/python
    # -*- coding:UTF-8 -*-
    # author:cxx time:2019/8/28 0028
    import tensorflow as tf
    import numpy as np
    import matplotlib.pyplot as plt
    date = np.linspace(1,15,15)#在1-15之间生成15个间隔相同得数字作为十五天的日期
    endPrice = np.array([2511,2538,2510,2591,2732,2701,2701,2678,2726,2681,2739,2715,2823,2864,2919])#当日股票收盘时(下午三点)的价格
    beginPrice = np.array([2438,2500,2534,2512,2594,2743,2697,2695,2678,2722,2674,2744,2717,2832,2877])#当日股票开盘时(上午九点)的价格
    print(date)
    plt.figure()
    for i in range(0,15):
    dateOne = np.zeros([2])#生成一个初始值为0的长度为2的一维数组-->开盘收盘的日期
    dateOne[0] = i
    dateOne[1] = i
    priceOne = np.zeros([2])#价格
    priceOne[0] = beginPrice[i]#当日开盘价格
    priceOne[1] = endPrice[i]#当日收盘价格
    if beginPrice[i]>endPrice[i]:
    plt.plot(dateOne,priceOne,'g',lw=8)
    else:
    plt.plot(dateOne,priceOne,'r',lw=8)
    # A(15x1)*w1(1x10)+b1(1x10) = B(15x10)
    # B(15x10)*w2(10x1)+b2(15x1) = C(15x1)
    #A 输入层
    dateNormal = np.zeros([15,1])
    priceNormal = np.zeros([15,1])
    for i in range(0,15):
    dateNormal[i,0] = i/14.0#归一化
    priceNormal[i,0] = endPrice[i]/3000.0
    x = tf.placeholder(tf.float32,[None,1])#placeholder用于占位
    y = tf.placeholder(tf.float32,[None,1])
    #B 隐藏层
    w1 = tf.Variable(tf.random_uniform([1,10],0,1))#返回值在0-1之间的一行十列的矩阵
    b1 = tf.Variable(tf.zeros([1,10]))
    wb1 = tf.matmul(x,w1)+b1
    layer1 = tf.nn.relu(wb1)#激励函数relu
    #C 输出层
    w2 = tf.Variable(tf.random_uniform([10,1],0,1))
    b2 = tf.Variable(tf.zeros([15,1]))
    wb2 = tf.matmul(layer1,w2)+b2
    layer2 = 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,50):
    sess.run(train_step,feed_dict=({x:dateNormal,y:priceNormal}))#训练出优化的w1,w2,b1,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]#还原归一化后的价格
    plt.plot(date,predPrice,'b',lw=1)
    plt.show()

    效果图:

  • 相关阅读:
    webapi支持session
    webapi返回数据同时支持xml与json
    webapi权限控制
    Asp.net 将js文件打包进dll 方法
    All Media to FLV asp.net在线视频自动转换并截图调试成功!
    未能从程序集“System.ServiceModel
    [javascript]浮动广告
    [python]multi thread tcp connect port scanner
    [vc]让你Y的用YY
    [python]扫描网站后台脚本
  • 原文地址:https://www.cnblogs.com/cxxBoo/p/11443756.html
Copyright © 2020-2023  润新知