{"id":20904363,"url":"https://github.com/magnitopic/ft-linear-regression","last_synced_at":"2026-04-16T05:01:31.451Z","repository":{"id":260632924,"uuid":"879565899","full_name":"magnitopic/ft-linear-regression","owner":"magnitopic","description":"Your first implementation of a machine learning algorithm. Predicting the price of a car by it's milage","archived":false,"fork":false,"pushed_at":"2024-11-09T23:01:03.000Z","size":16,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-12T21:23:34.430Z","etag":null,"topics":["42school","ai","ai-algorithm","ft-linear-regression","linear-regression","machine-learning","matplotlib","numpy","pandas","prediction-algorithm","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/magnitopic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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-10-28T06:33:17.000Z","updated_at":"2024-11-11T09:48:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"9030b6c8-95c8-4e05-bf20-be3b90836a50","html_url":"https://github.com/magnitopic/ft-linear-regression","commit_stats":null,"previous_names":["magnitopic/ft-linear-regression"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/magnitopic/ft-linear-regression","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magnitopic%2Fft-linear-regression","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magnitopic%2Fft-linear-regression/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magnitopic%2Fft-linear-regression/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magnitopic%2Fft-linear-regression/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/magnitopic","download_url":"https://codeload.github.com/magnitopic/ft-linear-regression/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magnitopic%2Fft-linear-regression/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268489197,"owners_count":24258390,"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","status":"online","status_checked_at":"2025-08-03T02:00:12.545Z","response_time":2577,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["42school","ai","ai-algorithm","ft-linear-regression","linear-regression","machine-learning","matplotlib","numpy","pandas","prediction-algorithm","python"],"created_at":"2024-11-18T13:16:52.922Z","updated_at":"2026-04-16T05:01:26.382Z","avatar_url":"https://github.com/magnitopic.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ft-linear-regression\n\nYour first implementation of a machine learning algorithm.\n\n\u003cdiv align=\"center\"\u003e\n\u003cimg width=\"512\" alt=\"Screenshot 2024-11-03 at 10 10 38\" src=\"https://github.com/user-attachments/assets/b6e46a98-1b76-4de9-86db-ed1210a772e7\"\u003e\n\u003c/div\u003e\n\n## Description\n\nThis project is about implementing a simple linear regression algorithm. The goal is to predict the price of a car based on its mileage.\n\nTo do this we will use the following formula:\n\n$$ y = \\theta_0 + \\theta_1 \\cdot x$$\n\nWhere:\n\n-   $y$ is the price of the car (dependent variable)\n-   $x$ is the mileage of the car (independent variable)\n-   $\\theta_0$ is the intercept\n-   $\\theta_1$ is the slope\n\nTo figure out the values of $\\theta_0$ and $\\theta_1$ we will use the [gradient descent algorithm](https://en.wikipedia.org/wiki/Gradient_descent).\n\n## Clone and run project\n\n```\ngit clone https://github.com/magnitopic/ft-linear-regression.git\n\ncd ft-linear-regression\n\npip install -r requirements.txt\n\npython src/main.py\n```\n\n## Math\n\n$$ y = \\theta_0 + \\theta_1 \\cdot x$$\n\n$$ERROR = \\frac{1}{m} \\sum_{i=1}^{m} (\\widehat{y}-y)^2$$\n\nCost function to minimize:\n\n$$J = \\frac{1}{2m} \\sum_{i=1}^{m} (\\widehat{y}-y)^2$$\n\n$$J = \\frac{1}{2m} \\sum_{i=1}^{m} (\\widehat{y}-(\\theta_0 + \\theta_1 \\cdot x))^2$$\n\nTo minimize the cost, we need the partial derivative of the cost function.\n\n\u003e The gradient is: $\\nabla J = \\left( \\frac{\\partial J}{\\partial \\theta_0} , \\frac{\\partial J}{\\partial \\theta_1} \\right) $\n\n$$\\frac{\\partial J}{\\partial \\theta_0} = \\frac{-1}{m} \\sum_{i=1}^{m} (\\widehat{y}-y)$$\n\n$$\\frac{\\partial J}{\\partial \\theta_1} = \\frac{-1}{m} \\sum_{i=1}^{m} (\\widehat{y}-y) \\cdot x$$\n\n$$ \\theta_0 := \\alpha \\cdot \\left( \\frac{\\partial J}{\\partial \\theta_0} \\right) = \\alpha \\left( \\frac{-1}{m} \\sum(\\widehat{y} - y) \\right) $$\n\n$$ \\theta_1 := \\alpha \\cdot \\left( \\frac{\\partial J}{\\partial \\theta_1} \\right) = \\alpha \\left( \\frac{-1}{m} \\sum x\\cdot(\\widehat{y} - y) \\right) $$\n\n## Evaluation of the Model: Coefficient of Determination ($R^2$)\n\n$R^2$ is a metric that varies between 0 and 1, where:\n\n-   $R^2 = 0$: There is no linear relationship between the variables\n-   $R^2 = 1$: There is a perfect linear relationship between the variables (all points are on the line)\n-   $R^2 \\approx 0.8$: Good fit in many contexts\n-   $R^2 \u003c 0.2$: Poor fit, suggests non-linear relationship or no relationship at all\n\n```math\nR^2 = 1 - \\frac{\\sum_{i=1}^{n} (Y_i - \\widehat{Y}_i)^2}{\\sum_{i=1}^{n} (Y_i - \\bar{Y})^2}\n```\n\n-   $\\sum_{i=1}^{n} (Y_i - \\widehat{Y}_i)^2$ calculates the sum of squared residuals (difference between observed and predicted values).\n\n-   $\\sum_{i=1}^{n} (Y_i - \\bar{Y})^2$ calculates the total sum of squares (total variation in the data).\n\n## Correlation vs Causation\n\nIt's important in this statistics example to remember that:\n\n-   Correlation does not imply causation\n-   There may exist false correlations\n    \u003e [As shown by this website](https://www.tylervigen.com/spurious-correlations)\n-   The value of $R^2$ should be interpreted with the specific context in mind\n    -   In social sciences, a value of $R^2$ of 0.7 is considered high\n    -   In physics, a value of $R^2$ of 0.7 is considered low\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagnitopic%2Fft-linear-regression","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmagnitopic%2Fft-linear-regression","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagnitopic%2Fft-linear-regression/lists"}