Python 运行时错误中的 interp2d 函数

interp2d function in Python runtime error

我有以下数据,在 MATLAB 中可以使用 interp2 函数轻松对其进行插值。但是在 Python 中使用 interp2d 时,会遇到以下错误:

RuntimeWarning: No more knots can be added because the number of B-spline
coefficients already exceeds the number of data points m.
Probable causes: either s or m too small. (fp>s)
    kx,ky=3,3 nx,ny=17,11 m=90 fp=0.000013 s=0.000000
  warnings.warn(RuntimeWarning(_iermess2[ierm][0] + _mess))

我曾尝试使用 griddata,但也没有成功。 如果能解决这些问题,我们将不胜感激。

代码:

OF_S = np.array([[5, 5, 5, 5, 5, 5, 5,     5,     5,     5,     5,     5,     5,     5,     5,     5,     5,     5,     5,     5,     5,     5,     5,     5,     5,     5,     5,     5,     5,     5],
                 [6, 6, 6,     6,     6,     6,     6,     6,     6,     6,     6,     6,     6,     6,     6, 6,     6,     6,     6,     6,     6,     6,     6,     6,     6,     6,     6,     6,     6,     6],
                 [7, 7, 7,     7,     7,     7,     7,     7,     7,     7,     7,     7,     7,     7,     7,     7,     7,     7,     7,     7,     7,     7,     7,     7,     7,     7,     7,     7,     7,     7]])

FT_FTAVAIL_S = np.array([[0.198,     0.205,     0.214,     0.227,     0.237,     0.249,     0.26,     0.271,     0.285,     0.304,     0.332,     0.371,     0.405,     0.436,     0.464,     0.507,     0.548,     0.598,     0.649,     0.694,     0.746,     0.787,     0.822,     0.851,     0.879,     0.914,     0.951,     0.985,     0.999,     1],
                         [0.198,     0.205,     0.214,     0.227,     0.237,     0.249,     0.26,     0.271,     0.285,     0.304,     0.332,     0.371,     0.405,     0.436,     0.464, 0.507,     0.548,     0.598,     0.649,     0.694,     0.746,     0.787,     0.822,     0.851,     0.879,     0.914,     0.951,     0.985,     0.999,     1],
                         [0.198,     0.205,     0.214,     0.227,     0.237,     0.249,     0.26,     0.271,     0.285,     0.304,     0.332,     0.371,     0.405,     0.436,     0.464,     0.507,     0.548,     0.598,     0.649,     0.694,     0.746,     0.787,     0.822,     0.851,     0.879,     0.914,     0.951,     0.985,     0.999,     1.000]])

ISP_ISPAVAIL_S = np.array([[0.9845,     0.9867,     0.9867,     0.9867,     0.9867,     0.9889,     0.9889,     0.9889,     0.9889,     0.9911,     0.9911,     0.9933,     0.9933,     0.9956,     0.9956,     0.9956,     0.9978,     0.9978,     0.9978,     0.9978,     0.9978,     0.9978,     0.9978,     0.9978,     0.9978,     0.9978,     1,     1,     1,     1],
                           [0.9756,     0.9756,     0.9761,     0.9778,     0.9778,     0.9778,     0.9787,     0.9798,     0.98,     0.9804,     0.9827,     0.9844,     0.9855,     0.9867, 0.9889,     0.9892,     0.9911,     0.9927,     0.9933,     0.9942,     0.9956,     0.9956,     0.9978,     0.9978,     0.9978,     0.9978,     0.9988,     1,     1,     1],
                           [0.9662,     0.9662,     0.9662,     0.9679,     0.9685,     0.9697,     0.9707,     0.9709,     0.972,     0.9738,     0.9755,     0.9797,     0.9805,     0.982,     0.9842,     0.9863,     0.9873,     0.9887,     0.991,     0.992,     0.9932,     0.9943,     0.9955,     0.9955,     0.9966,     0.9977,     0.9978,     0.9986,     0.9999,     1]])

ISP_ISPAVAIL_interpGrid = interp2d(FT_FTAVAIL_S, OF_S, ISP_ISPAVAIL_S, kind='cubic')

我最终改用了 griddata。这里的关键是在任何插值之前将数据展平。因此我使用 .ravel() 来这样做。

ISP_ISPAVAIL= griddata(
        (FT_FTAVAIL_S.ravel(), OF_S.ravel()), ISP_ISPAVAIL_S.ravel(), (XX, YY), method="nearest")

其中 XX 和 YY 分别是 FT_FTAVAIL_S 和 OF_S 的任意边界。