python - 为什么我的预测值几乎相同(并且与平均值相似)?

标签 python machine-learning scikit-learn time-series linear-regression

我已从包含 17 年来每小时记录的特定城市 911 起火灾报警的数据集中删除了趋势和季节性。然后,我用线性回归器拟合它,并尝试预测即将到来的 24 小时期间的值。然而,我的 R^2 值通常接近 0(通常为负值),并且我的预测值彼此之间都在万分之一(或更小)以内,因此绘制时,它本质上看起来像一条粗略反射(reflect)平均值的水平线。

我做错了什么?

这是我的代码:

from datetime import timedelta
def run_regression(df, dependent, label):
    cut_datetime = df[dependent].max()-timedelta(hours=26) #24 hour lag plus 4 hours to predict

    train = df[df[dependent] < cut_datetime][['julian_datetime', label]].dropna(how='any') #train == data before cut_datetime
    test = df[df[dependent] >= cut_datetime][['julian_datetime', label]].dropna(how='any') #test == data after cut_datetime

    regress = sklearn.linear_model.LinearRegression().fit(
                                              X = train[['julian_datetime']],
                                              y = train[label])

    test['predicted_value'] = regress.predict(
                                              X = test[['julian_datetime']])

    #Plots
    (test[label] - test['predicted_value']).plot()
    test[[label, 'predicted_value']].plot()

    #Metrics
    print('MSE: ', sklearn.metrics.mean_squared_error(test[label], test['predicted_value']))
    print('R^2: ', sklearn.metrics.r2_score(test[label], test['predicted_value']))
    print('Sample of predicted values: ', '\n', test['predicted_value'][:10])

run_regression(exp_model_df, 'incident_hour', 'label')

incident_hour --> 函数开头引用的 julian_date 的日期时间格式

这是数据集的示例:

incident_hour   julian_datetime     label
0   2003-11-07 09:00:00     2452950.87500000    6.696136
1   2003-11-07 10:00:00     2452950.91666667    -5.293884
2   2003-11-07 11:00:00     2452950.95833333    5.679681
3   2003-11-07 12:00:00     2452951.00000000    4.411278
4   2003-11-07 13:00:00     2452951.04166667    5.837476
5   2003-11-07 14:00:00     2452951.08333333    6.469543
6   2003-11-07 15:00:00     2452951.12500000    2.191286
7   2003-11-07 16:00:00     2452951.16666667    0.347877
8   2003-11-07 17:00:00     2452951.20833333    0.151539
9   2003-11-07 18:00:00     2452951.25000000    5.925230
10  2003-11-07 19:00:00     2452951.29166667    8.563340
11  2003-11-07 20:00:00     2452951.33333333    3.151843
12  2003-11-07 21:00:00     2452951.37500000    3.751080
13  2003-11-07 22:00:00     2452951.41666667    5.476664
14  2003-11-07 23:00:00     2452951.45833333    0.146253
15  2003-11-08 00:00:00     2452951.50000000    2.879449
16  2003-11-08 01:00:00     2452951.54166667    0.712886
17  2003-11-08 02:00:00     2452951.58333333    6.118765
18  2003-11-08 03:00:00     2452951.62500000    6.052857
19  2003-11-08 04:00:00     2452951.66666667    0.892937
20  2003-11-08 05:00:00     2452951.70833333    -3.009876
21  2003-11-08 06:00:00     2452951.75000000    -3.525916
22  2003-11-08 07:00:00     2452951.79166667    -0.076345
23  2003-11-08 08:00:00     2452951.83333333    -3.236072
24  2003-11-08 09:00:00     2452951.87500000    -2.855910
25  2003-11-08 10:00:00     2452951.91666667    3.599330
26  2003-11-08 11:00:00     2452951.95833333    6.845144
27  2003-11-08 12:00:00     2452952.00000000    6.764351
28  2003-11-08 13:00:00     2452952.04166667    -1.896929
29  2003-11-08 14:00:00     2452952.08333333    0.370614
30  2003-11-08 15:00:00     2452952.12500000    4.899800
31  2003-11-08 16:00:00     2452952.16666667    7.245627
32  2003-11-08 17:00:00     2452952.20833333    1.559531
33  2003-11-08 18:00:00     2452952.25000000    8.437391
34  2003-11-08 19:00:00     2452952.29166667    4.957201
35  2003-11-08 20:00:00     2452952.33333333    1.349833
36  2003-11-08 21:00:00     2452952.37500000    6.257467
37  2003-11-08 22:00:00     2452952.41666667    -1.221531
38  2003-11-08 23:00:00     2452952.45833333    0.552749
39  2003-11-09 00:00:00     2452952.50000000    -0.917920
40  2003-11-09 01:00:00     2452952.54166667    -4.394944
41  2003-11-09 02:00:00     2452952.58333333    -2.238189
42  2003-11-09 03:00:00     2452952.62500000    -1.062656
43  2003-11-09 04:00:00     2452952.66666667    3.813087
44  2003-11-09 05:00:00     2452952.70833333    -4.540094
45  2003-11-09 06:00:00     2452952.75000000    2.680210
46  2003-11-09 07:00:00     2452952.79166667    4.581881
47  2003-11-09 08:00:00     2452952.83333333    3.803750
48  2003-11-09 09:00:00     2452952.87500000    6.590574
49  2003-11-09 10:00:00     2452952.91666667    8.227202

这是结果图:

enter image description here

最佳答案

您对时间序列使用了完全错误的方法。

让我们看看线性回归在做什么:

julian_datetime     label
0   2.452951e+06    6.696136
1   2.452951e+06    -5.293884
2   2.452951e+06    5.679681
3   2.452951e+06    4.411278
4   2.452951e+06    5.837476

所以基本上他会找到函数 f(x) = a*x +b 和你的 x= julian_datetimef(x)= label。他将最小化标签预测的损失,所以问题是对于所有数字2.45,...,2.45他会找到另一个数字。但在时间序列中,您必须处理数据流!现在根本不包括时间。

将其添加到时间序列(这不是最好的方法)的一个示例是将所有前 2 个值作为特征添加到其中,使其看起来像:

julian_datetime     julian_datetime-1 julian_datetime-2 label
0   2.452951e+06                                         6.696136
1   2.452951e+06    2.452951e+06                         -5.293884
2   2.452951e+06    2.452951e+06     2.452951e+06       5.679681
3   2.452951e+06    2.452951e+06     2.452951e+06       4.411278

顺便说一句,为什么朱利安日期时间总是相同的值?啊,更糟糕了!您必须使用标签列作为先前的值,以便它看起来像:

   y          y-1        y-2
0 6.696136
1 -5.293884  6.696136
2 5.679681   -5.293884           6.696136
3

那么你的三角化是y-1, y-2,你的预测是y

关于python - 为什么我的预测值几乎相同(并且与平均值相似)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59958414/

相关文章:

python - 计算时间序列中的峰值

r - 具有异构变量的客户集的聚类

c++ - 具有高维标签的机器学习代码 dlib

python - 使用 k-Means 聚类尝试识别 2D 离群值显示根本没有离群值(而不是一个)

python - 精度始终为零

更新嵌套 JSON 的 Python PUT 请求失败

python - 为什么 Python 深受动画行业从业人员的喜爱?

r - R 中 SVM 调优错误

python - 为什么我会收到数据转换警告?

python-3.x - 如何在 TfidfVectorizer 中计算词频?