我有数据框
df
,其中包含以下列:
X | 是 | delta_t |
---|---|---|
. | . | . |
. | . | . |
. | . | . |
. | . | . |
. | . | . |
并且我正在尝试使用 python curve_fit
(https://docs.scipy) 拟合指数函数: Y/X = a x Exp(-(b_1 - b_2) x
cx delta_t) .org/doc/scipy/reference/ generated/scipy.optimize.curve_fit.html)
我遇到一个问题
"LinAlgError: SVD did not converge"
。
有关数据框和变量的一些详细信息:
df.shape = (4592, 92)
df['Y'].describe()
:
|统计|数字|
|------|-------------|
|计数| 3537.000000 |
|意思是| 19.985690 |
|标准| 22.486308 |
|分钟 | 0.582168 |
|25% | 5.446700|
|50% | 10.997200|
|75% | 25.789900|
|最大| 121.417000|
df['X'].describe()
:
|统计|数字|
|------|-------------|
|计数| 3537.000000|
|意思是| 5.686478|
|标准| 5.564841|
|分钟| 0.265318|
| 25% | 1.928670|
| 50% | 3.581990|
| 75% | 6.938480|
|最大 |26.451900 |
df['delta_t'].describe()
:
|统计|数字|
|------|-------------|
|计数| 1318.000000|
|意思是| 10.666609|
|标准| 5.854462|
|分钟| -6.644779|
|25% | 6.354654|
|50% | 9.811106|
|75% | 14.335943|
|最大| 46.353073|
b_2 = 0.83e-12
c = 2.5e6
df.dropna(subset=['Y / X'], inplace=True)
Q1 = df['Y / X'].quantile(0.25)
Q3 = df['Y / X'].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
dfsum_copy2 = df[(df['Y / X'] >= lower_bound) & (df['Y / X'] <= upper_bound)]
x_data = df['c']
y_data = df['Y / X']
def fitting_function(delta_t, a, b_2):
return a * np.exp(-(b_1 - b_2) * c* delta_t)
x_data_normalized = (x_data - x_data.mean()) / x_data.std()
y_data_normalized = (y_data - y_data.mean()) / y_data.std()
initial_guess = [0.22, 1.25e-12]
fit_params, pcov = curve_fit(fitting_function, x_data_normalized, y_data_normalized, p0=initial_guess, method='lm')
a_fit, b_1_fit = fit_params
x_curve = np.linspace(min(x_data), max(x_data), 1000)
y_curve = fitting_function(x_curve, a_fit, b_1_fit)
plt.scatter(x_data, y_data, label='Data')
plt.plot(x_curve, y_curve, 'r-', label='Fitted Curve')
plt.xlabel('delta_t')
plt.ylabel('Y/ X')
plt.legend()
plt.grid(True)
plt.show()
a_fit, b_1_fit = fit_params
print("Fitted Parameters:")
print("a =", a_fit)
print("b_1 =", b_1)
当我尝试检查它的收敛性时:
np.linalg.cond(pcov)
LinAlgError Traceback (most recent call last)
Cell In[202], line 1
----> 1 np.linalg.cond(pcov)
File <array_function internals>:200, in cond(*args, **kwargs)
File /usr/local/lib/python3.11/site-packages/numpy/linalg/linalg.py:1747, in cond(x, p)
1745 raise LinAlgError("cond is not defined on empty arrays")
1746 if p is None or p == 2 or p == -2:
-> 1747 s = svd(x, compute_uv=False)
1748 with errstate(all='ignore'):
1749 if p == -2:
File <array_function internals>:200, in svd(*args, **kwargs)
File /usr/local/lib/python3.11/site-packages/numpy/linalg/linalg.py:1654, in svd(a, full_matrices, compute_uv, hermitian)
1651 gufunc = _umath_linalg.svd_n
1653 signature = 'D->d' if isComplexType(t) else 'd->d'
-> 1654 s = gufunc(a, signature=signature, extobj=extobj)
1655 s = s.astype(_realType(result_t), copy=False)
1656 return s
File /usr/local/lib/python3.11/site-packages/numpy/linalg/linalg.py:98, in _raise_linalgerror_svd_nonconvergence(err, flag)
97 def _raise_linalgerror_svd_nonconvergence(err, flag):
---> 98 raise LinAlgError("SVD did not converge")
LinAlgError: SVD did not converge
请帮我解决问题。
提前致谢。
问候 阿伦