{"id":19418494,"url":"https://github.com/sudipto3331/linear-polynomial-regression-numerical-method-implementation-in-python","last_synced_at":"2025-02-25T03:42:24.877Z","repository":{"id":254980083,"uuid":"848161428","full_name":"sudipto3331/Linear-Polynomial-Regression-Numerical-Method-Implementation-in-Python","owner":"sudipto3331","description":"This repository contains a Python implementation of Linear Polynomial Regression for fitting a polynomial to a set of data points using the Gauss-Seidel method for solving the system of linear equations. The code reads data from an Excel file (`Regression.xls`), performs polynomial regression.","archived":false,"fork":false,"pushed_at":"2024-08-27T08:50:39.000Z","size":47,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-07T17:30:07.674Z","etag":null,"topics":["excel","linear-regression","numerical-analysis","numerical-methods","plot","python3"],"latest_commit_sha":null,"homepage":"http://sudiptomondal.me/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sudipto3331.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-27T08:47:30.000Z","updated_at":"2024-08-27T08:51:20.000Z","dependencies_parsed_at":"2024-08-27T10:01:02.610Z","dependency_job_id":null,"html_url":"https://github.com/sudipto3331/Linear-Polynomial-Regression-Numerical-Method-Implementation-in-Python","commit_stats":null,"previous_names":["sudipto3331/linear-polynomial-regression-numerical-method-implementation-in-python"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sudipto3331%2FLinear-Polynomial-Regression-Numerical-Method-Implementation-in-Python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sudipto3331%2FLinear-Polynomial-Regression-Numerical-Method-Implementation-in-Python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sudipto3331%2FLinear-Polynomial-Regression-Numerical-Method-Implementation-in-Python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sudipto3331%2FLinear-Polynomial-Regression-Numerical-Method-Implementation-in-Python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sudipto3331","download_url":"https://codeload.github.com/sudipto3331/Linear-Polynomial-Regression-Numerical-Method-Implementation-in-Python/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240599180,"owners_count":19826959,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["excel","linear-regression","numerical-analysis","numerical-methods","plot","python3"],"created_at":"2024-11-10T13:14:08.211Z","updated_at":"2025-02-25T03:42:24.860Z","avatar_url":"https://github.com/sudipto3331.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Linear Polynomial Regression Numerical Method Implementation in Python\n\nThis repository contains a Python implementation of Linear Polynomial Regression for fitting a polynomial to a set of data points using the Gauss-Seidel method for solving the system of linear equations. The code reads data from an Excel file (`Regression.xls`), performs polynomial regression, and plots the original data points along with the fitted polynomial curve.\n\n## Table of Contents\n- [Linear Polynomial Regression Theory](#linear-polynomial-regression-theory)\n- [Dependencies](#dependencies)\n- [Installation](#installation)\n- [Usage](#usage)\n- [Code Explanation](#code-explanation)\n- [Example](#example)\n- [Files in the Repository](#files-in-the-repository)\n- [Input Parameters](#input-parameters)\n- [Troubleshooting](#troubleshooting)\n- [Author](#author)\n\n## Linear Polynomial Regression Theory\nLinear Polynomial Regression is a form of regression analysis where the relationship between the independent variable \\( x \\) and the dependent variable \\( y \\) is modeled as an \\( m \\)-degree polynomial. The Gauss-Seidel method is an iterative technique used to solve the system of equations derived from the normal equations of the regression problem.\n\n**Steps:**\n1. Formulate the normal equations from the polynomial regression problem.\n2. Use the Gauss-Seidel method to iteratively solve the system of linear equations.\n3. Obtain the polynomial coefficients and plot the regression curve.\n\n## Dependencies\nTo run this code, you need the following libraries:\n- `numpy`\n- `xlrd`\n- `matplotlib`\n\n## Installation\nTo install the required libraries, you can use `pip`:\n```sh\npip install numpy xlrd matplotlib\n```\n\n## Usage\n1. Clone the repository.\n2. Ensure the script and the Excel file (`Regression.xls`) are in the same directory.\n3. Run the script using Python:\n    ```sh\n    python polynomial_regression.py\n    ```\n4. Provide the required input when prompted:\n    - Enter the order of the regression polynomial.\n\n## Code Explanation\nThe code begins by importing the necessary libraries and defining the Gauss-Seidel function for solving systems of linear equations. It reads the data points from the Excel file and constructs the normal equations for the polynomial regression problem. The Gauss-Seidel method is used to solve for the polynomial coefficients, which are then used to compute the fitted values. The original data points and the fitted polynomial curve are plotted.\n\nBelow is a snippet from the code illustrating the main logic:\n\n```python\nimport numpy as np\nimport xlrd\nfrom matplotlib import pyplot as plt\n\ndef brcond_Seidel(E, err, n):\n    a = 0\n    for i in range(n):\n        if E[i] \u003c err:\n            a += 1\n    return a / n\n\ndef Gauss_Seidel(a):\n    err = 10**-15         # Assumed percentage relative error\n    ite = 10**4          # Assumed number of iterations\n    \n    n = np.shape(a)[0]\n    E = np.zeros([n])\n    rel_err = np.zeros([ite, n])\n    X = np.zeros([n])\n    x = np.zeros([ite, n])\n    itern = np.zeros([ite])\n    p = np.zeros([n])\n    \n    for j in range(ite):\n        itern[j] = j + 1\n        for i in range(n):\n            summation = 0\n            for k in range(n):\n                if k != i:\n                    summation += a[i, k] * x[j, k]\n            x[j, i] = (a[i, n] - summation) / a[i, i]\n            if j \u003e 0:\n                rel_err[j, i] = abs((x[j, i] - x[j-1, i]) / x[j, i]) * 100\n                E[i] = rel_err[j, i]\n        if j \u003e 0 and brcond_Seidel(E, err, n) == 1:\n            break\n        if j == ite - 1:\n            break\n        x[j+1, :] = x[j, :]\n    \n    X = x[j, :]\n    \n    # Result Verification    \n    for i in range(n):\n        summation = 0\n        for j in range(n):\n            summation += a[i, j] * X[j]\n        p[i] = summation - a[i, j+1]\n    \n    return X, p\n\nm = int(input('Enter the order of the regression polynomial: '))\n\n# Reading data from excel file\nloc = ('Regression.xls')\nwb = xlrd.open_workbook(loc)\nsheet = wb.sheet_by_index(3)\n\nN = sheet.ncols - 1\nx = np.zeros([N])\ny = np.zeros([N])\nY = np.zeros([N])\na = np.zeros([m+1, m+2])\n\n# Creating matrix from the data \nfor i in range(1, sheet.ncols):\n    x[i-1] = sheet.cell_value(0, i)\n    y[i-1] = sheet.cell_value(1, i)\n\nfor p in range(m+1):\n    for q in range(m+2):\n        if p == 0 and q == 0:\n            a[p, q] = N\n        elif p == 0:\n            a[p, q] = sum(x**q)\n        elif q \u003c m+1:\n            a[p, q] = sum(x**(q+p))\n        else:\n            a[p, q] = sum((x**p) * y)\n\nX, p = Gauss_Seidel(a)\n\nfor Z in range(N):\n    addition = 0\n    for v in range(m+1):\n        addition += X[v] * x[Z]**v\n    Y[Z] = addition\n\nplt.figure(1)\nplt.plot(x, y, 'o')\nplt.plot(x, Y)\nplt.xlabel('Values of x')\nplt.ylabel('Values of y')\nplt.title('Curve fitting using polynomial regression')\nplt.legend(['Measured', 'Estimated'], loc='upper left')\nplt.show()\n```\n\nThe code completes by plotting the original data points and the fitted polynomial curve using `matplotlib`.\n\n## Example\nBelow is an example of how to use the script:\n\n1. Prepare the `Regression.xls` file with the data points in two rows: the first row for \\( x \\)-values and the second row for \\( y \\)-values.\n2. **Run the script**:\n    ```sh\n    python polynomial_regression.py\n    ```\n\n3. **Enter the input value**:\n    ```\n    Enter the order of the regression polynomial: 2\n    ```\n\n4. **Output**:\n    - The script will compute the polynomial regression and plot the original data points along with the fitted polynomial curve.\n\n## Files in the Repository\n- `polynomial_regression.py`: The main script for performing polynomial regression.\n- `Regression.xls`: Excel file from which the data points are read.\n\n## Input Parameters\nThe initial input data is expected to be in the form of two rows within the `Regression.xls` file:\n- First row: \\( x \\)-values\n- Second row: \\( y \\)-values\n\n## Troubleshooting\n1. **Excel File**: Ensure that the input data is correctly formatted and placed in the `Regression.xls` file.\n2. **Order of Polynomial**: The order of the polynomial should be a non-negative integer and less than the number of data points.\n3. **Python Version**: This script is compatible with Python 3. Ensure you have Python 3 installed.\n\n## Author\nScript created by Sudipto.\n\n---\n\nThis documentation should guide you through understanding, installing, and using the polynomial regression script. For further issues or feature requests, please open an issue in the repository on GitHub. Feel free to contribute by creating issues and submitting pull requests. Happy coding!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsudipto3331%2Flinear-polynomial-regression-numerical-method-implementation-in-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsudipto3331%2Flinear-polynomial-regression-numerical-method-implementation-in-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsudipto3331%2Flinear-polynomial-regression-numerical-method-implementation-in-python/lists"}