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

如何使用Python将erf函数拟合至给定数据集?

Fitting an ERF Function to Your Sigmoidal Dataset

Hey there! Let's figure out how to fit an erf function to your dataset—you’re already halfway there since you know what erf is and have messed around with some code. Let’s break this down step by step.

First: The Right ERF Model for Your Data

Your data has a classic sigmoidal shape (flat low end, sharp rise, flat high end), which aligns perfectly with an erf-based model. The standard form we’ll use is:
Y = A + B * erf(C*(X - D))

Here’s what each parameter does, tailored to your dataset:

  • A: The midpoint between your lower and upper plateaus (since erf ranges from -1 to 1, this shifts the curve to sit between your two flat regions)
  • B: Half the difference between your upper and lower plateau values (controls the total height of the rise)
  • C: Controls how steep the transition between plateaus is (higher = sharper rise)
  • D: The X-value where the curve crosses the midpoint (the center of your transition region)

From your data, we can make quick initial guesses to help the fitting algorithm converge:

  • Lower plateau ≈ 21, upper ≈79 → A = (21+79)/2 = 50, B=(79-21)/2=29
  • Transition happens between X=41 and X=47 → D≈44
  • Start with C=0.1 (we can tweak if needed)

Step-by-Step Implementation (Python Example)

I’ll use Python with scipy for fitting and matplotlib for visualization—this is the most common approach, and it should align with code you’ve seen in threads.

1. Import Required Libraries

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

2. Define the ERF Fitting Function

def erf_model(x, A, B, C, D):
    return A + B * np.erf(C * (x - D))

3. Load Your Dataset

# Your X and Y values as numpy arrays
x_data = np.array([33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75])
y_data = np.array([21.09,21.14,21.21,21.32,21.46,23,27,31,36,40,45,49,53,57,61,65,69,73,77,78,79,79])

4. Set Initial Parameter Guesses

(Critical for good convergence—don’t skip this!)

initial_guess = [50, 29, 0.1, 44]

5. Run the Curve Fit

# Perform the fit; params will hold our optimized values
params, params_covariance = curve_fit(erf_model, x_data, y_data, p0=initial_guess)

6. Extract and Inspect Fitted Parameters

A_fit, B_fit, C_fit, D_fit = params
print(f"Fitted parameters:")
print(f"A = {A_fit:.2f} (midpoint of plateaus)")
print(f"B = {B_fit:.2f} (half the plateau difference)")
print(f"C = {C_fit:.4f} (steepness of transition)")
print(f"D = {D_fit:.2f} (X midpoint of transition)")

7. Plot to Verify the Fit

# Create a smooth range of X values for plotting the fitted curve
x_fit = np.linspace(30, 80, 100)
y_fit = erf_model(x_fit, A_fit, B_fit, C_fit, D_fit)

# Plot original data and fitted curve
plt.scatter(x_data, y_data, color='blue', label='Original Data')
plt.plot(x_fit, y_fit, 'r-', linewidth=2, label='Fitted ERF Model')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('ERF Model Fit to Your Dataset')
plt.legend()
plt.show()

Troubleshooting Tips

  • If the fit doesn’t converge: Adjust the initial guess for C (try 0.05 or 0.2) or add bounds to restrict parameters to reasonable ranges. For example:
    # Bounds: [min_A, min_B, min_C, min_D], [max_A, max_B, max_C, max_D]
    bounds = ([48, 27, 0.01, 40], [52, 31, 0.5, 50])
    params, params_covariance = curve_fit(erf_model, x_data, y_data, p0=initial_guess, bounds=bounds)
    
  • Double-check the model: Make sure you’re using the standard np.erf (ranges -1 to 1). If you were using a scaled erf (like from 0 to 1), you’d need to adjust the model to Y = lower_plateau + (upper_plateau - lower_plateau) * (0.5 * (1 + erf(C*(X-D))))—but the first model we used is more straightforward for your data.

Give this a shot—your dataset is clean, so the fit should be really tight. Let me know if you hit any snags!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:39:12