`interp2d`已被弃用!但无法找到替代方法。

huangapple go评论240阅读模式
英文:

`interp2d` is deprecated! but can't get alternatives to work

问题

几个月前,我制作了一个使用scipy模块中interp2d类的模型。现在我想继续使用这个模型,但我收到以下消息:

“interp2d” 在 SciPy 1.10 中已弃用,并将在 SciPy 1.12.0 中删除。

对于旧代码,几乎与之兼容的替代方法是在规则网格上使用 “RectBivariateSpline”,以及对于散布的2D数据,可以使用 “bisplrep”/“bisplev”。

在新代码中,对于规则网格,请改用 “RegularGridInterpolator”。对于散布数据,推荐使用 “LinearNDInterpolator” 或 “CloughTocher2DInterpolator”。

更多详情请参阅 https://gist.github.com/ev-br/8544371b40f414b7eaf3fe6217209bff

在尝试了一段时间并使用了建议的替代方法后,我无法让它正常工作。我主要遇到尺寸方面的问题。这是我的代码:

def Compressibility_Factor(Pressure, Temperature):   # 这个函数计算氢的压缩因子
    
   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]    #摄氏度
   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('压缩因子计算失败')
       1/F_c
   return F_c

我尝试了以下方法:

def Compressibility_Factor(Pressure, Temperature):   # 这个函数计算氢的压缩因子
    
   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]    #摄氏度
   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 =

<details>
<summary>英文:</summary>

A few months back I made a model that uses the the class interp2d from the scipy module. I want to go on with the model now but I get the following message:

 `interp2d` is deprecated in SciPy 1.10 and will be removed in 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`

After messing around for some time with the proposed alternatives I don&#39;t get it to work. I mainly run into problems with the dimensions. This is my code:


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


Can anyone help me understand what method I should use and how I can implement it? That would really help me. 
I have tried the following:

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


This results in a different interpolated value than the original code. Can anyone help me understand this?
</details>
# 答案1
**得分**: 1
翻译好的部分:
"The gist in the deprecation notice details exact steps. In short, you need to transpose the `z` array, then transpose the result."
"BTW, note that as of June 19, 2023, the canonic location of the transition guide is https://scipy.github.io/devdocs/notebooks/interp_transition_guide.html"
<details>
<summary>英文:</summary>
The gist in the deprecation notice details exact steps. In short, you need to transpose the `z` array, then transpose the result.
BTW, note that as of June 19, 2023, the canonic location of the transition guide is https://scipy.github.io/devdocs/notebooks/interp_transition_guide.html
</details>

huangapple
  • 本文由 发表于 2023年6月19日 20:14:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506522.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定