python - 双向插值的替代方法

标签 python scipy interpolation

我写了一些代码来根据两个标准执行插值,即保险金额和免赔额百分比。我一直在努力一次完成插值,所以拆分了过滤。表 hf 包含我用来作为插值结果基础的已知数据。表 df 包含需要基于 hf 插值的开发因子的新数据.

现在我的工作是首先根据 ded_amount 百分比过滤每个表,然后在空数据框中执行插值并在每个循环后追加。

我觉得这样做效率低下,并且有更好的方法来执行此操作,希望听到一些关于我可以做出的改进的反馈。谢谢

下面提供测试数据。

import pandas as pd
from scipy import interpolate

known_data={'AOI':[80000,100000,150000,200000,300000,80000,100000,150000,200000,300000],'Ded_amount':['2%','2%','2%','2%','2%','3%','3%','3%','3%','3%'],'factor':[0.797,0.774,0.739,0.733,0.719,0.745,0.737,0.715,0.711,0.709]}
new_data={'AOI':[85000,120000,130000,250000,310000,85000,120000,130000,250000,310000],'Ded_amount':['2%','2%','2%','2%','2%','3%','3%','3%','3%','3%']}

hf=pd.DataFrame(known_data)
df=pd.DataFrame(new_data)

deduct_fact=pd.DataFrame()
for deduct in hf['Ded_amount'].unique():
    deduct_table=hf[hf['Ded_amount']==deduct]
    aoi_table=df[df['Ded_amount']==deduct]
    x=deduct_table['AOI']
    y=deduct_table['factor']
    f=interpolate.interp1d(x,y,fill_value="extrapolate")
    xnew=aoi_table[['AOI']]
    ynew=f(xnew)
    append_frame=aoi_table
    append_frame['Factor']=ynew
    deduct_fact=deduct_fact.append(append_frame)

最佳答案

是的,有一种方法可以更有效地做到这一点,而不必制作一堆中间数据帧并附加它们。看看这段代码:

from scipy import interpolate
known_data={'AOI':[80000,100000,150000,200000,300000,80000,100000,150000,200000,300000],'Ded_amount':['2%','2%','2%','2%','2%','3%','3%','3%','3%','3%'],'factor':[0.797,0.774,0.739,0.733,0.719,0.745,0.737,0.715,0.711,0.709]}
new_data={'AOI':[85000,120000,130000,250000,310000,85000,120000,130000,250000,310000],'Ded_amount':['2%','2%','2%','2%','2%','3%','3%','3%','3%','3%']}

hf=pd.DataFrame(known_data)
df=pd.DataFrame(new_data)

# Create this column now
df['Factor'] = None

# I like specifying this explicitly; easier to debug
deduction_amounts = list(hf.Ded_amount.unique())
for deduction_amount in deduction_amounts:
    # You can index a dataframe and call a column in one line
    x, y = hf[hf['Ded_amount']==deduction_amount]['AOI'], hf[hf['Ded_amount']==deduction_amount]['factor']

    f = interpolate.interp1d(x, y, fill_value="extrapolate")

    # This is the most important bit. Lambda function on the dataframe
    df['Factor'] = df.apply(lambda x: f(x['AOI']) if x['Ded_amount']==deduction_amount else x['Factor'], axis=1)

lambda 函数的工作方式是: 它逐行遍历“因素”列,并根据其他列的条件为其赋值。

如果扣除额匹配则返回df的AOI列的插值(这就是你所说的xnew),否则返回同样的东西。

关于python - 双向插值的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60940856/

相关文章:

python - 为什么 scipy.io.loadmat 无法过滤除此生成器函数的第一次迭代以外的所有变量?

python - 使用 python 修复拐点估计

regex - perl6如何在正则表达式内插中使用结点?

python - 永远重复 "Kernel died, restarting"

python - 从列表中删除以特定表达式开头的字符串

python - Keras 和 TensorFlow : Saving non-sequential model weights

python - 为 Pandas 创建自定义插值函数

python - 为什么我的 Facebook 应用程序出现错误 104 ("invalid signature")?

python:使用引用图像和来自相机的当前图像查找相机偏移角度

java - 牛顿多项式的规范系数