You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于XGBoost模型的日前小时电价预测问题求助

基于XGBoost模型的日前小时电价预测问题求助

我用2015-2024年的小时级历史数据训练了一个XGBoost电价预测模型,特征包含天气数据、电力消耗、核电及其他可再生能源发电量等,目标变量是电价。模型训练阶段没问题,但现在在预测次日小时级电价时遇到了不少麻烦:

  • 手头数据只到2024年12月7日,比如想预测2025年1月15日的小时电价,不知道该如何正确构造预测所需的特征数据集
  • 当前的预测代码只是把所有特征的最后一个值重复填充到要预测的所有行中,结果完全不准
  • 我试过用最新24小时的特征数据来预测,但预测结果和真实电价相差甚远

以下是我目前使用的预测代码:

# Now add the prediction code
from datetime import datetime, timedelta
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Get the last known data
last_known_data = X_test.copy()
# print(last_known_data.columns)

# Set target date for tomorrow
target_date = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) + timedelta(days=1)

# Create DataFrame with 24 hours
prediction_hours = pd.date_range(target_date, target_date + timedelta(hours=23), freq='H')
future_df = pd.DataFrame(index=prediction_hours)
future_df['Start_date'] = future_df.index

# Convert datetime to unix timestamp (as used in training)
future_df['Start_date'] = future_df['Start_date'].astype(np.int64) // 10**9

# Get the last known values for all features
last_values = last_known_data.iloc[-1].copy()

# Create feature matrix for prediction
for col in X_train.columns:
    if col in ['Start_date', 'End_date_x', 'End_date_y', 'End_date', 'dt_iso']:
        future_df[col] = future_df['Start_date']
    else:
        future_df[col] = last_values[col]

# Add time-based features
future_df['hour'] = future_df.index.hour
future_df['day_of_week'] = future_df.index.dayofweek
future_df['month'] = future_df.index.month

# Handle price lag features using last known prices
last_known_prices = merged_data['Germany/Luxembourg [€/MWh]'].iloc[-72:]

future_df['price_lag_24'] = last_known_prices.iloc[-24:].values
future_df['price_lag_48'] = last_known_prices.iloc[-48:-24].values
future_df['price_lag_72'] = last_known_prices.iloc[-72:-48].values

# Calculate rolling temperature features
for window in [24, 48, 72]:
    future_df[f'temp_roll_{window}'] = future_df['temp'].rolling(window=window, min_periods=1).mean()

# Ensure columns match training data
prediction_features = future_df[X_train.columns]

# Make predictions
predictions = xgb_model.predict(prediction_features)

# Create results DataFrame
results_df = pd.DataFrame({
    'Datetime': prediction_hours,
    'Hour': prediction_hours.hour,
    'Predicted_Price_EUR_MWh': np.round(predictions, 2)
})


# Print predictions
print("\nPredicted Prices for Next 24 Hours:")
print(results_df.to_string(index=False))

备注:内容来源于stack exchange,提问作者Nafees Mohammad Adil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 14:10:28