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

如何使用Scikit-learn对双变量二维参考值矩阵执行非线性回归

双变量非线性回归实现指南(基于Scikit-learn)

你提供的原始映射参考如下:

A/B 1000    1100    1200    1300    1400    1500    1600    1700    1800    1900    2000
0   8.78    8.21    7.64    7.07    6.50    5.92    5.35    4.78    4.21    3.63    3.06
5   8.06    7.56    7.07    6.58    6.08    5.59    5.10    4.60    4.11    3.62    3.12
10  7.33    6.91    6.50    6.09    5.67    5.26    4.84    4.43    4.01    3.60    3.19
15  6.60    6.27    5.93    5.59    5.26    4.92    4.59    4.25    3.92    3.58    3.25
20  5.87    5.62    5.36    5.10    4.85    4.59    4.33    4.08    3.82    3.57    3.31
25  5.14    4.97    4.79    4.61    4.44    4.26    4.08    3.90    3.73    3.55    3.37
30  4.42    4.32    4.22    4.12    4.02    3.93    3.83    3.73    3.63    3.53    3.43
35  3.80    3.78    3.75    3.72    3.70    3.67    3.64    3.62    3.59    3.56    3.54
40  2.86    2.93    2.99    3.05    3.12    3.18    3.24    3.31    3.37    3.43    3.50
45  2.08    2.24    2.39    2.54    2.70    2.85    3.00    3.16    3.31    3.46    3.62
50  1.64    1.84    2.05    2.26    2.46    2.67    2.88    3.08    3.29    3.50    3.70
55  1.55    1.77    1.98    2.19    2.41    2.62    2.83    3.05    3.26    3.47    3.69
60  2.09    2.22    2.35    2.48    2.61    2.74    2.87    3.00    3.13    3.26    3.39
65  3.12    3.08    3.05    3.02    2.98    2.95    2.92    2.88    2.85    2.82    2.78
70  3.50    3.39    3.28    3.17    3.06    2.95    2.84    2.73    2.62    2.51    2.40
75  3.42    3.32    3.21    3.10    3.00    2.89    2.78    2.68    2.57    2.46    2.36
80  3.68    3.55    3.43    3.31    3.18    3.06    2.94    2.81    2.69    2.57    2.44
85  3.43    3.35    3.28    3.21    3.13    3.06    2.99    2.91    2.84    2.77    2.69
90  3.43    3.35    3.28    3.21    3.13    3.06    2.99    2.91    2.84    2.77    2.69
95  3.43    3.35    3.28    3.21    3.13    3.06    2.99    2.91    2.84    2.77    2.69
100 3.43    3.35    3.28    3.21    3.13    3.06    2.99    2.91    2.84    2.77    2.69

对应数据3D曲面:
数据3D曲面图


步骤1:数据结构化处理

首先需要将二维表格转换为Scikit-learn支持的(样本数, 特征数)格式输入,代码示例如下:

import numpy as np

# 定义特征取值
B_values = [1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000]
A_values = [0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100]
output_matrix = np.array([
    [8.78,8.21,7.64,7.07,6.50,5.92,5.35,4.78,4.21,3.63,3.06],
    [8.06,7.56,7.07,6.58,6.08,5.59,5.10,4.60,4.11,3.62,3.12],
    [7.33,6.91,6.50,6.09,5.67,5.26,4.84,4.43,4.01,3.60,3.19],
    [6.60,6.27,5.93,5.59,5.26,4.92,4.59,4.25,3.92,3.58,3.25],
    [5.87,5.62,5.36,5.10,4.85,4.59,4.33,4.08,3.82,3.57,3.31],
    [5.14,4.97,4.79,4.61,4.44,4.26,4.08,3.90,3.73,3.55,3.37],
    [4.42,4.32,4.22,4.12,4.02,3.93,3.83,3.73,3.63,3.53,3.43],
    [3.80,3.78,3.75,3.72,3.70,3.67,3.64,3.62,3.59,3.56,3.54],
    [2.86,2.93,2.99,3.05,3.12,3.18,3.24,3.31,3.37,3.43,3.50],
    [2.08,2.24,2.39,2.54,2.70,2.85,3.00,3.16,3.31,3.46,3.62],
    [1.64,1.84,2.05,2.26,2.46,2.67,2.88,3.08,3.29,3.50,3.70],
    [1.55,1.77,1.98,2.19,2.41,2.62,2.83,3.05,3.26,3.47,3.69],
    [2.09,2.22,2.35,2.48,2.61,2.74,2.87,3.00,3.13,3.26,3.39],
    [3.12,3.08,3.05,3.02,2.98,2.95,2.92,2.88,2.85,2.82,2.78],
    [3.50,3.39,3.28,3.17,3.06,2.95,2.84,2.73,2.62,2.51,2.40],
    [3.42,3.32,3.21,3.10,3.00,2.89,2.78,2.68,2.57,2.46,2.36],
    [3.68,3.55,3.43,3.31,3.18,3.06,2.94,2.81,2.69,2.57,2.44],
    [3.43,3.35,3.28,3.21,3.13,3.06,2.99,2.91,2.84,2.77,2.69],
    [3.43,3.35,3.28,3.21,3.13,3.06,2.99,2.91,2.84,2.77,2.69],
    [3.43,3.35,3.28,3.21,3.13,3.06,2.99,2.91,2.84,2.77,2.69],
    [3.43,3.35,3.28,3.21,3.13,3.06,2.99,2.91,2.84,2.77,2.69]
])

# 转换为标准数据集格式:X为[A,B]二维特征,y为对应输出
X = []
y = []
for i, A in enumerate(A_values):
    for j, B in enumerate(B_values):
        X.append([A, B])
        y.append(output_matrix[i][j])
X = np.array(X)
y = np.array(y)

步骤2:带显式方程的非线性回归(可解释方案)

如果需要得到明确的双变量函数表达式,推荐使用多项式特征转换+线性回归的组合方案,可完美匹配二次、高次函数拟合需求:

from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
from sklearn.metrics import r2_score

# 构建二次多项式回归Pipeline,可调整degree参数测试更高次拟合效果
model = make_pipeline(
    PolynomialFeatures(degree=2, include_bias=False),
    LinearRegression()
)

# 拟合模型
model.fit(X, y)

# 评估拟合效果,当前数据集二次拟合R²可达0.99以上
y_pred = model.predict(X)
print(f"拟合R²得分: {r2_score(y, y_pred):.4f}")

# 输出显式双变量方程
poly = model.named_steps['polynomialfeatures']
feature_names = poly.get_feature_names_out(['A', 'B'])
coef = model.named_steps['linearregression'].coef_
intercept = model.named_steps['linearregression'].intercept_

print("拟合得到的双变量方程:")
equation = f"y = {intercept:.4f}"
for name, c in zip(feature_names, coef):
    equation += f" + {c:.6f}*{name}"
print(equation)

如果需要加入对数特征拟合,可在特征转换环节新增对数项:

from sklearn.preprocessing import FunctionTransformer
from sklearn.compose import ColumnTransformer

# 新增log(A+1)、log(B)特征,避免A=0时对数无意义
preprocessor = ColumnTransformer(
    transformers=[
        ('original', 'passthrough', [0,1]),
        ('log', FunctionTransformer(np.log), [0,1])
    ]
)
# 组合为新的拟合Pipeline
model = make_pipeline(
    preprocessor,
    PolynomialFeatures(degree=2, include_bias=False),
    LinearRegression()
)

步骤3:无显式方程的高精度拟合方案

如果不需要得到明确的函数式,仅需要高精度的输入输出映射,可直接使用内置非线性回归器:

  • 高斯过程回归:适配小样本平滑曲面拟合,和当前3D曲面匹配度极高
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel

kernel = ConstantKernel() * RBF()
model = GaussianProcessRegressor(kernel=kernel, random_state=42)
model.fit(X, y)
# 预测调用方式:model.predict([[A取值, B取值]])
  • 随机森林回归:鲁棒性强,无需特征预处理,泛化能力好
from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor(n_estimators=100, random_state=42)
model.fit(X, y)

内容的提问来源于stack exchange,提问作者ortunoa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 13:06:05