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

Python构建3D数据查询表实现控制器温度、速度、占空比参数插值查询

实现方案

你要做的是结构化三维网格的线性插值,用scipy自带的插值工具可以直接实现,不需要手动写矩阵计算逻辑。

前置依赖

先安装需要的库:

pip install numpy scipy

代码实现

1. 数据结构整理

你手上的多组二维表刚好对应三维结构化网格,先统一整理为以下格式:

  • temp_points:所有温度采样点组成的一维升序数组
  • duty_points:占空比采样点组成的一维升序数组,所有温度下的占空比采样规则必须统一
  • speed_points:速度采样点组成的一维升序数组,所有温度下的速度采样规则必须统一
  • ref_values:三维数组,维度为(温度点数量, 占空比点数量, 速度点数量),每个位置对应三个维度采样点组合下的参考输出值

2. 完整代码示例

import numpy as np
from scipy.interpolate import RegularGridInterpolator

# ---------------------- 替换为你自己的参考数据 ----------------------
# 维度采样点,必须严格升序排列
temp_points = np.array([-5, 0, 10, 20])
duty_points = np.array([0, 20, 40, 60, 80, 100])
speed_points = np.array([0, 1000, 2000, 3000, 4000, 5000])

# 参考值数组,示例用随机数模拟,按你的实际二维表填充即可
# 比如温度为0℃对应的二维表,填到ref_values[1, :, :]
ref_values = np.random.rand(len(temp_points), len(duty_points), len(speed_points))
# -------------------------------------------------------------------

# 初始化插值器,超出采样范围时自动取边界值
interpolator = RegularGridInterpolator(
    points=(temp_points, duty_points, speed_points),
    values=ref_values,
    method="linear",
    bounds_error=False,
    fill_value=None
)

def my_function(temperature: float, speed: float, duty: float) -> float:
    """输入工况参数,返回插值后的预期参考值"""
    return interpolator([temperature, duty, speed])[0]

使用示例

# 单条查询
expected = my_function(temperature=5, speed=2500, duty=30)
diff = 实际运行值 - expected # 直接计算差值

# 批量处理日志(性能更高,不需要逐行循环)
log_temps = 日志温度数组
log_speeds = 日志速度数组
log_dutys = 日志占空比数组
input_points = np.stack([log_temps, log_dutys, log_speeds], axis=1)
batch_expected = interpolator(input_points)

注意事项

  • 如果不同温度下的占空比/速度采样点不统一,需要先手动对齐到相同的采样网格再填充ref_values
  • 若需要对超出采样范围的输入做报错处理,把bounds_error改为True即可
  • 插值器初始化只需要执行一次,批量处理时不要放到循环内重复初始化

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 11:09:02