scipy.odr.ODR传入sy(y_err)时出现「解处秩不足」错误
Scipy ODR传入y误差(sy)时拟合失败问题排查
我计划用ODR处理实验室数据,先通过模拟数据学习scipy.odr的用法。测试发现,只要向odr.RealData类传入y误差(sy),就会出现StopReason提示“Problem is not full rank at solution”,且拟合参数和初始猜测值完全一致,没有更新。尝试添加setup参数fit_type=2(强制使用类似numpy.polyfit的线性回归),结果没有变化。该问题疑似和Stack Overflow帖子78522990相关,但目前该帖无解决方案,仅讨论numpy和scipy版本问题。
复现代码
if __name__ == "__main__": import scipy import numpy as np import pandas as pd print(f"scipy version: {scipy.__version__}") print(f"numpy version: {np.__version__}") print(f"pandas version: {pd.__version__}") print("") np.random.seed(10) pd.options.display.float_format = '{:,.4f}'.format def fit_func(beta, x): y = beta[0] + beta[1]*np.exp(-1 * beta[2] * x) return y # initial guess of parameters beta0 = (1.00, 85.0, 0.02) # actual parameters unknown to us but close to beta0 beta = (np.abs(np.random.normal(beta0[0], 2.000)), np.abs(np.random.normal(beta0[1], 5.000)), np.abs(np.random.normal(beta0[2], 0.004)), ) # noiseless data x = np.linspace(10, 90, 81) y = fit_func(beta, x) # simulate discrepancy dx = np.random.normal(0, 0.2, len(x)) dy = np.random.normal(0, 1.0, len(y)) # add discrepancy x += dx y += dy # estimate uncertainty sx = 0.5 + np.abs(dx) sy = 1.4 + np.abs(dy) # ODR with 'sy' odr_noisy = scipy.odr.ODR( data=scipy.odr.RealData(x, y, sx, sy), model=scipy.odr.Model(fit_func), beta0=beta0, ) output_noisy = odr_noisy.run() print(f"ODR_NOISY: {output_noisy.stopreason}") # ODR without 'sy' odr_clean = scipy.odr.ODR( data=scipy.odr.RealData(x, y, sx, None), model=scipy.odr.Model(fit_func), beta0=beta0, ) output_clean = odr_clean.run() print(f"ODR_CLEAN: {output_clean.stopreason}") # compare results results = {"initial": beta0, "ODR noisy": output_noisy.beta, "ODR clean": output_clean.beta, "actual": beta, } table = pd.DataFrame(results).transpose() table.columns = ["beta[0]", "beta[1]", "beta[2]"] print(f"\n{table}")
输出结果
scipy version: 1.16.2 numpy version: 2.3.3 pandas version: 2.3.3 ODR_NOISY: ['Problem is not full rank at solution', 'Sum of squares convergence'] ODR_CLEAN: ['Sum of squares convergence'] beta[0] beta[1] beta[2] initial 1.0000 85.0000 0.0200 ODR noisy 1.0000 85.0000 0.0200 ODR clean 0.7697 90.5035 0.0129 actual 3.6632 88.5764 0.0138
可视化图表

更新补充信息
- 系统:Windows 11 Pro for Workstations,OS版本26200.6899
- scipy通过miniforge/mamba安装
- 详细构建信息:
1.16.2 2.3.3 sys.version_info(major=3, minor=13, micro=0, releaselevel='final', serial=0) Build Dependencies: blas: detection method: pkgconfig found: true include directory: C:/Users/nukamoi/AppData/Local/miniforge3/envs/pyvt/Library/include lib directory: C:/Users/nukamoi/AppData/Local/miniforge3/envs/pyvt/Library/lib name: mkl-sdl openblas configuration: unknown pc file directory: C:\miniconda3\conda-bld\scipy_1759489068917\_h_env\Library\lib\pkgconfig version: '2025' lapack: detection method: pkgconfig found: true include directory: C:/Users/nukamoi/AppData/Local/miniforge3/envs/pyvt/Library/include lib directory: C:/Users/nukamoi/AppData/Local/miniforge3/envs/pyvt/Library/lib name: mkl-sdl openblas configuration: unknown pc file directory: C:\miniconda3\conda-bld\scipy_1759489068917\_h_env\Library\lib\pkgconfig version: '2025' pybind11: detection method: pkgconfig include directory: C:/Users/nukamoi/AppData/Local/miniforge3/envs/pyvt/Library/include name: pybind11 version: 2.13.6 Compilers: c: args: -IC:\miniconda3\conda-bld\scipy_1759489068917\work\scipy\linalg, -IC:\miniconda3\conda-bld\scipy_1759489068917\work\scipy\_lib commands: clang-cl.exe linker: lld-link name: clang-cl version: 20.1.8 c++: commands: clang-cl.exe linker: lld-link name: clang-cl version: 20.1.8 cython: commands: cython linker: cython name: cython version: 3.1.4 fortran: commands: ifort linker: xilink name: intel-cl version: 2021.6.0 pythran: include directory: ..\..\_h_env\Lib\site-packages\pythran version: 0.18.0 Machine Information: build: cpu: x86_64 endian: little family: x86_64 system: windows cross-compiled: false host: cpu: x86_64 endian: little family: x86_64 system: windows Python Information: path: C:\miniconda3\conda-bld\scipy_1759489068917\_h_env\python.exe version: '3.13' Build Dependencies: blas: detection method: pkgconfig found: true include directory: C:/Users/nukamoi/AppData/Local/miniforge3/envs/pyvt/Library/include lib directory: C:/Users/nukamoi/AppData/Local/miniforge3/envs/pyvt/Library/lib name: mkl-sdl openblas configuration: unknown pc file directory: C:\miniconda3\conda-bld\numpy_and_numpy_base_1757670256083\_h_env\Library\lib\pkgconfig version: '2025' lapack: detection method: pkgconfig found: true include directory: C:/Users/nukamoi/AppData/Local/miniforge3/envs/pyvt/Library/include lib directory: C:/Users/nukamoi/AppData/Local/miniforge3/envs/pyvt/Library/lib name: mkl-sdl openblas configuration: unknown pc file directory: C:\miniconda3\conda-bld\numpy_and_numpy_base_1757670256083\_h_env\Library\lib\pkgconfig version: '2025' Compilers: c: commands: cl.exe linker: link name: msvc version: 19.29.30159 c++: commands: cl.exe linker: link name: msvc version: 19.29.30159 cython: commands: cython linker: cython name: cython version: 3.1.3 Machine Information: build: cpu: x86_64 endian: little family: x86_64 system: windows host: cpu: x86_64 endian: little family: x86_64 system: windows Python Information: path: C:\miniconda3\conda-bld\numpy_and_numpy_base_1757670256083\_h_env\python.exe version: '3.13' SIMD Extensions: baseline: - SSE - SSE2 - SSE3 found: - SSSE3 - SSE41 - POPCNT - SSE42 - AVX - F16C
内容的提问来源于stack exchange,提问作者nukamoi
相关产品推荐
相关产品推荐

