Alternative method for two way interpolation

Bjc51192 :

I wrote some code to perform interpolation based on two criteria, the amount of insurance and the deductible amount %. I was struggling to do the interpolation all at once, so had split the filtering.The table hf contains the known data which I am using to base my interpolation results on.Table df contains the new data which needs the developed factors interpolated based on hf.

Right now my work around is first filtering each table based on the ded_amount percentage and then performing the interpolation into an empty data frame and appending after each loop.

I feel like this is inefficient, and there is a better way to perform this, looking to hear some feedback on some improvements I can make. Thanks

Test data provided below.

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)
Alan :

Yep, there is a way to do this more efficiently, without having to make a bunch of intermediate dataframes and appending them. have a look at this code:

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)

The way the lambda function works is: It goes row by row through the column 'Factor' and gives it a value based on conditions on the other columns.

It returns the interpolation of the AOI column of df (this is what you called xnew) if the deduction amount matches, otherwise it just returns the same thing back.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=386347&siteId=1