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

如何在0.1-40区间以0.001步长对温度与比重数据进行样条插值?

解决方法

1. 生成目标插值温度序列

直接用np.arange指定起始、终止(包含40需设为40.001)和步长,比linspace更直观:

new_temp = np.arange(0.1, 40.001, 0.001)

如果偏好linspace,需计算精确点数:(40 - 0.1)/0.001 + 1 = 39901,代码如下:

new_temp = np.linspace(0.1, 40, 39901)

2. 构建样条插值函数并计算结果

无需将DataFrame列转成list,直接传入即可生成插值函数,再计算对应比重值:

from scipy.interpolate import CubicSpline

# 构建三次样条插值函数
cs = CubicSpline(sg_temp['temp'], sg_temp['sg'])
# 计算插值后的比重数据
new_sg = cs(new_temp)

3. 整合插值数据与原始数据

把插值结果转成DataFrame,和原始数据合并后去重、排序:

# 插值数据转为DataFrame
interpolated_df = pd.DataFrame({'temp': new_temp, 'sg': new_sg})
# 合并原始数据与插值数据
combined_df = pd.concat([sg_temp, interpolated_df], ignore_index=True)
# 去重(避免原始点重复)并按温度排序
combined_df = combined_df.drop_duplicates(subset='temp').sort_values('temp').reset_index(drop=True)

你的代码问题点

  • 起始温度错误:需求是从0.1开始,你写了0,不符合插值范围要求
  • 步长逻辑错误:np.linspace第三个参数是总点数,不是步长,40*1000得到的步长并非精确0.001
  • 缺少结果计算:仅定义了插值函数,未调用它生成新的比重值,也未完成数据整合

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 21:50:39