gpt4 book ai didi

python - 线性回归图没有给我有意义的可视化

转载 作者:行者123 更新时间:2023-12-04 09:24:22 29 4
gpt4 key购买 nike

我正在使用一些时间序列功耗数据并尝试对其进行线性回归分析。
数据具有以下列:
日期,丹麦_消费,德国_消费,捷克_消费,奥地利_消费。
它是频率为小时的时间序列数据。
但是,每列都有 NaN 的值
我的目标是创建一个线性回归模型
对没有空值的数据子集进行训练和测试,然后尝试预测丹麦消费列的值,例如,当前具有 NaN 值的值。
我计划使用一个国家/地区消费列以及序号值中的日期作为我的培训/测试功能,以尝试预测第二个国家/地区的消费值。
这是数据的示例。

Date                   Denmark    Germany    Czech   Austria

2018-01-01 00:00:00 1607.0 42303.0 5520 6234.0

2018-01-01 01:00:00 1566.0 41108.0 5495 6060.0

2018-01-01 02:00:00 1460.0 40554.0 5461 5872.0

2018-01-01 03:00:00 1424.0 38533.0 5302 5564.0

2018-01-01 04:00:00 1380.0 38494.0 5258 5331.0
我做了几件事。
  • 我删除了带有任何空值的行来创建我的训练和测试数据集。
  • 我将日期列设置为数据框索引。
  • 我将数据从每小时上采样到每周。我使用了
    默认'mean'聚合函数。
  • 我将日期作为一列重新添加到训练和测试数据中,并将其转换为序数值。
  • 因为各个消费值都是高度相关的,所以我只对X_train和X_test数据集使用了德国消费列

  • 我使用 sklearn 创建了一个线性回归模型,并使用德国消费和序数日期作为我的“X”和丹麦消费作为我的“Y”来拟合数据。
    我试图通过散点图和线来绘制输出,但我得到的图形如下所示:
    enter image description here
    为什么我的情节看起来像有人在上面乱涂乱画?我期待着某种形式的单行。
    这是我的 x_train 数据集的示例
                            Germany    Date
    consumption
    Date
    2018-07-08 44394.125000 736883
    2019-01-16 66148.125000 737075
    2019-08-03 45718.083333 737274
    2019-06-09 41955.250000 737219
    2020-03-04 61843.958333 737488
    这是我的 y_train 数据集的示例。
    Date
    2018-01-01 1511.083333
    2018-01-02 1698.625000
    2018-01-03 1781.291667
    2018-01-04 1793.458333
    2018-01-05 1796.875000
    Name: Denmark_consumption, dtype: float64
    这是实际的相关代码。
    lin_model = LinearRegression()
    lin_model.fit(X_train,y_train)
    y_pred = lin_model.predict(X_test)
    plt.scatter(X_test['Date'].map(dt.datetime.fromordinal),y_pred,color='black')
    plt.plot(X_test['Date'],y_pred)
    系数、R 平方和均方误差为:
    Coefficients: 
    [0.01941453 0.01574128]
    Mean squared error: 14735.12
    Coefficient of determination: 0.51
    有人可以让我知道我做错了什么吗?另外,我的方法准确吗?尝试有意义吗
    并根据第二个国家的消费量 + 日期来预测消费值?
    任何帮助表示赞赏。

    最佳答案

    你的方法很复杂,但可行。就我个人而言,我认为在德国的日期和德国的消费量之间创建线性映射可能更容易,然后尝试通过这种方式对丹麦的消费量进行预测。
    但是,坚持你的方法,你应该记住有两个自变量(德国的日期转换为整数,德国的消费量),丹麦的消费量取决于这两个变量。因此,通过像现在这样在 2D 图中根据预测绘制测试日期,您实际上错过了消费变量。您应该绘制的是德国的日期,以及德国的消费量与丹麦在 3D 平面中的消费量。
    此外,您不应该期望得到一条直线:通过多元线性回归和两个自变量,您可以预测飞机。
    这是我放在一起的一个简短示例,它类似于您可能想要实现的目标。根据需要随意更改日期格式。

    import pandas as pd
    import numpy as np
    import datetime as dt
    from mpl_toolkits.mplot3d import *
    import matplotlib.pyplot as plt
    from matplotlib import cm
    from sklearn.linear_model import LinearRegression

    from pandas.plotting import register_matplotlib_converters
    register_matplotlib_converters()

    # starts 2018/11/02
    df_germany = pd.DataFrame({
    'Germany consumption': [45000, 47000, 48000, 42000, 50000],
    'Date': [737000, 737001, 737002, 737003, 737004]})
    df_germany_test = pd.DataFrame({
    'Germany consumption': [42050, 42000, 57000, 30000, 52000, 53000],
    'Date': [737000, 737001, 737002, 737003, 737004, 737005]})
    df_denmark = pd.DataFrame({
    'Denmark consumption': [1500, 1600, 1700, 1800, 2000]
    })

    X_train = df_germany.to_numpy()
    y_train = df_denmark['Denmark consumption']

    # make X_test the same as X_train to make sure all points are on the plane
    # X_test = df_germany

    # make X_test slightly different
    X_test = df_germany_test

    lin_model = LinearRegression()
    lin_model.fit(X_train,y_train)
    y_pred = lin_model.predict(X_test)

    fig = plt.figure()
    ax = fig.gca(projection='3d')
    # plt.hold(True)

    x_surf=np.linspace(min(X_test['Date'].values), max(X_test['Date'].values), num=20)
    y_surf=np.linspace(min(X_test['Germany consumption'].values), max(X_test['Germany consumption'].values), num=20)
    x_surf, y_surf = np.meshgrid(x_surf, y_surf)
    b0 = lin_model.intercept_
    b1, b2 = lin_model.coef_
    z_surf = b0+ b2*x_surf + b1*y_surf
    ax.plot_surface(x_surf, y_surf, z_surf, cmap=cm.cool, alpha = 0.2) # plot a 3d surface plot

    ax.scatter(X_test['Date'].values, X_test['Germany consumption'].values, y_pred, alpha=1.0)
    plt.show()
    enter image description here

    关于python - 线性回归图没有给我有意义的可视化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63045562/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com