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

设置藻类生长温度拟合替代最小、最大参数的问题咨询

问题根因与修复

报错核心原因

  • 运算符优先级错误:pandas逻辑与运算用&,但它的优先级高于==、>这类比较运算符,未给每个判断条件单独加括号会触发运算顺序错误,正确写法应该是(条件1) & (条件2)
  • 维度不匹配:你直接将单列的标量统计值(如最小温度)和整个列的Series做逻辑运算,会触发类型匹配错误
  • 遍历逻辑错误:for value in df2遍历的是DataFrame的列名,不是行数据,逻辑完全不成立,pandas处理这类需求不需要循环,用内置布尔索引和统计方法即可
  • 无实际赋值操作:你写的df2['Temperature'].min() <= 18这类语句只是判断表达式,没有赋值或存储结果的操作,不会对数据产生任何实际修改

第一步:计算替代边界温度参数

不需要新增列存储中间结果,直接计算符合要求的边界参数即可:

import pandas as pd
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt

# 导入你的数据
df = pd.DataFrame({
    'Temperature': [14.0, 18.0, 22.0, 26.0, 30.0, 34.0],
    'Growth rate': [0.363, 0.677, 0.822, 0.936, 0.897, 0.000],
    'Standard Error': [0.110, 0.043, 0.044, 0.073, 0.051, 0.000],
    'Weight': [9.091, 23.256, 22.727, 13.699, 19.608, 0.000],
    'Result': [0.000, 0.767, 0.975, 0.975, 0.767, 0.000]
})

# 先判断边界无生长的触发条件是否成立
min_temp_original = df['Temperature'].min()
max_temp_original = df['Temperature'].max()
is_min_zero = df.loc[df['Temperature']==min_temp_original, 'Growth rate'].iloc[0] == 0
is_max_zero = df.loc[df['Temperature']==max_temp_original, 'Growth rate'].iloc[0] == 0

# 计算符合要求的替代边界参数
if is_min_zero and is_max_zero:
    # 替代最低温:大于14且小于等于18的最小正生长温度
    alt_min_temp = df.loc[(df['Temperature']>14) & (df['Temperature']<=18) & (df['Growth rate']>0), 'Temperature'].min()
    # 替代最高温:大于等于30且小于34的最大正生长温度
    alt_max_temp = df.loc[(df['Temperature']>=30) & (df['Temperature']<34) & (df['Growth rate']>0), 'Temperature'].max()
else:
    alt_min_temp = min_temp_original
    alt_max_temp = max_temp_original

注:你提供的样例数据中14℃的生长速率为0.363不为0,所以默认不会触发替代参数逻辑,如果业务需要强制使用替代参数,直接删掉判断条件固定赋值alt_min_temp=18、alt_max_temp=30即可。


第二步:拟合温度生长函数

你的温度函数写法是正确的,np.sin默认输入为弧度,符合你的公式设计:

def boatman_temperature_function(temp, max_growth_rate, min_temp, temp_range, skewness, kurtosis):
    num = temp - min_temp
    denom = temp_range - min_temp  # temp_range为最高温度
    inside_sin = np.pi*((num/denom)**skewness)
    sin_result = np.sin(inside_sin)
    final = max_growth_rate*(sin_result**kurtosis)
    return final

# 拟合用的x为温度,y为Result列,也可根据需求换成Growth rate列
x = df['Temperature'].values
y = df['Result'].values

# 设置参数初始猜测值,顺序对应函数入参:最大生长速率、最低温、最高温、偏度、峰度
p0 = [1.0, alt_min_temp, alt_max_temp, 1, 1]
# 加参数边界限制避免拟合结果离谱
bounds = (
    [0, 10, 20, 0.1, 0.1],
    [2, 20, 40, 5, 5]
)
# 执行拟合
popt, pcov = curve_fit(boatman_temperature_function, x, y, p0=p0, bounds=bounds)
# 输出拟合得到的参数
print("拟合参数:最大生长速率=%.2f, 最低温=%.2f, 最高温=%.2f, 偏度=%.2f, 峰度=%.2f" % tuple(popt))

第三步:绘制散点图与拟合曲线

# 生成连续温度值绘制拟合曲线
x_pred = np.linspace(alt_min_temp, alt_max_temp, 100)
y_pred = boatman_temperature_function(x_pred, *popt)

# 绘图
plt.scatter(x, y, label='实测Result值', color='red')
plt.plot(x_pred, y_pred, label='拟合曲线', color='blue')
plt.xlabel('温度(℃)')
plt.ylabel('生长速率')
plt.legend()
plt.show()

如果需要把模拟的生长速率存到DataFrame里,新增一行代码即可:

df['simulated_growth'] = boatman_temperature_function(df['Temperature'], *popt)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 05:18:00