Skip to main content

Machine Learning for Curve Fitting Parameter Estimation

Project description

ZeroGuess: Machine Learning for Curve Fitting Parameter Estimation

Build Status Coverage Status License: MIT Python Versions Benchmark Results

ZeroGuess uses machine learning to improve curve fitting parameter estimation. It generates optimal starting parameters for fitting functions, reducing computation time and increasing fitting reliability.

Problem Statement

Curve fitting in scientific applications often encounters issues with initial parameter estimation:

  • Convergence to local rather than global minima
  • High computational cost for complex functions
  • Failed convergence with inadequate starting points

Conventional approaches rely on domain expertise, manual adjustment, or computationally intensive global optimization methods.

ZeroGuess addresses these limitations through machine learning models trained on synthetic data, providing effective parameter estimates that allow simpler fitting algorithms to achieve results comparable to advanced methods with significantly reduced computation time.

Installation

pip install zeroguess

Quick Start

Setup experimental data

import numpy as np
from zeroguess.functions import WaveletFunction, add_gaussian_noise

# Create a double peakGaussian function
wavelet = WaveletFunction()

# Create some experimental data
true_params = wavelet.get_random_params()
x_data = np.linspace(-10, 10, 100)
y_data = add_gaussian_noise(wavelet(x_data, **true_params), sigma=0.1)

lmfit Integration

from zeroguess.integration import ZeroGuessModel

# Enhanced lmfit Model with parameter estimation
model = ZeroGuessModel(
    wavelet,
    independent_vars_sampling={"x": x_data},
    estimator_settings={
        "make_canonical": wavelet.get_canonical_params,
        # Configure training parameters
        # "n_samples": 1000,
        # "n_epochs": 200,
        # "validation_split": 0.2,
        # "add_noise": True,
        # "noise_level": 0.1,
        # 'verbose': True
        "snapshot_path": "model_dg.pth", # saves and loads model automatically
    },
)

model.set_param_hint("frequency", min=0.05, max=1.0)
model.set_param_hint("phase", min=0.0, max=2.0 * np.pi)
model.set_param_hint("position", min=5.0, max=15.0)
model.set_param_hint("width", min=0.1, max=3.0)

# Standard lmfit workflow
params = model.guess(y_data, x=x_data)

result = model.fit(y_data, x=x_data, params=params)

Scipy usage

import zeroguess
from scipy import optimize

# Create and train parameter estimator
estimator = zeroguess.create_estimator(
    function=wavelet,
    param_ranges={
        "frequency": (0.05, 1.0),
        "phase": (0.0, 2.0 * np.pi),
        "position": (5.0, 15.0),
        "width": (0.1, 3.0),
    },
    independent_vars_sampling={
        'x': x_data
    },
    snapshot_path="model_dg_plain.pth", # saves and loads model automatically
)

if not estimator.is_trained:
    estimator.train()

# Get parameter estimates for experimental data
initial_params = estimator.predict(x_data, y_data)

# Use in standard curve fitting
optimal_params, _ = optimize.curve_fit(
    wavelet, x_data, y_data,
    p0=list(initial_params.values())
)

Background

Canonical Representations

Many fitting functions produce identical outputs with different parameter combinations. For example, in a double Gaussian function, swapping the parameters of the two peaks produces the same curve:

# These two parameter sets produce identical curves
params1 = {"amplitude1": 2, "center1": -2, "width1": 1, 
           "amplitude2": 3, "center2": 2, "width2": 0.5}
params2 = {"amplitude1": 3, "center2": 2, "width2": 0.5,
           "amplitude2": 2, "center1": -2, "width1": 1}

You can provide ZeroGuess a function for make_canonical to transform parameters into a standardized form (e.g., ordering peaks from left to right), enabling the model to learn a consistent mapping and provide more reliable parameter estimates.

Performance and Advanced Fitting Methods

Accurate starting parameters are essential for local optimization methods like least_squares. Global optimization methods like dual_annealing find better solutions but require substantially more computation.
Benchmark Results

Benchmarks indicate that ZeroGuess improves the performance of least_squares fits to comparable levels with dual_annealing (~70% success rate) while reducing computation time by a factor of 100 (from 1150ms to 12ms).

This makes ZeroGuess useful for complex fitting functions with multiple parameters and for large datasets where computation efficiency is important.

Features

  • Automatic estimation of starting parameters for curve fitting
  • Support for both SciPy and lmfit curve fitting libraries
  • Neural network-based parameter estimation
  • Model persistence for reuse without retraining
  • Detailed diagnostics and visualization tools

Requirements

  • Python 3.10+
  • Dependencies: numpy, scipy, torch, lmfit (optional)

License

MIT

Performance Benchmarks

ZeroGuess is benchmarked regularly to ensure optimal performance. View the latest benchmark results.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zeroguess-0.8.2.tar.gz (33.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zeroguess-0.8.2-py3-none-any.whl (42.3 kB view details)

Uploaded Python 3

File details

Details for the file zeroguess-0.8.2.tar.gz.

File metadata

  • Download URL: zeroguess-0.8.2.tar.gz
  • Upload date:
  • Size: 33.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for zeroguess-0.8.2.tar.gz
Algorithm Hash digest
SHA256 70caaf02ef8a1956924ad8b235a61fee95e8c7a34d51486e8d9970c0985e152f
MD5 457a9dda7c09d94d3bc8a5dd62ab7a64
BLAKE2b-256 f34bd102d4cfd03eed4212639b4b559ec7367be5870662b5799cb732f909e889

See more details on using hashes here.

File details

Details for the file zeroguess-0.8.2-py3-none-any.whl.

File metadata

  • Download URL: zeroguess-0.8.2-py3-none-any.whl
  • Upload date:
  • Size: 42.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for zeroguess-0.8.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dce0f7c832d88cdce5195066173f10ca99f2c072952e9beef91b9a79261e22c7
MD5 b83fd3ef7cce9f439765cec0faacf492
BLAKE2b-256 caa6406b78341eecb942083edd2a1f4f77d53a1cbba302f81d8fe68872d44fb1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page