python `interp 2d`已被弃用!却找不到替代品

4uqofj5v  于 2023-06-20  发布在  Python
关注(0)|答案(1)|浏览(773)

几个月前,我做了一个模型,它使用了scipy模块中的interp2d类。我现在想继续使用模型,但得到以下消息:
interp2d在SciPy 1.10中被弃用,并将在SciPy 1.12.0中被删除。

For legacy code, nearly bug-for-bug compatible replacements are
    `RectBivariateSpline` on regular grids, and `bisplrep`/`bisplev` for
    scattered 2D data.

    In new code, for regular grids use `RegularGridInterpolator` instead.
    For scattered data, prefer `LinearNDInterpolator` or
    `CloughTocher2DInterpolator`.

    For more details see
    `https://gist.github.com/ev-br/8544371b40f414b7eaf3fe6217209bff`

在与建议的替代方案混在一起一段时间后,我没有让它工作。我主要是在尺寸方面遇到了问题。这是我的代码:

def Compressibility_Factor(Pressure,Temperature):   # This function calculates the compressibility factor of Hydrogen
    
   Press = [0.001,0.1 , 1, 5, 10, 30, 50, 100]                          #MPa
   Temp = [-150, -125, -100, -75, -50, -25, 0, 25, 50, 75, 100, 125]    #Celcius
   CompFact_table = [[1,1.0003,1.0036,1.0259,1.0726,1.3711,1.7167, 0],
                         [1,1.0006,1.0058,1.0335,1.0782,1.3231,1.6017,2.2856],
                         [1,1.0007,1.0066,1.0356,1.0778,1.2880,1.5216,2.1006],
                         [1,1.0007,1.0068,1.0355,1.0751,1.2604,1.4620,1.9634],
                         [1,1.0007,1.0067,1.0344,1.0714,1.2377,1.4153,1.8572],
                         [1,1.0006,1.0065,1.0329,1.0675,1.2186,1.3776,1.7725],
                         [1,1.0006,1.0062,1.0313,1.0637,1.2022,1.3462,1.7032],
                         [1,1.0006,1.0059,1.0297,1.0601,1.1879,1.3197,1.6454],
                         [1,1.0006,1.0056,1.0281,1.0567,1.1755,1.2969,1.5964],
                         [1,1.0005,1.0053,1.0266,1.0536,1.1644,1.2770,1.5542],
                         [1,1.0005,1.0050,1.0252,1.0507,1.1546,1.2596,1.5175],
                         [1,1.0005,1.0048,1.0240,1.0481,1.1458,1.2441,1.4852]]
   
   F_c1 = interpolate.interp2d(Press,Temp,CompFact_table,kind = 'linear', fill_value = '0')

   F_c = F_c1(Pressure,Temperature-273.15)
   if F_c <= 0:
       print('Compressibility Factor Fail')
       1/F_c
   return F_c

谁能帮助我理解我应该使用什么方法以及如何实现它?那对我真的有帮助。
我尝试了以下方法:

def Compressibility_Factor(Pressure,Temperature):   # This function calculates the compressibility factor of Hydrogen
    
   Press = [0.001,0.1 , 1, 5, 10, 30, 50, 100]                          #MPa
   Temp = [-150, -125, -100, -75, -50, -25, 0, 25, 50, 75, 100, 125]    #Celcius
   CompFact_table = [[1,1.0003,1.0036,1.0259,1.0726,1.3711,1.7167, 0],
                         [1,1.0006,1.0058,1.0335,1.0782,1.3231,1.6017,2.2856],
                         [1,1.0007,1.0066,1.0356,1.0778,1.2880,1.5216,2.1006],
                         [1,1.0007,1.0068,1.0355,1.0751,1.2604,1.4620,1.9634],
                         [1,1.0007,1.0067,1.0344,1.0714,1.2377,1.4153,1.8572],
                         [1,1.0006,1.0065,1.0329,1.0675,1.2186,1.3776,1.7725],
                         [1,1.0006,1.0062,1.0313,1.0637,1.2022,1.3462,1.7032],
                         [1,1.0006,1.0059,1.0297,1.0601,1.1879,1.3197,1.6454],
                         [1,1.0006,1.0056,1.0281,1.0567,1.1755,1.2969,1.5964],
                         [1,1.0005,1.0053,1.0266,1.0536,1.1644,1.2770,1.5542],
                         [1,1.0005,1.0050,1.0252,1.0507,1.1546,1.2596,1.5175],
                         [1,1.0005,1.0048,1.0240,1.0481,1.1458,1.2441,1.4852]]
   F_c1 = interpolate.RectBivariateSpline(Temp,Press,CompFact_table)
   

   F_c = F_c1(Pressure,Temperature-273.15)
   if F_c <= 0:
       print('Compressibility Factor Fail')
       1/F_c
   return F_c

这将导致与原始代码不同的插值。有人能帮我理解吗?

4nkexdtk

4nkexdtk1#

弃用通知中的要点详细说明了具体步骤。简而言之,您需要转置z数组,然后转置结果。
顺便说一句,请注意,截至2023年6月19日,过渡指南的规范位置为https://scipy.github.io/devdocs/notebooks/interp_transition_guide.html

相关问题