使用线性回归预测 python 中时间序列数据的 y 值
predicting y values of time series data in python using linear regression
我想使用线性回归预测代表 # of A-type clients/ time
的 Y 值,其中 X 值是时间序列数据。
密码是
df1 = pd.DataFrame({'time': past_time_array, 'A_clients': client_A_array})
x_a = np.arange(len(past_time_array))
fit_A = np.polyfit(x_a, df1['A_clients'], 1)
fit_fn_A = np.poly1d(fit_A)
print df1
print "fitness function = %s" %fit_fn_A
print df1
的结果是
A_clients time
0 0 2018-02-09 14:45:00
1 0 2018-02-09 14:46:00
2 1 2018-02-09 14:47:00
3 4 2018-02-09 14:48:00
4 4 2018-02-09 14:49:00
5 2 2018-02-09 14:50:00
6 2 2018-02-09 14:51:00
7 2 2018-02-09 14:52:00
8 2 2018-02-09 14:53:00
9 4 2018-02-09 14:54:00
10 1 2018-02-09 14:55:00
11 3 2018-02-09 14:56:00
12 4 2018-02-09 14:57:00
13 2 2018-02-09 14:58:00
14 4 2018-02-09 14:59:00
15 3 2018-02-09 15:00:00
16 1 2018-02-09 15:01:00
17 1 2018-02-09 15:02:00
18 0 2018-02-09 15:03:00
19 4 2018-02-09 15:04:00
20 1 2018-02-09 15:05:00
21 1 2018-02-09 15:06:00
22 4 2018-02-09 15:07:00
23 4 2018-02-09 15:08:00
print "fitness function = %s" %fit_fn_A
的结果是
0.0001389 x + 2.213
问题是当我尝试预测像
这样的值时
predicted_ta = fit_fn_A(x_a[10])
print "predicted values = %f"%predicted_ta
它总是给我 2.213
这是 y = mx+c
的 c
值
最佳拟合线如下所示
编辑 1
当我每 2 分钟而不是一次计算 #clietns 时,回归线有一些斜率
值的预测是正确的,但在我计算 number of clients/ minute
之前,该图是线性的,如上所示。因此,当我计算 number of clients/ 2 minutes
的回归线时,适应度函数给出了正确的结果。
你不能在这里应用他的模型。完全没有依赖性。
尝试计算总客户数 (value[x] = sum(value[: x])。通常它非常适合 log() 模型。
我想使用线性回归预测代表 # of A-type clients/ time
的 Y 值,其中 X 值是时间序列数据。
密码是
df1 = pd.DataFrame({'time': past_time_array, 'A_clients': client_A_array})
x_a = np.arange(len(past_time_array))
fit_A = np.polyfit(x_a, df1['A_clients'], 1)
fit_fn_A = np.poly1d(fit_A)
print df1
print "fitness function = %s" %fit_fn_A
print df1
的结果是
A_clients time
0 0 2018-02-09 14:45:00
1 0 2018-02-09 14:46:00
2 1 2018-02-09 14:47:00
3 4 2018-02-09 14:48:00
4 4 2018-02-09 14:49:00
5 2 2018-02-09 14:50:00
6 2 2018-02-09 14:51:00
7 2 2018-02-09 14:52:00
8 2 2018-02-09 14:53:00
9 4 2018-02-09 14:54:00
10 1 2018-02-09 14:55:00
11 3 2018-02-09 14:56:00
12 4 2018-02-09 14:57:00
13 2 2018-02-09 14:58:00
14 4 2018-02-09 14:59:00
15 3 2018-02-09 15:00:00
16 1 2018-02-09 15:01:00
17 1 2018-02-09 15:02:00
18 0 2018-02-09 15:03:00
19 4 2018-02-09 15:04:00
20 1 2018-02-09 15:05:00
21 1 2018-02-09 15:06:00
22 4 2018-02-09 15:07:00
23 4 2018-02-09 15:08:00
print "fitness function = %s" %fit_fn_A
的结果是
0.0001389 x + 2.213
问题是当我尝试预测像
这样的值时predicted_ta = fit_fn_A(x_a[10])
print "predicted values = %f"%predicted_ta
它总是给我 2.213
这是 y = mx+c
c
值
最佳拟合线如下所示
编辑 1
当我每 2 分钟而不是一次计算 #clietns 时,回归线有一些斜率
值的预测是正确的,但在我计算 number of clients/ minute
之前,该图是线性的,如上所示。因此,当我计算 number of clients/ 2 minutes
的回归线时,适应度函数给出了正确的结果。
你不能在这里应用他的模型。完全没有依赖性。
尝试计算总客户数 (value[x] = sum(value[: x])。通常它非常适合 log() 模型。