简体   繁体   English

Python中的趋势“预测变量”?

[英]Trend “Predictor” in Python?

I'm currently working with data frames (in pandas ) that have 2 columns: the first column is some numeric quantitative data, like weight, amount of money spent on some day, GPA, etc., and the second column are date values, ie the date on which the corresponding column 1 entry was added on. 我目前正在使用具有2列的数据框(以pandas ):第一列是一些数字定量数据,例如重量,某天花费的金额,GPA等;第二列是日期值,即添加相应的第1列条目的日期。

I was wondering, is there a way to "predict" what the next value after time X is going to be in Python? 我想知道,有没有一种方法可以“预测”在X时间之后Python中的下一个值? Eg if I have 100 weight entries spanning over 2-3 months (not all entries have the same time difference, so 1 entry could be during Day 3, the next Day 5, and the next Day 10), and wanted to "predict" what my next entry after 1 month, is there a way to do that? 例如,如果我有100个体重条目跨越2-3个月(并非所有条目都具有相同的时差,那么1个条目可能在第3天,下一个第5天和下一个第10天),并且想“预测”我在1个月后的下一个录入内容,有什么办法吗?

I think this has something to do with Time Series Analysis, but my statistical background isn't very strong, so I don't know if that's the right approach. 我认为这与“时间序列分析”有关,但是我的统计背景不是很强,所以我不知道这是否是正确的方法。 If it is, how could I apply it to my data frames (ie which packages)? 如果是,该如何将其应用于数据框(即哪些包)? Would there be any significance to the value it potentially returns, or would it be meaningless in the context of what I'm working with? 它可能返回的值是否具有任何意义,或者在我正在使用的上下文中它毫无意义? Thank you. 谢谢。

For predicting time-series data, I feel the best choice would be a LSTM, which is a type of recurrent neural network, which are well suited for time-series regression. 对于预测时序数据,我觉得最好的选择是LSTM,这是一种递归神经网络,非常适合时序回归。

If you don't want to dive deep into the backend of neural networks, I suggest using the Keras library, which is a wrapper for the Tensorflow framework. 如果您不想深入了解神经网络的后端,建议使用Keras库,该库是Tensorflow框架的包装。

Lets say you have a 1-D array of values and you want to predict the next value. 假设您有一个一维值数组,并且想要预测下一个值。 Code in Keras could look like: Keras中的代码可能类似于:

#start off by building the training data, let arr = the list of values
X = []
y = []
for i in range(len(arr)-100-1):
    X.append(arr[i:i+100]) #get prev 100 values for the X
    y.append(arr[i+100])   # predict next value for Y

Since an LSTM takes a 3-D input, we want to reshape our X data to have 3 dimensions: 由于LSTM需要3D输入,因此我们希望将X数据重塑为3维:

import numpy as np
X = np.array(X)
X = X.reshape(len(X), len(X[0]), 1)

Now X is in the form (samples, timesteps, features) 现在,X的形式为(样本,时间步长,特征)

Here we can build a neural network using keras: 在这里,我们可以使用keras构建神经网络:

from keras.models import Sequential
from keras.layers import Dense, LSTM

model = Sequential()
model.add(LSTM(input_shape = (len(X[0], 1)) #input 3-D timeseries data
model.add(Dense(1)) #output 1-D vector of predicted values
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X, y)

And viola, you can use your model to predict the next values in your data 中提琴,您可以使用模型来预测数据中的下一个值

Statsmodels is a python module that provides one of the "most famous" methods in time series forecasting (Arima). Statsmodels是一个python模块,提供时间序列预测(Arima)中“最著名的”方法之一。

An example can be seen in the following link : https://machinelearningmastery.com/arima-for-time-series-forecasting-with-python/ 可以在以下链接中看到一个示例: https : //machinelearningmastery.com/arima-for-time-series-forecasting-with-python/

Other methods for time series forecasting are available in some libraries, like support vector regression, Holt-Winters and Simple Exponential Smoothing. 一些库中提供了其他时间序列预测方法,例如支持向量回归,Holt-Winters和简单指数平滑。

Spark-ts ( https://github.com/sryza/spark-timeseries ) is one time series library that supports Python , and provides methods like Arima, Holt-Winters and Exponential Weighted Moving Average. Spark-ts( https://github.com/sryza/spark-timeseries )是一个支持Python的时间序列库,并提供Arima,Holt-Winters和指数加权移动平均值之类的方法。

Libsvm ( https://github.com/cjlin1/libsvm ) provides support vector regression methods. Libsvm( https://github.com/cjlin1/libsvm )提供了支持向量回归方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM