请提供一段代码:用高斯分布拟合函数拟合数据
时间: 2024-05-01 17:17:41 浏览: 171
Sure, here's some Python code for fitting a Gaussian distribution to data using the scipy package:
```python
import numpy as np
from scipy.optimize import curve_fit
def gaussian(x, a, b, c):
return a * np.exp(-(x - b)**2 / (2 * c**2))
x_data = np.array([1, 2, 3, 4, 5])
y_data = np.array([1.2, 3.5, 7.1, 11.5, 15.8])
p0 = [1, np.mean(x_data), 1]
coeff, _ = curve_fit(gaussian, x_data, y_data, p0=p0)
print(f"mean = {coeff[1]}, std = {coeff[2]}")
```
This code defines a Gaussian function and uses `curve_fit` to fit the function to the `x_data` and `y_data` arrays. The resulting coefficients are printed to the console. Note that this is just an example and the actual dataset and initial guesses for the parameters would need to be specified for a real-world application.
阅读全文