Pandas多字段分组拟合Weibull分布获取各组shape/scale参数
你原有代码的问题在于将所有分组的风速数据拼接后统一传入拟合函数,因此只能得到全局的拟合参数。你需要将拟合逻辑放在分组后的apply回调内,对每个分组单独执行拟合,实现代码如下:
from scipy.stats import weibull_min import pandas as pd def weibull_fit_per_group(group): # 过滤空值,避免拟合异常 wind_speed = group['wind_speed_ms'].dropna() # 样本量过少时返回空值,避免不可靠的拟合结果 if len(wind_speed) < 10: return pd.Series([None, None, None], index=["shape", "loc", "scale"]) shape, loc, scale = weibull_min.fit(wind_speed, floc=0) return pd.Series([shape, loc, scale], index=["shape", "loc", "scale"]) # 按指定字段分组拟合,得到各分组的参数结果 result_params = df.groupby(["plant_name", "month", "year"]).apply(weibull_fit_per_group).reset_index()
输出的result_params为DataFrame格式,每一行对应一个分组,依次包含风场名称、月份、年份,以及对应的Weibull分布三个参数值,可直接查看或导出使用。
内容的提问来源于stack exchange,提问作者user2100039
相关产品推荐
相关产品推荐

