import numpy as np import pandas as pd from scipy.optimize import curve_fit import matplotlib.pyplot as plt # 定义 Logistic 函数 def logistic_model(t, K, a, t0): return K / (1 + np.exp(-a * (t - t0))) # 模拟人口数据 years = np.array([1950, 1951 ,1952 ,1953 ,1954 ,1955 ,1956 ,1957 ,1958 ,1959 ,1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018])# 拟合 Logistic 模型 population = np.array([3583, 3672, 3739, 3821, 3904, 4008, 4102, 4195, 4258, 4287, 4245, 4221, 4333, 4442, 4532, 4632, 4748, 4863, 4996, 5120, 5252, 5345, 5437, 5512, 5567, 5621, 5700, 5776, 5834, 5888, 5938, 5986, 6088, 6134, 6071, 6213, 6269, 6348, 6438, 6535, 6767, 6844, 6911, 6967, 7021, 7066, 7110, 7148, 7182, 7213, 7327, 7355, 7381, 7406, 7433, 7475, 7550, 7625, 7677, 7725, 7866, 7899, 7920, 7939, 7960, 7976, 7999, 8029, 8051])# 提取拟合参数 #拟合Logistic模型 popt, pcov = curve_fit(logistic_model, years, population, p0=[100, 0.05, 2000]) #提取拟合参数 # 计算预测值与残差 predicted_population = logistic_model(years, *popt) residuals = population - predicted_population from sklearn.model_selection import train_test_split X = years.reshape(-1, 1) # 特征 y = residuals # 目标:残差 # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) print("训练特征:", X_train) print("测试特征:", X_test) print("训练标签:", y_train) print("测试标签:", y_test) # 构建神经网络模型 # 编译模型 model.compile(optimizer='adam', loss='mse') # 训练模型 history = model.fit(X_train, y_train, epochs=200, batch_size=4, validation_split=0.2, verbose=0) # 预测残差 nn_residuals = model.predict(X_test).flatten() # 融合 Logistic 和神经网络预测结果 final_prediction = logistic_model(X_test.flatten(), *popt) + nn_residuals # 可视化最终预测结果 import matplotl 给这一部分代码补写适用于python3.13版本的构建神经网络模型的代码
时间: 2025-07-01 19:54:02 浏览: 8
### 残差预测与Logistic模型融合的神经网络实现
在构建神经网络模型时,为了提升模型的表达能力和泛化性能,可以采用残差预测(residual prediction)策略。这种策略的核心思想是让模型学习目标值与基线模型输出之间的残差,从而逐步修正预测结果。此外,将神经网络与逻辑回归(Logistic Regression)模型进行融合,可以在保持模型可解释性的同时,进一步增强其非线性建模能力。
以下是一个适用于 Python 3.13 的神经网络模型构建代码示例,用于残差预测,并与 Logistic 回归模型进行集成:
```python
import torch
import torch.nn as nn
import torch.optim as optim
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# 定义残差预测神经网络模型
class ResidualNet(nn.Module):
def __init__(self, input_dim):
super(ResidualNet, self).__init__()
self.model = nn.Sequential(
nn.Linear(input_dim, 64),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(64, 32),
nn.ReLU(),
nn.Linear(32, 1)
)
def forward(self, x):
return self.model(x)
# 假设已有训练数据 X_train, y_train 和测试数据 X_test, y_test
# 其中 y_train 是二分类标签(0 或 1)
# 训练逻辑回归模型作为基线
log_reg = LogisticRegression()
log_reg.fit(X_train, y_train)
# 获取基线模型的预测概率
baseline_preds = log_reg.predict_proba(X_train)[:, 1]
# 构造残差:真实标签与基线预测之间的差异
residuals = y_train - baseline_preds
# 转换为 PyTorch 张量
X_train_tensor = torch.tensor(X_train.values, dtype=torch.float32)
residuals_tensor = torch.tensor(residuals, dtype=torch.float32).view(-1, 1)
# 初始化神经网络和优化器
model = ResidualNet(input_dim=X_train.shape[1])
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# 训练残差预测模型
for epoch in range(100):
model.train()
optimizer.zero_grad()
outputs = model(X_train_tensor)
loss = criterion(outputs, residuals_tensor)
loss.backward()
optimizer.step()
# 预测残差并加到逻辑回归的预测上
model.eval()
with torch.no_grad():
residual_preds = model(torch.tensor(X_test.values, dtype=torch.float32)).numpy()
# 最终预测 = 基线预测 + 残差预测
final_preds = log_reg.predict_proba(X_test)[:, 1] + residual_preds.flatten()
# 将最终预测转换为类别
final_class_preds = (final_preds > 0.5).astype(int)
# 评估最终模型
accuracy = accuracy_score(y_test, final_class_preds)
print(f"Final Model Accuracy: {accuracy}")
```
上述代码首先使用 Logistic 回归生成初始预测,然后训练一个神经网络来预测这些预测与真实标签之间的残差。最终的预测结果由基线预测和残差预测共同构成,从而提升整体模型的精度。
该方法借鉴了自动特征交互学习的思想[^1],通过引入额外的神经网络模块来捕捉数据中的复杂模式,同时保留逻辑回归模型的可解释性。
---
阅读全文
相关推荐



















