① 如何使用Python获取股票分时成交数据
可以使用爬虫来爬取数据,在写个处理逻辑进行数据的整理。你可以详细说明下你的需求,要爬取的网站等等。
希望我的回答对你有帮助
② python中两列金融时间序列数据,怎么对时间相同的数据做运算
使用判断的方法 逐个遍历 如果两个时间相等就进行运算
③ 请问用python调用一个较复杂的模型进行参数优化选择
时间序列预测分析就是利用过去一段时间内某事件时间的特征来预测未来一段时间内该事件的特征。这是一类相对比较复杂的预测建模问题,和回归分析模型的预测不同,时间序列模型是依赖于事件发生的先后顺序的,同样大小的值改变顺序后输入模型产生的结果是不同的。
举个栗子:根据过去两年某股票的每天的股价数据推测之后一周的股价变化;根据过去2年某店铺每周想消费人数预测下周来店消费的人数等等!
④ 如何在Python中用LSTM网络进行时间序列预测
时间序列模型
时间序列预测分析就是利用过去一段时间内某事件时间的特征来预测未来一段时间内该事件的特征。这是一类相对比较复杂的预测建模问题,和回归分析模型的预测不同,时间序列模型是依赖于事件发生的先后顺序的,同样大小的值改变顺序后输入模型产生的结果是不同的。
举个栗子:根据过去两年某股票的每天的股价数据推测之后一周的股价变化;根据过去2年某店铺每周想消费人数预测下周来店消费的人数等等
RNN 和 LSTM 模型
时间序列模型最常用最强大的的工具就是递归神经网络(recurrent neural network, RNN)。相比与普通神经网络的各计算结果之间相互独立的特点,RNN的每一次隐含层的计算结果都与当前输入以及上一次的隐含层结果相关。通过这种方法,RNN的计算结果便具备了记忆之前几次结果的特点。
典型的RNN网路结构如下:

4. 模型训练和结果预测
将上述数据集按4:1的比例随机拆分为训练集和验证集,这是为了防止过度拟合。训练模型。然后将数据的X列作为参数导入模型便可得到预测值,与实际的Y值相比便可得到该模型的优劣。
实现代码
时间间隔序列格式化成所需的训练集格式
import pandas as pdimport numpy as npdef create_interval_dataset(dataset, look_back): """ :param dataset: input array of time intervals :param look_back: each training set feature length :return: convert an array of values into a dataset matrix. """ dataX, dataY = [], [] for i in range(len(dataset) - look_back): dataX.append(dataset[i:i+look_back]) dataY.append(dataset[i+look_back]) return np.asarray(dataX), np.asarray(dataY)df = pd.read_csv("path-to-your-time-interval-file") dataset_init = np.asarray(df) # if only 1 columndataX, dataY = create_interval_dataset(dataset, lookback=3) # look back if the training set sequence length这里的输入数据来源是csv文件,如果输入数据是来自数据库的话可以参考这里
LSTM网络结构搭建
import pandas as pdimport numpy as npimport randomfrom keras.models import Sequential, model_from_jsonfrom keras.layers import Dense, LSTM, Dropoutclass NeuralNetwork(): def __init__(self, **kwargs): """ :param **kwargs: output_dim=4: output dimension of LSTM layer; activation_lstm='tanh': activation function for LSTM layers; activation_dense='relu': activation function for Dense layer; activation_last='sigmoid': activation function for last layer; drop_out=0.2: fraction of input units to drop; np_epoch=10, the number of epoches to train the model. epoch is one forward pass and one backward pass of all the training examples; batch_size=32: number of samples per gradient update. The higher the batch size, the more memory space you'll need; loss='mean_square_error': loss function; optimizer='rmsprop' """ self.output_dim = kwargs.get('output_dim', 8) self.activation_lstm = kwargs.get('activation_lstm', 'relu') self.activation_dense = kwargs.get('activation_dense', 'relu') self.activation_last = kwargs.get('activation_last', 'softmax') # softmax for multiple output self.dense_layer = kwargs.get('dense_layer', 2) # at least 2 layers self.lstm_layer = kwargs.get('lstm_layer', 2) self.drop_out = kwargs.get('drop_out', 0.2) self.nb_epoch = kwargs.get('nb_epoch', 10) self.batch_size = kwargs.get('batch_size', 100) self.loss = kwargs.get('loss', 'categorical_crossentropy') self.optimizer = kwargs.get('optimizer', 'rmsprop') def NN_model(self, trainX, trainY, testX, testY): """ :param trainX: training data set :param trainY: expect value of training data :param testX: test data set :param testY: epect value of test data :return: model after training """ print "Training model is LSTM network!" input_dim = trainX[1].shape[1] output_dim = trainY.shape[1] # one-hot label # print predefined parameters of current model: model = Sequential() # applying a LSTM layer with x dim output and y dim input. Use dropout parameter to avoid overfitting model.add(LSTM(output_dim=self.output_dim, input_dim=input_dim, activation=self.activation_lstm, dropout_U=self.drop_out, return_sequences=True)) for i in range(self.lstm_layer-2): model.add(LSTM(output_dim=self.output_dim, input_dim=self.output_dim, activation=self.activation_lstm, dropout_U=self.drop_out, return_sequences=True)) # argument return_sequences should be false in last lstm layer to avoid input dimension incompatibility with dense layer model.add(LSTM(output_dim=self.output_dim, input_dim=self.output_dim, activation=self.activation_lstm, dropout_U=self.drop_out)) for i in range(self.dense_layer-1): model.add(Dense(output_dim=self.output_dim, activation=self.activation_last)) model.add(Dense(output_dim=output_dim, input_dim=self.output_dim, activation=self.activation_last)) # configure the learning process model.compile(loss=self.loss, optimizer=self.optimizer, metrics=['accuracy']) # train the model with fixed number of epoches model.fit(x=trainX, y=trainY, nb_epoch=self.nb_epoch, batch_size=self.batch_size, validation_data=(testX, testY)) # store model to json file model_json = model.to_json() with open(model_path, "w") as json_file: json_file.write(model_json) # store model weights to hdf5 file if model_weight_path: if os.path.exists(model_weight_path): os.remove(model_weight_path) model.save_weights(model_weight_path) # eg: model_weight.h5 return model这里写的只涉及LSTM网络的结构搭建,至于如何把数据处理规范化成网络所需的结构以及把模型预测结果与实际值比较统计的可视化,就需要根据实际情况做调整了。
⑤ 对股票收盘价进行时间序列分析,预测其下一个交易日的收盘价,并与实际收盘价格进行对比
股票投资的分析这么复杂啊,先问问老师有依据这个买股票没,再回答。
⑥ 如何用python 取所有股票一段时间历史数据
各种股票软件,例如通达信、同花顺、大智慧,都可以实时查看股票价格和走势,做一些简单的选股和定量分析,但是如果你想做更复杂的分析,例如回归分析、关联分析等就有点捉襟见肘,所以最好能够获取股票历史及实时数据并存储到数据库,然后再通过其他工具,例如SPSS、SAS、EXCEL或者其他高级编程语言连接数据库获取股票数据进行定量分析,这样就能实现更多目的了。
⑦ 怎么用excel对股票收盘价进行时间序列分析
最好附上内容
⑧ python 时间序列模型中forecast和predict的区别
举一个例子吧,比如月度的数据,就是周期为12,它有季节影响。 先对其1阶12步差分,通过看acf pac f看是简单加法模型,还是乘法季节模型 如果是乘法模型那就要对季节部分模拟arima模型 季节部分的arima是以周期位置的acf pacf
⑨ 这是用股票收盘价形成的时间序列数据线性回归模型,求大神帮忙进行回归诊断!
还诊断啥 你看看那R-squared,这模型能用吗 然后回归系数也没有通过显着性检验
⑩ 如何用python获取股票数据
在Python的QSTK中,是通过s_datapath变量,定义相应股票数据所在的文件夹。一般可以通过QSDATA这个环境变量来设置对应的数据文件夹。具体的股票数据来源,例如沪深、港股等市场,你可以使用免费的WDZ程序输出相应日线、5分钟数据到s_datapath变量所指定的文件夹中。然后可使用Python的QSTK中,qstkutil.DataAccess进行数据访问。