http://www.sh-dupai.com

  • 当前位置:8号配资网 > 配资平台 >
  • 10 股票价格预测,今天的股市行情及走势

    欢迎来到8号配资网,下面小编就介绍下10 股票价格预测,今天的股市行情及走势的相关资讯。

    股价预测分析是一件十分忽悠人的事儿,但假如只根据历史记录开展预测分析,显而易见彻底不可靠

    10 股票价格预测,今天的股市行情及走势 明日股票市场行情

    股价是典型性的时间序列分析数据信息(通称时序数据),会遭受经济形势、政府政策、人为因素实际操作多种多样繁杂要素的危害

    不象气象数据那般具有显著的時间和周期性方式,比如一天以内和一年以内的平均气温转变等

    美股行情

    即便如此,以股价为例子,详细介绍怎样对时序数据开展预测分析,依然非常值得一做

    下列应用TensorFlow和Keras,对 S&P 500 股票价格数据信息开展剖析和预测分析

    黄金股票个股

    S&P 500 股票价格数据爬取自Google Finance API,早已开展过缺少值解决

    载入库,pandas关键用以数据清洗和梳理

    股市行情

    # -*- coding: utf-8 -*- import pandas as pd import numpy as np import tensorflow as tf import matplotlib.pyplot as plt %matplotlib inline from sklearn.preprocessing import MinMaxScaler import time 复制代码

    用pandas载入csv文件为DataFrame,并且用 describe() 查询特点的标值遍布

    2020股票市场行情预测分析

    data = pd.read_csv('data_stocks.csv') data.describe() 复制代码

    数据信息共502列,41266行,502列各自为:

    股价如何计算股票预测

    print(time.strftime('%Y-%m-%d', time.localtime(data['DATE'].max())), time.strftime('%Y-%m-%d', time.localtime(data['DATE'].min()))) 复制代码

    plt.plot(data['SP500']) 复制代码

    data.drop('DATE', axis=1, inplace=True) data_train = data.iloc[:int(data.shape[0] * 0.8), :] data_test = data.iloc[int(data.shape[0] * 0.8):, :] print(data_train.shape, data_test.shape) 复制代码

    数据归一化,只有应用 data_train 开展 fit()

    scaler = MinMaxScaler(feature_range=(-1, 1)) scaler.fit(data_train) data_train = scaler.transform(data_train) data_test = scaler.transform(data_test) 复制代码

    同歩预测分析就是指,应用当今時刻的500支股票股票价格,预测分析当今時刻的股市大盘,即一个回归问题,键入共500维特点,輸出一维,即 [None, 500] => [None, 1]

    应用TensorFlow完成同歩预测分析,关键采用多层感知机(Multi-Layer Perceptron,MLP),损失函数用均方误差(Mean Square Error,MSE)

    X_train = data_train[:, 1:] y_train = data_train[:, 0] X_test = data_test[:, 1:] y_test = data_test[:, 0] input_dim = X_train.shape[1] hidden_1 = 1024 hidden_2 = 512 hidden_3 = 256 hidden_4 = 128 output_dim = 1 batch_size = 256 epochs = 10 tf.reset_default_graph() X = tf.placeholder(shape=[None, input_dim], dtype=tf.float32) Y = tf.placeholder(shape=[None], dtype=tf.float32) W1 = tf.get_variable('W1', [input_dim, hidden_1], initializer=tf.contrib.layers.xavier_initializer(seed=1)) b1 = tf.get_variable('b1', [hidden_1], initializer=tf.zeros_initializer()) W2 = tf.get_variable('W2', [hidden_1, hidden_2], initializer=tf.contrib.layers.xavier_initializer(seed=1)) b2 = tf.get_variable('b2', [hidden_2], initializer=tf.zeros_initializer()) W3 = tf.get_variable('W3', [hidden_2, hidden_3], initializer=tf.contrib.layers.xavier_initializer(seed=1)) b3 = tf.get_variable('b3', [hidden_3], initializer=tf.zeros_initializer()) W4 = tf.get_variable('W4', [hidden_3, hidden_4], initializer=tf.contrib.layers.xavier_initializer(seed=1)) b4 = tf.get_variable('b4', [hidden_4], initializer=tf.zeros_initializer()) W5 = tf.get_variable('W5', [hidden_4, output_dim], initializer=tf.contrib.layers.xavier_initializer(seed=1)) b5 = tf.get_variable('b5', [output_dim], initializer=tf.zeros_initializer()) h1 = tf.nn.relu(tf.add(tf.matmul(X, W1), b1)) h2 = tf.nn.relu(tf.add(tf.matmul(h1, W2), b2)) h3 = tf.nn.relu(tf.add(tf.matmul(h2, W3), b3)) h4 = tf.nn.relu(tf.add(tf.matmul(h3, W4), b4)) out = tf.transpose(tf.add(tf.matmul(h4, W5), b5)) cost = tf.reduce_mean(tf.squared_difference(out, Y)) optimizer = tf.train.AdamOptimizer().minimize(cost) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for e in range(epochs): shuffle_indices = np.random.permutation(np.arange(y_train.shape[0])) X_train = X_train[shuffle_indices] y_train = y_train[shuffle_indices] for i in range(y_train.shape[0] // batch_size): start = i * batch_size batch_x = X_train[start : start batch_size] batch_y = y_train[start : start batch_size] sess.run(optimizer, feed_dict={X: batch_x, Y: batch_y}) if i % 50 == 0: print('MSE Train:', sess.run(cost, feed_dict={X: X_train, Y: y_train})) print('MSE Test:', sess.run(cost, feed_dict={X: X_test, Y: y_test})) y_pred = sess.run(out, feed_dict={X: X_test}) y_pred = np.squeeze(y_pred) plt.plot(y_test, label='test') plt.plot (y_pred, label='pred') plt.title('Epoch ' str(e) ', Batch ' str(i)) plt.legend() plt.show() 复制代码

    最终检测集的loss在0.005上下,预测分析結果以下

    应用Keras完成同歩预测分析,编码量会少许多 ,但实际完成关键点不如TensorFlow灵便

    from keras.layers import Input, Dense from keras.models import Model X_train = data_train[:, 1:] y_train = data_train[:, 0] X_test = data_test[:, 1:] y_test = data_test[:, 0] input_dim = X_train.shape[1] hidden_1 = 1024 hidden_2 = 512 hidden_3 = 256 hidden_4 = 128 output_dim = 1 batch_size = 256 epochs = 10 X = Input(shape=[input_dim,]) h = Dense(hidden_1, activation='relu')(X) h = Dense(hidden_2, activation='relu')(h) h = Dense(hidden_3, activation='relu')(h) h = Dense(hidden_4, activation='relu')(h) Y = Dense(output_dim, activation='sigmoid')(h) model = Model(X, Y) model.compile(loss='mean_squared_error', optimizer='adam') model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, shuffle=False) y_pred = model.predict(X_test) print('MSE Train:', model.evaluate(X_train, y_train, batch_size=batch_size)) print('MSE Test:', model.evaluate(X_test, y_test, batch_size=batch_size)) plt.plot(y_test, label='test') plt.plot(y_pred, label='pred') plt.legend() plt.show() 复制代码

    最终检测集的loss在0.007上下,预测分析結果以下

    多线程预测分析就是指,应用历史时间数个時刻的股市大盘,预测分析当今時刻的股市大盘,那样才更为合乎预测分析的界定

    比如,应用前五个股市大盘,预测分析当今的股市大盘,每一组键入包含五个step,每一个step相匹配一个历史时间時刻的股市大盘,輸出一维,即 [None, 5, 1] => [None, 1]

    应用Keras完成多线程预测分析,关键采用循环系统神经元网络即RNN(Recurrent Neural Network)中的LSTM(Long Short-Term Memory)

    from keras.layers import Input, Dense, LSTM from keras.models import Model output_dim = 1 batch_size = 256 epochs = 10 seq_len = 5 hidden_size = 128 X_train = np.array([data_train[i : i seq_len, 0] for i in range(data_train.shape[0] - seq_len)])[:, :, np.newaxis] y_train = np.array([data_train[i seq_len, 0] for i in range(data_train.shape[0] - seq_len)]) X_test = np.array([data_test[i : i seq_len, 0] for i in range(data_test.shape[0] - seq_len)])[:, :, np.newaxis] y_test = np.array([data_test[i seq_len, 0] for i in range(data_test.shape[0] - seq_len)]) print(X_train.shape, y_train.shape, X_test.shape, y_test.shape) X = Input(shape=[X_train.shape[1], X_train.shape[2],]) h = LSTM(hidden_size, activation='relu')(X) Y = Dense(output_dim, activation='sigmoid')(h) model = Model(X, Y) model.compile(loss='mean_squared_error', optimizer='adam') model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, shuffle=False) y_pred = model.predict(X_test) print('MSE Train:', model.evaluate(X_train, y_train, batch_size=batch_size)) print('MSE Test:', model.evaluate(X_test, y_test, batch_size=batch_size)) plt.plot(y_test, label='test') plt.plot(y_pred, label='pred') plt.legend() plt.show() 复制代码

    最终检测集的loss在0.0015上下,预测分析結果以下,一层LSTM的实际效果早已好十分多了

    自然,也有一种很有可能的试着,应用历史时间数个時刻的500支股票股票价格及其股市大盘,预测分析当今時刻的股市大盘,即 [None, 5, 501] => [None, 1]

    from keras.layers import Input, Dense, LSTM from keras.models import Model output_dim = 1 batch_size = 256 epochs = 10 seq_len = 5 hidden_size = 128 X_train = np.array([data_train[i : i seq_len, :] for i in range(data_train.shape[0] - seq_len)]) y_train = np.array([data_train[i seq_len, 0] for i in range(data_train.shape[0] - seq_len)]) X_test = np.array([data_test[i : i seq_len, :] for i in range(data_test.shape[0] - seq_len)]) y_test = np.array([data_test[i seq_len, 0] for i in range(data_test.shape[0] - seq_len)]) print(X_train.shape, y_train.shape, X_test.shape, y_test.shape) X = Input(shape=[X_train.shape[1], X_train.shape[2],]) h = LSTM(hidden_size, activation='relu')(X) Y = Dense(output_dim, activation='sigmoid')(h) model = Model(X, Y) model.compile(loss='mean_squared_error', optimizer='adam') model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, shuffle=False) y_pred = model.predict(X_test) print('MSE Train:', model.evaluate(X_train, y_train, batch_size=batch_size)) print('MSE Test:', model.evaluate(X_test, y_test, batch_size=batch_size)) plt.plot(y_test, label='test') plt.plot(y_pred, label='pred') plt.legend() plt.show() 复制代码

    最终的loss在0.004 上下,結果反倒下降了

    500支股票再加上股市大盘的预测分析实际效果,还比不上仅应用股市大盘

    表明特点并并不是愈多愈好,有时反倒会导入多余的噪声

    因为仍未牵涉到繁杂的CNN或RNN,因此 在CPU上运作的速率还能够


    以上全部内容股票资讯sh-dupai.com提供,如果您还想了解更多的关于股票配资股票股票公司的文章,请点击查看股票配资sh-dupai.com的其它文章

  • 本文地址:8号配资网http://www.sh-dupai.com/two/2257.html
  • 上一篇:山钢集团股票价格,山东钢铁集团股票代码 下一篇:赛增 股票价格表,定增的股票
  • 说点什么吧
    • 全部评论(0
      还没有评论,快来抢沙发吧!