Я ищу предложения о том, как улучшить приведенный ниже процесс.
У меня есть таблица с именем deductible_table , в которой содержится сопоставление страховой суммы, франшизы и опасности с соответствующим коэффициентом дисконтирования. Эта таблица используется в качестве справочной информации, где для выполнения интерполяции содержится известное отображение.
Мне предоставляется input_table , где каждая Группа представляет уникальный почтовый индекс / AOI комбинация.
Проблема, с которой я сталкиваюсь, связана с текущим состоянием, в котором я работаю над новыми логами c. Эта логика c гласит, что Ветер Опасность должна иметь такое же количество франшизы, что и ураган
Однако в Hurricane есть логика c, настроенная на то, что определенные почтовые индексы содержат обязательную 3% франшизу на сумму риска урагана, независимо от того, что они выбрали ранее. Чтобы учесть это, я создал новый столбец с именем Ураган 3% . Следует отметить, что значение этого столбца не влияет на сумму франшизы Ветер . Например, если изначально франшиза урагана была установлена на 0,02, но помечена как 3%, это не повлияет на франшизу Ветер .
Приведенный ниже код работает для небольших наборов данных , Тем не менее, это довольно громоздко, когда я имею дело с большими наборами данных. Я полагаю, что проблема заключается в моем методе интерполяции ветра и урагана, когда я повторяю одну за другой и применяю такое количество комбинаций страховка / франшиза.
Ценю руководство / предложения.
import pandas as pd
import numpy as np
from scipy import interpolate
data_deduct={'Policy Amount Of Insurance':[100,200,300,100,200,300,100,200,300],'Deductible Amount':[0.02,0.02,0.02,0.03,0.03,0.03,1000,1000,1000],"Fire":[1,1,1,1,1,1,.8,.7,.6],"Wind":[1,1,1,1,1,1,1,.8,.8],"Hurricane":[.2,.2,.2,.6,.5,.3,1,1,1]}
deductible_table=pd.DataFrame(data_deduct)
input_data={'Group':[1,2,3,4,5,6,7,8,9],'Enter Amount of Insurance':[105,205,305,105,205,305,105,205,305],'All Other Peril Deductible':[0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02],'Hurricane Deductible':[0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02],'Hurricane 3%':[0.02,0.02,0.02,0.02,0.03,0.02,0.03,0.02,0.02]}
input_table=pd.DataFrame(input_data)
all_peril_deductible_factor=pd.DataFrame()
deductible_table=pd.melt(deductible_table,id_vars=['Policy Amount Of Insurance','Deductible Amount'],var_name='Peril Type',value_name='Factor')
for deduct in list(input_table['All Other Peril Deductible'].unique()):
deduct_table=deductible_table[deductible_table['Deductible Amount']==deduct]
for peril in list(deduct_table['Peril Type'].unique()):
x=deduct_table['Policy Amount Of Insurance'][deduct_table['Peril Type']==peril]
y=deduct_table['Factor'][deduct_table['Peril Type']==peril]
f=interpolate.interp1d(x,y,fill_value="extrapolate")
xnew=input_table[['Enter Amount of Insurance']]
ynew=f(xnew)
append_frame=input_table[['Group','Enter Amount of Insurance']]
append_frame['Peril Type']=str(peril)
append_frame['Factor']=ynew
all_peril_deductible_factor=all_peril_deductible_factor.append(append_frame)
#all_peril_deductible_factor=all_peril_deductible_factor.append(deduct_table[['Group','Enter Amount of Insurance','All Other Peril Deductible','Hurricane Deductible','Peril Type','Factor']])
wind_deductible_factor=input_table[['Group']]
wind_factor=[]
wind_aoi=[]
aoi_list=np.array(input_table['Enter Amount of Insurance'].to_list())
w_deduct_list=np.array(input_table['Hurricane Deductible'].to_list())
for aoi, w_deduct in zip(aoi_list,w_deduct_list):
w_deduct_table=deductible_table[deductible_table['Deductible Amount']==w_deduct]
for peril in ['Wind']:
x=w_deduct_table['Policy Amount Of Insurance'][w_deduct_table['Peril Type']==peril]
y=w_deduct_table['Factor'][w_deduct_table['Peril Type']==peril]
f=interpolate.interp1d(x,y,fill_value="extrapolate")
xnew=aoi
ynew=f(xnew)
wind_aoi.append(aoi)
wind_factor.append(ynew)
wind_deductible_factor['Peril Type']=str(peril)
wind_deductible_factor['Enter Amount of Insurance']=wind_aoi
wind_deductible_factor['Factor']=wind_factor
hurricane_deductible_factor=input_table[['Group']]
hurricane_factor=[]
hurricane_aoi=[]
aoi_list=np.array(input_table['Enter Amount of Insurance'].to_list())
h_deduct_list=np.array(input_table['Hurricane 3%'].to_list())
for aoi,h_deduct in zip(aoi_list,h_deduct_list):
h_deduct_table=deductible_table[deductible_table['Deductible Amount']==h_deduct]
for peril in ['Hurricane']:
x=h_deduct_table['Policy Amount Of Insurance'][h_deduct_table['Peril Type']==peril]
y=h_deduct_table['Factor'][h_deduct_table['Peril Type']==peril]
f=interpolate.interp1d(x,y,fill_value="extrapolate")
xnew=aoi
ynew=f(xnew)
hurricane_aoi.append(aoi)
hurricane_factor.append(ynew)
hurricane_deductible_factor['Peril Type']=str(peril)
hurricane_deductible_factor['Enter Amount of Insurance']=hurricane_aoi
hurricane_deductible_factor['Factor']=hurricane_factor
# Concat deductible tables
combine=all_peril_deductible_factor[(all_peril_deductible_factor['Peril Type']!='Hurricane') & (all_peril_deductible_factor['Peril Type']!='Wind')]
all_peril_deductible_factor=pd.concat([combine,hurricane_deductible_factor,wind_deductible_factor])