我想要将数据拟合成二维表面,具体来说,我希望找到一个将像素坐标映射到波长坐标的函数,就像IRAF中的FITCOORDS
一样。
例如,在以下代码片段中,我想要找到对test
数组进行拟合的结果:
import numpy as np
from astropy.modeling.models import Chebyshev2D
from astropy.modeling.fitting import LevMarLSQFitter
#%%
test = np.array([[7473, 7040, 6613, 6183, 5753, 5321, 4888],
[7474, 7042, 6616, 6186, np.nan, 5325, 4893],
[7476, 7044, 6619, 6189, 5759, 5328, 4897],
[7479, 7047, np.nan, 6192, 5762, 5331, 4900]])
grid_pix, grid_wave = np.mgrid[:4, :7]
fitter = LevMarLSQFitter()
c2_init = Chebyshev2D(x_degree=3, y_degree=3)
c2_fit = fitter(c2_init, grid_wave, grid_pix, test)
print(c2_fit)
在Python 3.6上,使用astropy 2.0.2
和numpy 1.13.3
的结果如下:
Model: Chebyshev2D
Inputs: ('x', 'y')
Outputs: ('z',)
Model set size: 1
X-Degree: 3
Y-Degree: 3
Parameters:
c0_0 c1_0 c2_0 c3_0 c0_1 c1_1 c2_1 c3_1 c0_2 c1_2 c2_2 c3_2 c0_3 c1_3 c2_3 c3_3
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
WARNING: Model is linear in parameters; consider using linear fitting methods. [astropy.modeling.fitting]
很明显,拟合从未成功过。
如果我将np.nan
更改为某些值,则拟合按预期工作(例如,手动将np.nan
更改为0、1等)。
我应该如何获得合理的结果?我可以让拟合器忽略np.nan
值吗?
np.where
例如 - Julien