回归预测实战 #

项目概述 #

text
┌─────────────────────────────────────────────────────────────┐
│                    回归预测流程                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  1. 数据准备                                                │
│     ├── 数据加载                                           │
│     ├── 数据清洗                                           │
│     └── 特征工程                                           │
│                                                             │
│  2. 数据预处理                                              │
│     ├── 归一化/标准化                                      │
│     ├── 特征选择                                           │
│     └── 数据划分                                           │
│                                                             │
│  3. 模型构建                                                │
│     ├── 全连接网络                                         │
│     └── 输出层设计                                         │
│                                                             │
│  4. 模型训练与评估                                          │
│                                                             │
└─────────────────────────────────────────────────────────────┘

波士顿房价预测 #

数据准备 #

python
import keras
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

data = fetch_california_housing()
X, y = data.data, data.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

print(f'训练集: {X_train.shape}')
print(f'测试集: {X_test.shape}')

模型构建 #

python
import keras

model = keras.Sequential([
    keras.layers.Dense(64, activation='relu', input_shape=(X_train.shape[1],)),
    keras.layers.BatchNormalization(),
    keras.layers.Dropout(0.2),
    keras.layers.Dense(32, activation='relu'),
    keras.layers.BatchNormalization(),
    keras.layers.Dropout(0.2),
    keras.layers.Dense(16, activation='relu'),
    keras.layers.Dense(1)
])

model.compile(
    optimizer=keras.optimizers.Adam(learning_rate=0.001),
    loss='mse',
    metrics=['mae']
)

model.summary()

模型训练 #

python
callbacks = [
    keras.callbacks.EarlyStopping(
        monitor='val_loss',
        patience=20,
        restore_best_weights=True
    ),
    keras.callbacks.ReduceLROnPlateau(
        monitor='val_loss',
        factor=0.5,
        patience=10,
        min_lr=1e-6
    )
]

history = model.fit(
    X_train, y_train,
    validation_split=0.2,
    epochs=200,
    batch_size=32,
    callbacks=callbacks,
    verbose=1
)

模型评估 #

python
test_loss, test_mae = model.evaluate(X_test, y_test, verbose=0)
print(f'测试 MSE: {test_loss:.4f}')
print(f'测试 MAE: {test_mae:.4f}')

predictions = model.predict(X_test)

import matplotlib.pyplot as plt

plt.figure(figsize=(10, 6))
plt.scatter(y_test, predictions, alpha=0.5)
plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'r--')
plt.xlabel('True Values')
plt.ylabel('Predictions')
plt.title('True vs Predicted Values')
plt.show()

时间序列预测 #

数据准备 #

python
import numpy as np
import keras

def create_sequences(data, seq_length):
    X, y = [], []
    for i in range(len(data) - seq_length):
        X.append(data[i:i+seq_length])
        y.append(data[i+seq_length])
    return np.array(X), np.array(y)

t = np.arange(0, 1000, 0.1)
data = np.sin(t) + 0.1 * np.random.randn(len(t))

seq_length = 50
X, y = create_sequences(data, seq_length)

train_size = int(len(X) * 0.8)
X_train, X_test = X[:train_size], X[train_size:]
y_train, y_test = y[:train_size], y[train_size:]

X_train = X_train.reshape(-1, seq_length, 1)
X_test = X_test.reshape(-1, seq_length, 1)

LSTM 模型 #

python
import keras

model = keras.Sequential([
    keras.layers.LSTM(64, return_sequences=True, input_shape=(seq_length, 1)),
    keras.layers.Dropout(0.2),
    keras.layers.LSTM(32),
    keras.layers.Dropout(0.2),
    keras.layers.Dense(16, activation='relu'),
    keras.layers.Dense(1)
])

model.compile(
    optimizer='adam',
    loss='mse',
    metrics=['mae']
)

history = model.fit(
    X_train, y_train,
    validation_split=0.2,
    epochs=50,
    batch_size=32,
    callbacks=[
        keras.callbacks.EarlyStopping(patience=10, restore_best_weights=True)
    ]
)

预测与可视化 #

python
predictions = model.predict(X_test)

import matplotlib.pyplot as plt

plt.figure(figsize=(15, 5))
plt.plot(y_test, label='True')
plt.plot(predictions, label='Predicted')
plt.xlabel('Time Step')
plt.ylabel('Value')
plt.title('Time Series Prediction')
plt.legend()
plt.show()

多变量回归 #

python
import keras
import numpy as np

X_train = np.random.randn(1000, 10)
y_train = np.random.randn(1000, 3)

model = keras.Sequential([
    keras.layers.Dense(64, activation='relu', input_shape=(10,)),
    keras.layers.Dense(32, activation='relu'),
    keras.layers.Dense(3)
])

model.compile(
    optimizer='adam',
    loss='mse',
    metrics=['mae']
)

model.fit(X_train, y_train, epochs=50, batch_size=32, validation_split=0.2)

下一步 #

恭喜你完成了 Keras 深度学习完全指南的学习!现在你已经掌握了从基础到高级的 Keras 技能,可以开始构建自己的深度学习项目了!

最后更新:2026-04-04