gpt4 book ai didi

python - 如何让 python 中的函数在 2 个列表(每个列表都有自己的 x、y)之间进行插值?

转载 作者:太空宇宙 更新时间:2023-11-03 20:09:46 25 4
gpt4 key购买 nike

我对 python 比较陌生,并且有一个复杂而有趣的问题(IMO),我正在尝试解决但不知道如何继续。

我有一个包含 3 个参数的保险费数据集:

  • 年龄(从 18 岁、20 岁、25 岁、30 岁……到 59 岁,步长为 5)
  • 覆盖范围(50k、100k、150k、200k)
  • 保费(特定年龄和特定承保范围的保险费用)

可视化

enter image description here

#Age
Age = ["18","20","25","30","35","40","45","50","55","59"]
#Insurance coverage
Coverage = ["50000","100000","150000","200000"]

#Premiums Data
Cover50k_Annual_Premium = [473.05,501.90,588.45,698.05,842.25,1032.65,1298.05,1696.10,2376.85,3380.65]
Cover100k_Annual_Premium = [946.10,1003.80,1176.90,1396.10,1684.55,2065.30,2596.05,3392.15,4753.65,6761.25]
Cover150k_Annual_Premium = [1419.15,1505.70,1765.30,2094.15,2526.80,3097.95,3894.10,5088.25,7130.50,10141.90]
Cover200k_Annual_Premium = [1892.25,2007.60,2353.75,2792.20,3369.10,4130.60,5192.10,6784.35,9507.30,13522.55 ]

#Matrix
Premiums = np.array([Cover50k_Annual_Premium, Cover100k_Annual_Premium, Cover150k_Annual_Premium, Cover200k_Annual_Premium])

#visualization
plt.plot(Cover50k_Annual_Premium, c='Black', ls= '--', marker='o', label='50k')
plt.plot(Cover100k_Annual_Premium, c='red', ls= '--', marker='o', label='100k')
plt.plot(Cover150k_Annual_Premium, c='blue', ls= '--', marker='o', label='150k')
plt.plot(Cover200k_Annual_Premium, c='green', ls= '--', marker='o', label='200k')

# interpolate between all premiums
# formula: interp = [(i1 + i2)/2.0 for i1, i2 in zip(l1, l2)]
interp_premiums = [(Cover50k_Annual_Premium+Cover100k_Annual_Premium+Cover150k_Annual_Premium+Cover200k_Annual_Premium)/4.0
for Cover50k_Annual_Premium, Cover100k_Annual_Premium, Cover150k_Annual_Premium, Cover200k_Annual_Premium
in zip(Cover50k_Annual_Premium, Cover100k_Annual_Premium, Cover150k_Annual_Premium, Cover200k_Annual_Premium)]

plt.plot(interp_premiums, c='magenta', ls= '--', marker='o', label='interp')

plt.legend(loc='upper left',bbox_to_anchor=(1,1))
plt.xticks(list(range(0,10)),Age, rotation='vertical')
plt.show()

interp_premiums

# given Age 27, find the Premiums for 50k, 100k, 150k, 200k
# ?????

我正在尝试获取给定年龄(例如 27 岁)的人每种保险(50k、100k、150k、200k)的估计保费(插值),但我陷入了困境。

最终,如果我返回一个人在给定的所有承保范围内的估计保费列表,这样我也可以在承保范围之间进行插值(例如,27 岁的人的保费是多少,对于保费125k)。

最佳答案

简短的答案是:

from scipy.interpolate import interp1d
AgeNum = np.array(Age, dtype=float)
interps = {Coverage[count]: interp1d(
AgeNum, param, kind='cubic', fill_value='extrapolate'
) for count, param in enumerate(Premiums)}

我们只是通过 scipy 的三次样条 interp1d 方法制作插值函数的字典。

但是我刚刚注意到你的绘图做了一些奇怪的事情 - 我建议不要通过用刻度标签“掩盖”真实的电位器值来覆盖缩放协议(protocol)。

编辑:

以下是我的完整编码方式:

import matplotlib.pyplot as plt
import numpy as np

from scipy.interpolate import interp1d

#Age
Age = ["18","20","25","30","35","40","45","50","55","59"]

#Insurance coverage
Coverage = ["50k","100k","150k","200k"]

#Premiums Data
Cover50k_Annual_Premium = [473.05,501.90,588.45,698.05,842.25,1032.65,1298.05,1696.10,2376.85,3380.65]
Cover100k_Annual_Premium = [946.10,1003.80,1176.90,1396.10,1684.55,2065.30,2596.05,3392.15,4753.65,6761.25]
Cover150k_Annual_Premium = [1419.15,1505.70,1765.30,2094.15,2526.80,3097.95,3894.10,5088.25,7130.50,10141.90]
Cover200k_Annual_Premium = [1892.25,2007.60,2353.75,2792.20,3369.10,4130.60,5192.10,6784.35,9507.30,13522.55 ]

#Matrix
Premiums = np.array([Cover50k_Annual_Premium, Cover100k_Annual_Premium, Cover150k_Annual_Premium, Cover200k_Annual_Premium])

#interpolation
AgeNum = np.array(Age, dtype=float)
interps = {Coverage[count]: interp1d(
AgeNum, param, kind='cubic', fill_value='extrapolate'
) for count, param in enumerate(Premiums)}

# 2d interp
coverages = lambda z, y: interp1d(
x=[50000,100000,150000,200000],
y=[interps[func_val](z) for func_val in Coverage],
kind='cubic',
fill_value='extrapolate'
)(y)


#visualization
fig = plt.figure()
ax = fig.add_subplot(111)
ax.tick_params(direction='in', pad = 5)

ax.plot(AgeNum, Cover50k_Annual_Premium, c='Black', ls= '--', marker='o', label='50k')
ax.plot(AgeNum, Cover100k_Annual_Premium, c='red', ls= '--', marker='o', label='100k')
ax.plot(AgeNum, Cover150k_Annual_Premium, c='blue', ls= '--', marker='o', label='150k')
ax.plot(AgeNum, Cover200k_Annual_Premium, c='green', ls= '--', marker='o', label='200k')

ages = np.linspace(18,59,100)
ax.plot(ages, interps["50k"](ages))
ax.plot(ages, interps["100k"](ages))
ax.plot(ages, interps["150k"](ages))
ax.plot(ages, interps["200k"](ages))

ax.legend(loc='upper left',bbox_to_anchor=(1,1))

plt.tight_layout()
plt.show()

这样,可以使用字典键调用插值:

In [23]:interps["50k"](27)
Out[23]: array(628.96153198)

In [24]:interps["200k"](27)
Out[24]: array(2515.80282111)

In [40]:coverages(27, 125000)
Out[40]: array(1572.3803509)

关于python - 如何让 python 中的函数在 2 个列表(每个列表都有自己的 x、y)之间进行插值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58776231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com