polynomial-fit


Namepolynomial-fit JSON
Version 0.0.2 PyPI version JSON
download
home_pageNone
SummaryFit a polynomial of any order and dimension to a dataset
upload_time2025-01-20 01:16:13
maintainerNone
docs_urlNone
authorSeth Reed
requires_pythonNone
licenseMIT
keywords polynomial
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Polynomial Fit Package

This package provides tools for fitting multivariate polynomials to data, optionally subject to constraints. The package includes functionality for generating polynomial terms, fitting data, evaluating the fitted polynomial, computing its analytical derivative, and visualizing the results.

## Features

- Generate polynomial terms up to a specified order.
- Fit multivariate polynomials to data with or without constraints.
- Compute residuals, R-squared values, and RMSE.
- Differentiate multivariate polynomials with respect to specific variables.
- Examples of polynomial fits in one and two dimensions.

---

## Installation

Clone this repository and ensure you have the required dependencies installed.

### Dependencies

- Python 3.6+
- NumPy
- Matplotlib
- SciPy

Install the dependencies with:

```bash
pip install numpy matplotlib scipy
```

---

## Functions

### 1. `generate_terms(num_var, order)`
Generates all possible terms for a polynomial with a given number of variables and order.

#### Parameters:
- `num_var (int)`: Number of variables.
- `order (int)`: Order of the polynomial.

#### Returns:
- `list`: List of terms represented as lists of powers for each variable.

---

### 2. `polynomial_function(coeffs, X, terms)`
Evaluates the polynomial function for given inputs.

#### Parameters:
- `coeffs (ndarray)`: Coefficients of the polynomial.
- `X (ndarray)`: Input data.
- `terms (list)`: List of terms representing the polynomial.

#### Returns:
- `y (ndarray)`: Polynomial function values.

---

### 3. `polynomial_fit(input_data, output_data, order, input_constraints=None, output_constraints=None)`
Fits a polynomial to data with optional constraints.

#### Parameters:
- `input_data (ndarray)`: Input data.
- `output_data (ndarray)`: Observed outputs.
- `order (int)`: Order of the polynomial.
- `input_constraints (ndarray, optional)`: Input constraint values.
- `output_constraints (ndarray, optional)`: Output constraint values.

#### Returns:
- `tuple`: Optimal coefficients, terms, and fit metrics (R² and RMSE).

---

### 4. `differentiate_polynomial(coeffs, terms, x)`
Computes the derivative of the polynomial with respect to a specific variable.

#### Parameters:
- `coeffs (list)`: Coefficients of the polynomial.
- `terms (list)`: List of terms representing the polynomial.
- `x (list)`: A binary list specifying which variable to differentiate.

#### Returns:
- `tuple`: Differentiated coefficients and terms.

---

### 5. `polynomial_to_string(terms, coefficients, variables)`
Converts the polynomial to a human-readable string.

#### Parameters:
- `terms (list)`: List of polynomial terms.
- `coefficients (list)`: Coefficients of the terms.
- `variables (list)`: Variable names as strings.

#### Returns:
- `str`: Polynomial in string format.

---

### 6. `one_D_example()`
Provides an example of a 1D polynomial fit and visualizes the result.

---

### 7. `two_D_example()`
Provides an example of a 2D polynomial fit and visualizes the result in 3D.

---

## Usage

### Fitting a Polynomial

```python
import numpy as np
from polynomial_fit import polynomial_fit, polynomial_to_string

# Define data
x_data = np.linspace(0, 10, 50)
y_data = 2.5 * x_data ** 2 - 1.2 * x_data + 0.8

# Fit polynomial
order = 2
coeffs, terms, metrics = polynomial_fit(x_data, y_data, order)

# Print results
print("Polynomial:", polynomial_to_string(terms, coeffs, ["x"]))
print("Metrics:", metrics)
```

### Differentiating a Polynomial

```python
from polynomial_fit import differentiate_polynomial

derivative_coeffs, derivative_terms = differentiate_polynomial(coeffs, terms, x=[1])
print("Derivative:", polynomial_to_string(derivative_terms, derivative_coeffs, ["x"]))
```

---

## Examples

### 1D Fit Example
Run `one_D_example()` to visualize a 1D polynomial fit.

### 2D Fit Example
Run `two_D_example()` to visualize a 2D polynomial fit with a 3D plot.

---

## Contributing
Feel free to open issues or submit pull requests if you have suggestions for improvement.

---

## License
This project is licensed under the MIT License. See the LICENSE file for details.


2025-01-19 22:21 UTC - Package created

2025-01-20 01:10 UTC - Added polynomial derivative function. Enhanced README file.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "polynomial-fit",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "polynomial",
    "author": "Seth Reed",
    "author_email": "seth.reed01@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/68/81/62530a914b7e59264aa9703b7a5ee8238a7657c9863a197a9871bc0eb56a/polynomial_fit-0.0.2.tar.gz",
    "platform": null,
    "description": "# Polynomial Fit Package\r\n\r\nThis package provides tools for fitting multivariate polynomials to data, optionally subject to constraints. The package includes functionality for generating polynomial terms, fitting data, evaluating the fitted polynomial, computing its analytical derivative, and visualizing the results.\r\n\r\n## Features\r\n\r\n- Generate polynomial terms up to a specified order.\r\n- Fit multivariate polynomials to data with or without constraints.\r\n- Compute residuals, R-squared values, and RMSE.\r\n- Differentiate multivariate polynomials with respect to specific variables.\r\n- Examples of polynomial fits in one and two dimensions.\r\n\r\n---\r\n\r\n## Installation\r\n\r\nClone this repository and ensure you have the required dependencies installed.\r\n\r\n### Dependencies\r\n\r\n- Python 3.6+\r\n- NumPy\r\n- Matplotlib\r\n- SciPy\r\n\r\nInstall the dependencies with:\r\n\r\n```bash\r\npip install numpy matplotlib scipy\r\n```\r\n\r\n---\r\n\r\n## Functions\r\n\r\n### 1. `generate_terms(num_var, order)`\r\nGenerates all possible terms for a polynomial with a given number of variables and order.\r\n\r\n#### Parameters:\r\n- `num_var (int)`: Number of variables.\r\n- `order (int)`: Order of the polynomial.\r\n\r\n#### Returns:\r\n- `list`: List of terms represented as lists of powers for each variable.\r\n\r\n---\r\n\r\n### 2. `polynomial_function(coeffs, X, terms)`\r\nEvaluates the polynomial function for given inputs.\r\n\r\n#### Parameters:\r\n- `coeffs (ndarray)`: Coefficients of the polynomial.\r\n- `X (ndarray)`: Input data.\r\n- `terms (list)`: List of terms representing the polynomial.\r\n\r\n#### Returns:\r\n- `y (ndarray)`: Polynomial function values.\r\n\r\n---\r\n\r\n### 3. `polynomial_fit(input_data, output_data, order, input_constraints=None, output_constraints=None)`\r\nFits a polynomial to data with optional constraints.\r\n\r\n#### Parameters:\r\n- `input_data (ndarray)`: Input data.\r\n- `output_data (ndarray)`: Observed outputs.\r\n- `order (int)`: Order of the polynomial.\r\n- `input_constraints (ndarray, optional)`: Input constraint values.\r\n- `output_constraints (ndarray, optional)`: Output constraint values.\r\n\r\n#### Returns:\r\n- `tuple`: Optimal coefficients, terms, and fit metrics (R\u00c2\u00b2 and RMSE).\r\n\r\n---\r\n\r\n### 4. `differentiate_polynomial(coeffs, terms, x)`\r\nComputes the derivative of the polynomial with respect to a specific variable.\r\n\r\n#### Parameters:\r\n- `coeffs (list)`: Coefficients of the polynomial.\r\n- `terms (list)`: List of terms representing the polynomial.\r\n- `x (list)`: A binary list specifying which variable to differentiate.\r\n\r\n#### Returns:\r\n- `tuple`: Differentiated coefficients and terms.\r\n\r\n---\r\n\r\n### 5. `polynomial_to_string(terms, coefficients, variables)`\r\nConverts the polynomial to a human-readable string.\r\n\r\n#### Parameters:\r\n- `terms (list)`: List of polynomial terms.\r\n- `coefficients (list)`: Coefficients of the terms.\r\n- `variables (list)`: Variable names as strings.\r\n\r\n#### Returns:\r\n- `str`: Polynomial in string format.\r\n\r\n---\r\n\r\n### 6. `one_D_example()`\r\nProvides an example of a 1D polynomial fit and visualizes the result.\r\n\r\n---\r\n\r\n### 7. `two_D_example()`\r\nProvides an example of a 2D polynomial fit and visualizes the result in 3D.\r\n\r\n---\r\n\r\n## Usage\r\n\r\n### Fitting a Polynomial\r\n\r\n```python\r\nimport numpy as np\r\nfrom polynomial_fit import polynomial_fit, polynomial_to_string\r\n\r\n# Define data\r\nx_data = np.linspace(0, 10, 50)\r\ny_data = 2.5 * x_data ** 2 - 1.2 * x_data + 0.8\r\n\r\n# Fit polynomial\r\norder = 2\r\ncoeffs, terms, metrics = polynomial_fit(x_data, y_data, order)\r\n\r\n# Print results\r\nprint(\"Polynomial:\", polynomial_to_string(terms, coeffs, [\"x\"]))\r\nprint(\"Metrics:\", metrics)\r\n```\r\n\r\n### Differentiating a Polynomial\r\n\r\n```python\r\nfrom polynomial_fit import differentiate_polynomial\r\n\r\nderivative_coeffs, derivative_terms = differentiate_polynomial(coeffs, terms, x=[1])\r\nprint(\"Derivative:\", polynomial_to_string(derivative_terms, derivative_coeffs, [\"x\"]))\r\n```\r\n\r\n---\r\n\r\n## Examples\r\n\r\n### 1D Fit Example\r\nRun `one_D_example()` to visualize a 1D polynomial fit.\r\n\r\n### 2D Fit Example\r\nRun `two_D_example()` to visualize a 2D polynomial fit with a 3D plot.\r\n\r\n---\r\n\r\n## Contributing\r\nFeel free to open issues or submit pull requests if you have suggestions for improvement.\r\n\r\n---\r\n\r\n## License\r\nThis project is licensed under the MIT License. See the LICENSE file for details.\r\n\r\n\r\n2025-01-19 22:21 UTC - Package created\r\n\r\n2025-01-20 01:10 UTC - Added polynomial derivative function. Enhanced README file.\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Fit a polynomial of any order and dimension to a dataset",
    "version": "0.0.2",
    "project_urls": null,
    "split_keywords": [
        "polynomial"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "688162530a914b7e59264aa9703b7a5ee8238a7657c9863a197a9871bc0eb56a",
                "md5": "3b5d4a2415608c7ce84550ea444be0da",
                "sha256": "4dd0e3776b9e5123841b39f3b7a0a6599373900b3196e2876b9544023955bed9"
            },
            "downloads": -1,
            "filename": "polynomial_fit-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "3b5d4a2415608c7ce84550ea444be0da",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 7697,
            "upload_time": "2025-01-20T01:16:13",
            "upload_time_iso_8601": "2025-01-20T01:16:13.037231Z",
            "url": "https://files.pythonhosted.org/packages/68/81/62530a914b7e59264aa9703b7a5ee8238a7657c9863a197a9871bc0eb56a/polynomial_fit-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-20 01:16:13",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "polynomial-fit"
}
        
Elapsed time: 2.05307s