簡體   English   中英

SciPy curve_fit 不會嘗試改變洛倫茲的峰值

[英]SciPy curve_fit doesn't try to shift Lorentzian's peak

我有一些洛倫茲數據,我正在嘗試擬合並且遇到問題。 我想也許 scipy 太吵了,找不到合適的,所以我生成了一個理想化的 Lorentzian 來代替,令我驚訝的是,scipy 也無法適應它,初始猜測幾乎完美。 在這種情況下,scipy 需要做的就是將 Lorentzian 的峰值移動不到 1%,盡管它會因任意准確的初始猜測而失敗。

import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit

def lorentzian(freq, gamma, center_freq, amplitude):
    return amplitude * gamma**2 / ( gamma**2 + ( freq - center_freq )**2) + 1

frequencies = np.arange(4.95e9, 5.05e9, 0.1e9/1000)

guess = [15000000, 4.997e9, 28]
fake = [15000000, 5.02e9, 28]
test_data = lorentzian(frequencies, *fake)
popt, pcov = curve_fit(lorentzian, test_data, frequencies, maxfev = 100000, p0 = guess)
fit_lorentzian = lorentzian(frequencies, *popt)
guess_lorentzian = lorentzian(frequencies, *guess)
plt.plot(frequencies, test_data)
plt.plot(frequencies, fit_lorentzian)

測試數據與擬合

scipy警告我OptimizeWarning: Covariance of the parameters could not be estimated warnings.warn('Covariance of the parameters could not be estimated',並將初始猜測返回為popt。pcov是一個3x3的inf數組。這是怎么回事?

正如評論中指出的,curve_fit 有參數(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False, check_finite=True, bounds=(- inf, inf), method=None, jac=None, **kwargs) ; 即,我交換了 x 數據和 y 數據的順序。 popt, pcov = curve_fit(lorentzian, frequencies, test_data, maxfev = 100000, p0 = guess)解決它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM