You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于多元回归的高斯求和问题:用单高斯曲线拟合16条高斯曲线

Fitting a Single Gaussian to the Sum of 16 Gaussians in Python

It sounds like you’re stuck trying to fit one Gaussian curve to the combined signal of 16 individual Gaussians using a regression-based approach. The main issue here is that Gaussian functions are non-linear in their core parameters (mean and standard deviation), so linear/multivariate regression methods won’t work well for this task. Instead, let’s use a reliable non-linear optimization approach with scipy.optimize.curve_fit—it’s designed exactly for this kind of curve fitting problem.

Here’s a step-by-step solution you can adapt to your data:

Step 1: Import Required Libraries

First, let’s load the tools we’ll need:

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

Step 2: Define Gaussian Functions

We’ll need two functions: one for a single Gaussian, and another to compute the sum of your 16 Gaussians:

def single_gaussian(x, amp, mean, std):
    """Define a single Gaussian curve."""
    return amp * np.exp(-((x - mean)**2) / (2 * std**2))

def sum_of_gaussians(x, *params):
    """Compute the sum of multiple Gaussians.
    Params should be grouped as: amp1, mean1, std1, amp2, mean2, std2, ..."""
    y = np.zeros_like(x)
    for i in range(0, len(params), 3):
        amp = params[i]
        mean = params[i+1]
        std = params[i+2]
        y += amp * np.exp(-((x - mean)**2)/(2*std**2))
    return y

Step 3: Prepare Your Data

Your v1 array appears to be your x-values. Note that 16 Gaussians require 48 parameters (3 per Gaussian: amplitude, mean, standard deviation)—your a array only has 16 elements, so you’ll need to fill in the missing parameters for each of the 16 curves. For demonstration, I’ll use sample parameters, but you can replace these with your actual values:

# Use your actual v1 array here (I'll generate a sample x-range for example)
x = np.array([2.5470357695283954, 0.1937004980283323, 0.43831655553839766, 6.07645636407398, 0.6331239135554633, 0.96993730864557])  # Your v1 snippet
x = np.sort(x)  # Sort x-values for cleaner plotting

# Example: Complete parameters for 16 Gaussians (replace with your full parameter set)
# Format: [amp1, mean1, std1, amp2, mean2, std2, ..., amp16, mean16, std16]
np.random.seed(42)  # For reproducibility
full_params = []
for _ in range(16):
    full_params.extend([np.random.uniform(1, 8), np.random.uniform(0, 7), np.random.uniform(0.2, 1)])

# Compute the sum of your 16 Gaussians
y_sum = sum_of_gaussians(x, *full_params)

Step 4: Fit the Single Gaussian

We’ll use curve_fit to find the best-fit parameters. Good initial guesses are key to getting a reliable fit:

# Initial guesses (adjust these based on your data if needed)
initial_amp = np.max(y_sum)  # Peak amplitude of the summed signal
initial_mean = x[np.argmax(y_sum)]  # X-value of the peak
initial_std = (x.max() - x.min()) / 4  # Rough width estimate

# Perform the non-linear least squares fit
popt, pcov = curve_fit(single_gaussian, x, y_sum, p0=[initial_amp, initial_mean, initial_std])

# Extract fitted parameters
fit_amp, fit_mean, fit_std = popt

Step 5: Visualize the Results

Let’s plot the summed signal and our fitted Gaussian to check how well it matches:

plt.figure(figsize=(10, 6))
plt.plot(x, y_sum, 'b.', label='Sum of 16 Gaussians')
plt.plot(x, single_gaussian(x, fit_amp, fit_mean, fit_std), 'r--', linewidth=2, 
         label=f'Fitted Gaussian\nAmp: {fit_amp:.2f}, Mean: {fit_mean:.2f}, Std: {fit_std:.2f}')
plt.xlabel('X Values')
plt.ylabel('Signal')
plt.legend()
plt.title('Fitting a Single Gaussian to 16 Combined Gaussians')
plt.show()

Key Notes

  • Why regression didn’t work: Gaussian functions are non-linear in their mean and standard deviation parameters, so linear/multivariate regression can’t properly optimize these values. Non-linear optimizers like curve_fit are far more effective here.
  • Poor fit? If the result isn’t great, tweak your initial guesses (especially the mean and standard deviation) or check that your full parameter set for the 16 Gaussians is correct.

内容的提问来源于stack exchange,提问作者Aleena Rehman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:33:09