气温预测--单特征

一、了解

数据集:气温数据,多种指标

任务目标:预测未来某一时间点的气温/未来某一时间片段的气温

数据集下载链接如下:jena_climate_2009_2016

二、实战--单特征

第一步,加载数据集

df = pd.read_csv('./datasets/jena_climate_2009_2016.csv')
df.head()

此数据集的数据,是每个十分钟一条数据,每个数据有多个指标

第二步,构建序列数据函数

'''
dataset表示输入的数据集,里面可以是一个或者多个列特征

history_size表示时间窗口的大小

indices = range(i-history_size,i)表示窗口序列索引,i表示每个窗口的起始位置

target_size 表示要预测的结果是窗口的第几个时间点,0则表示下一个时间点的预测结果,取其当做标签
'''
def univariate_data(dataset, start_index, end_index,
                    history_size,target_size):
    data = []
    labels = []
    
    start_index = start_index + history_size
    if end_index is None:
        end_index = len(dataset) - target_size
    
    for i in range(start_index, end_index):
        indices = range(i-history_size, i)
        #Reshape data from (history_size,) to (history_size,1)
        data.append(np.reshape(dataset[indices], (history_size, 1)))
        labels.append(dataset[i+target_size])
    return np.array(data), np.array(labels)

第三步,取出数据DF

#前30W个样本数据当训练集,剩下的当验证集
TRAIN_SPLIT = 300000

#取出单特征数据df, 只选一个温度特征
uni_data = df['T (degC)']
uni_data.index = df['Date Time']
uni_data.head()

#展示当前特征
uni_data.plot(subplots=True)

#得到温度数据
uni_data = uni_data.values
print( uni_data.shape )

#数据预处理,归一化
uni_train_mean = uni_data[:TRAIN_SPLIT].mean()
uni_train_std = uni_data[:TRAIN_SPLIT].std()
uni_data = (uni_data - uni_train_mean)/uni_train_std

#窗口大小为20条数据,预测一个时刻的气温
univariate_past_history = 20
univariate_future_target = 0
x_train_uni, y_train_uni = univariate_data(uni_data, 0, TRAIN_SPLIT,
                           univariate_past_history,
                           univariate_future_target)
x_val_uni, y_val_uni = univariate_data(uni_data, TRAIN_SPLIT,
                                      None, univariate_past_history,
                                      univariate_future_target)

#维度
print( x_train_uni.shape )
print( y_train_uni.shape )
print( x_val_uni.shape )
print( y_val_uni.shape )

第四步,时间序列数据展示

# 创建 以当前天为0,之前的 20天[-20,-19,,,,-1]
def create_time_steps(length):
    time_steps = []
    for i in range(-length, 0, 1):
        time_steps.append(i)
    return time_steps

def show_plot(plot_data, delta, title):
    labels = ['History', 'True Future', 'Model Prediction']
    marker = ['.-', 'rx', 'go']
    time_steps = create_time_steps(plot_data[0].shape[0])
    if delta:
        future = delta
    else:
        future = 0
        
    plt.title(title)
    for i, x in enumerate(plot_data):
        if i:
            plt.plot(future, plot_data[i], marker[i],
                    markersize=10, label=labels[i])
        else:
            plt.plot(time_steps, plot_data[i].flatten(),
                    marker[i], label=labels[i])
    plt.legend()
    plt.xlim([time_steps[0], (future+5)*2])
    plt.xlabel('Time-Step')
    return plt

show_plot([x_train_uni[0], y_train_uni[0]], 0, 'Sample Example')

第五步,用from_tensor_slices创建Tensor类型训练数据和验证数据

BATCH_SIZE = 256
BUFFER_SIZE = 10000

train_univariate = tf.data.Dataset.from_tensor_slices((x_train_uni, y_train_uni))
train_univariate = train_univariate.cache().shuffle(BUFFER_SIZE).batch(BATCH_SIZE).repeat()

val_univariate = tf.data.Dataset.from_tensor_slices((x_val_uni, y_val_uni))
val_univariate = val_univariate.batch(BATCH_SIZE).repeat()

第六步,用 LSTM模型 预测气温

simple_lstm_model = tf.keras.models.Sequential([
    keras.layers.LSTM(8, input_shape=x_train_uni.shape[-2:]),  #输入维度为 (20,1)
    keras.layers.Dense(1)
])

simple_lstm_model.compile(optimizer=tf.optimizers.Adam(),
                         loss='mae')

simple_lstm_model.summary()

运行结果如下:

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_1 (LSTM)                (None, 8)                 320       
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 9         
=================================================================
Total params: 329
Trainable params: 329
Non-trainable params: 0

第七步,先预测一下测试结果

#得到一个batch的测试结果
for x, y in val_univariate.take(1):
    print(simple_lstm_model.predict(x).shape)

第八步,训练模型

# 为了训练能快点,一个epoch就只训练200次
EVALUATION_INTERVAL = 200
EPOCHS = 10

simple_lstm_model.fit(train_univariate, 
                     epochs=EPOCHS,
                     steps_per_epoch=EVALUATION_INTERVAL,
                     validation_data=val_univariate,
                     validation_steps=50)

第九步,用训练好的模型预测结果

#展示其中部分数据的预测结果
# x 是 256个样例,即一个batch
for x, y in val_univariate.take(3):
    plot = show_plot([x[0].numpy(), y[0].numpy(),
                     simple_lstm_model.predict(x)[0]],
                    0, 'Simple LSTM model')
    plot.show()

后面,将用多特征来训练网络模型

### 数据预处理 对于气温预测任务,数据预处理是一个至关重要的环节。考虑到气温特征数据的空间依赖性以及时间上的连续特性,在准备数据时应充分考虑这些因素。具体而言: - **清洗与标准化**:去除异常值并进行必要的缺失值填补;对不同尺度的数据实施标准化或归一化操作以确保模型学习过程中的稳定性[^2]。 - **滑动窗口创建**:由于温度变化存在一定的周期性和趋势性,可以采用滑动窗口的方式构造输入样本,即选取一段时间内的历史观测作为当前时刻预测的基础。 ```python import numpy as np from sklearn.preprocessing import StandardScaler def preprocess_data(data, window_size=7): scaler = StandardScaler() scaled_data = scaler.fit_transform(data.reshape(-1, 1)) X, y = [], [] for i in range(len(scaled_data)-window_size): X.append(scaled_data[i:i+window_size]) y.append(scaled_data[i+window_size]) return np.array(X), np.array(y) X_train, y_train = preprocess_data(train_temperature_series) ``` ### 模型构建 为了有效捕捉到气象要素间的复杂关联模式,建议选用融合了卷积层(CNN)和双向长短时记忆元(BiLSTM)架构的混合深度学习框架来进行建模。其中, - **CNN部分负责提取局部空间特征**:通过对相邻时间段内各站点间相互作用的学习来增强表征能力; - **BiLSTM则专注于挖掘长期的历史演变规律**:不仅关注过去的影响因子还能兼顾未来可能发生的状况,从而提供更精准的趋势判断依据。 ```python from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv1D, MaxPooling1D, Bidirectional, LSTM, Dense model = Sequential([ Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(None, 1)), MaxPooling1D(pool_size=2), Bidirectional(LSTM(50, return_sequences=True)), Bidirectional(LSTM(50)), Dense(1) ]) model.compile(optimizer='adam', loss='mse') ``` ### 训练策略 在实际应用过程中,推荐采取如下措施保障训练效果: - 使用交叉验证技术合理分配训练集、验证集及测试集的比例,以便更好地评估超参数调整带来的影响,并最终确定最优配置方案[^1]。 - 应用早停法(Early Stopping),当验证损失不再下降时提前终止迭代更新,防止过拟合现象的发生。 - 定期保存检查点(checkpoint),记录下每次达到最低误差水平下的权重状态,便于后续加载继续调优或者部署上线服务。 ```python history = model.fit( X_train, y_train, epochs=50, batch_size=32, validation_split=0.2, callbacks=[ EarlyStopping(monitor='val_loss', patience=10), ModelCheckpoint('best_model.h5', save_best_only=True) ] ) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值