{"id":182969,"date":"2026-02-19T20:22:00","date_gmt":"2026-02-19T19:22:00","guid":{"rendered":"https:\/\/liora.io\/en\/?p=182969"},"modified":"2026-02-19T20:22:01","modified_gmt":"2026-02-19T19:22:01","slug":"linear-regression-with-python-how-does-it-work","status":"publish","type":"post","link":"https:\/\/liora.io\/en\/linear-regression-with-python-how-does-it-work","title":{"rendered":"Linear regression with Python: How does it work?"},"content":{"rendered":"<p><strong>A key algorithm in Machine Learning, linear regression is used to establish relationships between one or more variables. To put this algorithm into practice with ease, data scientists can turn to programming languages, particularly Python. So how do you use linear regression with Python? Liora answers the question.<\/strong><\/p>\t\t\n\t\t\t<h2>What is linear regression?<\/h2>\t\t\n\t\t<p>Before looking at the practical use of <strong>linear regression with Python, we need to go back to basics.<\/strong><\/p>\t\t\n\t\t\t<h3>Linear regression &#8211; Definition<\/h3>\t\t\n\t\t<p>The<strong> linear regression model<\/strong> is a supervised learning algorithm used to predict a continuous target variable (dependent variable) from one or more explanatory variables (independent or predictive variables). In other words, it establishes relationships between 2 or more variables.<\/p><p>When there is only one explanatory variable, we speak of simple linear regression. On the other hand, if there are several, we speak of<strong> multiple linear regression.<\/strong><\/p><p>Whether simple or multiple, linear regression can be used with Python.<\/p><p>&nbsp;<\/p><p>?Related articles:<\/p><table dir=\"ltr\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" data-sheets-root=\"1\"><colgroup><col width=\"656\"><\/colgroup><tbody><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;PyCharm: all about the most popular Python IDE&quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/pycharm-all-about-the-most-popular-python-ide\"><a href=\"https:\/\/liora.io\/en\/pycharm-all-about-the-most-popular-python-ide\" target=\"_blank\" rel=\"noopener\">PyCharm: all about the most popular Python IDE<\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Pandas : the Python library dedicated to Data Science&quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/pandas-the-python-library\"><a href=\"https:\/\/liora.io\/en\/pandas-the-python-library\" target=\"_blank\" rel=\"noopener\">Pandas : the Python library dedicated to Data Science<\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Seaborn : all about the Data Visualization tool in Python&quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/seaborn_and_data_visualization\"><a href=\"https:\/\/liora.io\/en\/seaborn_and_data_visualization\" target=\"_blank\" rel=\"noopener\">Seaborn : all about the Data Visualization tool in Python<\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;PyWin32: Unveiling the Python Extension Exclusively for Windows Systems&quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/pywin32-unveiling-the-python-extension-exclusively-for-windows-systems\"><a href=\"https:\/\/liora.io\/en\/pywin32-unveiling-the-python-extension-exclusively-for-windows-systems\" target=\"_blank\" rel=\"noopener\">PyWin32: Unveiling the Python Extension Exclusively for Windows Systems<\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;SymPy: everything you need to know about the Python symbolic computation library &quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/sympy-everything-you-need-to-know-about-the-python-symbolic-computation-library\"><a href=\"https:\/\/liora.io\/en\/sympy-everything-you-need-to-know-about-the-python-symbolic-computation-library\" target=\"_blank\" rel=\"noopener\">SymPy: everything you need to know about the Python symbolic computation library <\/a><\/td><\/tr><\/tbody><\/table><p>&nbsp;<\/p><p>&nbsp;<\/p>\t\t\n\t\t\t<h3>Linear regression &#8211; mathematical translations<\/h3>\t\t\n\t\t<p>The mathematical equation for linear regression is as follows:<\/p><pre style=\"padding-left: 40px\"><strong><i>Y = \u0398<sub>0<\/sub> + \u0398<sub>1<\/sub>x<sub>1<\/sub> + ... \u0398<sub>n<\/sub>x<sub>n<\/sub><\/i><\/strong><\/pre><p>In this equation :<\/p><ul><li>Y corresponds to the explanatory value ;<\/li><li>\u03b8 corresponds to the bias term or parameter vector;<\/li><li>x1, x2&#8230;, xn correspond to the entity values.<\/li><\/ul><p>From a visual point of view,<strong> linear regression<\/strong> applies when the training data represents a point cloud. In this case, the aim is to identify the straight line that most closely <strong>approximates the set of points.<\/strong><\/p><p>To ensure that this line is as accurate as possible, we measure the mean squared error.<\/p>\t\t\n\t\t\t\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/en\/courses\/data-ai\/data-scientist\">All you need to know about linear regression with Python<\/a><\/div><\/div>\n\n\t\t\t<h2>Use cases for linear regression<\/h2><figure class=\"wp-block-image size-large\" style=\"margin-top:var(--wp--preset--spacing--columns);margin-bottom:var(--wp--preset--spacing--columns)\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"572\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/screen-data-analysis-graphic-1024x572.jpg\" alt=\"A computer screen displaying code and a data analysis chart.\" class=\"wp-image-207555\" srcset=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/screen-data-analysis-graphic-56x56.jpg 56w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/screen-data-analysis-graphic-115x64.jpg 115w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/screen-data-analysis-graphic-150x150.jpg 150w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/screen-data-analysis-graphic-210x117.jpg 210w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/screen-data-analysis-graphic-300x167.jpg 300w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/screen-data-analysis-graphic-410x270.jpg 410w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/screen-data-analysis-graphic-440x246.jpg 440w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/screen-data-analysis-graphic-448x448.jpg 448w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/screen-data-analysis-graphic-587x510.jpg 587w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/screen-data-analysis-graphic-768x429.jpg 768w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/screen-data-analysis-graphic-785x438.jpg 785w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/screen-data-analysis-graphic-1024x572.jpg 1024w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/screen-data-analysis-graphic-1250x590.jpg 1250w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/screen-data-analysis-graphic-1440x680.jpg 1440w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/screen-data-analysis-graphic-1536x857.jpg 1536w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/screen-data-analysis-graphic-2048x1143.jpg 2048w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/screen-data-analysis-graphic.jpg 2560w\" sizes=\"(max-width: 1024px) 100vw, 1024px\"><\/figure>\t\t\n\t\t<p>If <strong>linear regression<\/strong> is the first<a href=\"https:\/\/liora.io\/en\/algorithm-what-is-it\"> algorithm<\/a> used in machine learning, it&#8217;s because it can be applied in so many different ways.<\/p><p><strong>For example:<\/strong><\/p><ul><li>Identify factors influencing the profitability of an investment;<\/li><li>Predict future sales by analyzing past sales;<\/li><li>Anticipate consumer behavior;<\/li><li>Predict the price of a house based on its characteristics;<\/li><li>etc.<\/li><\/ul><p>And for every linear regression application, you can use Python.<\/p>\t\t\n\t\t\t<h2>How to use linear regression with Python?<\/h2>\t\t\n\t\t<p>To explain<strong> linear regression with Python,<\/strong> let&#8217;s take a concrete example. Here&#8217;s the starting hypothesis:<\/p><ul><li><strong>A restaurateur<\/strong> who already owns several restaurants in several cities wants to expand his business by setting up in different locations.<\/li><li><strong>To analyze the next cities in which to set up, the restaurateur has two sets of data at his disposal<\/strong>: the profits made in the cities where he is already established, and the populations of the cities.<\/li><\/ul><p>Since the aim is to make as much profit as possible in the town where he&#8217;s going to set up, he needs to predict the profit made in the town where he&#8217;s going to set up (dependent variable = Y) as a function of its population (independent variable = X).<\/p><p>So how do you evaluate the <strong>linear regression model<\/strong> with Python? Here&#8217;s how.<\/p>\t\t\n\t\t\t<h3>Formatting data<\/h3>\t\t\n\t\t<p>To<strong> model linear regression with Python<\/strong>, you need to prepare the training data in the right format. Ideally, you should prepare a CSV file with two columns: one for the population (independent variable) and another for the benefits (independent variable). Here&#8217;s what such a file might look like:<\/p>\t\t\n\t\t\t\n.tg  {border-collapse:collapse;border-spacing:0;}\n.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n  overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg .tg-649r{background-color:#efefef;font-family:Tahoma, Geneva, sans-serif !important;font-size:16px;text-align:center;\n  vertical-align:top}\n.tg .tg-7vx0{font-family:Tahoma, Geneva, sans-serif !important;font-size:16px;font-weight:bold;text-align:center;vertical-align:top}\n\n<table style=\"undefined;width: 200px\">\n<colgroup>\n<col style=\"width: 100px\">\n<col style=\"width: 100px\">\n<\/colgroup>\n<thead>\n  <tr>\n    <th>Population<\/th>\n    <th>Profits <\/th>\n  <\/tr>\n<\/thead>\n<tbody>\n  <tr>\n    <td>811 000<\/td>\n    <td>175 000 \u20ac<\/td>\n  <\/tr>\n  <tr>\n    <td>757 000<\/td>\n    <td>91 300 \u20ac<\/td>\n  <\/tr>\n  <tr>\n    <td>551 000<\/td>\n    <td>21 000 \u20ac<\/td>\n  <\/tr>\n  <tr>\n    <td>372 000<\/td>\n    <td>&#8211; 6 000 \u20ac<\/td>\n  <\/tr>\n  <tr>\n    <td>\u2026<\/td>\n    <td>\u2026<\/td>\n  <\/tr>\n<\/tbody>\n<\/table>\t\t\n\t\t\t\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/en\/courses\/data-ai\/data-scientist\">Linear regression with Python<\/a><\/div><\/div>\n\n\t\t\t<h3>Load data<\/h3>\t\t\n\t\t<p>This training data must then be loaded into Python. Thanks to the Pandas library, you can easily read CSV files. Here&#8217;s how to do it.<\/p><pre>import pandas as pd&nbsp;\ndf=pd.read_csv(\"D:DEVPYTHON_PROGRAMMINGdonnees-d-entrainement-regression-lineaire.csv\")<\/pre><p>The read_csv() function returns a two-dimensional array containing the dependent and independent variables. But to use linear regression with Python, you need to separate the two columns into two Python variables.<\/p><p>For the first column corresponding to population size :<\/p><pre>X = df.iloc[0:len(df),0]<\/pre><p>For the second column corresponding to profits :<\/p><pre>Y = df.iloc[0:len(df),1]<\/pre><p>This gives you a simple table containing the entire training data set.<\/p>\t\t\n\t\t\t<h3>Visualize data<\/h3>\t\t\n\t\t<p>To better understand linear regression with <a href=\"https:\/\/liora.io\/en\/python-the-most-popular-language\">Python<\/a>, it can be useful to visualize it. This will enable you to identify points and better understand dispersions.<\/p><p>To obtain a scatter plot, you can use <a href=\"https:\/\/liora.io\/en\/matplotlib-master-data-visualization-in-python\">Matplotlib,<\/a> a Python library. Here&#8217;s how to get it:<\/p><pre>import matplotlib.pyplot as plt\naxes = plt.axes()\naxes.grid()\nplt.scatter(X,Y) \nplt.show()<\/pre>\t\t\n\t\t\t<h3>Apply the algorithm<\/h3>\t\t\n\t\t<p>From there, the aim is to find a predictive function F(X) with population size as input and expected profits as output.<\/p><p>To model linear regression with Python, the easiest way is to use the Scikit Learn library by typing this query:<\/p><pre>from sklearn.linear_model import LinearRegression.<\/pre><p>From there, you can build your template. Here&#8217;s the code to write:<\/p><pre>reg = LinearRegression(normalize=True)\nreg.fit(x,y)<\/pre><p>And to find the line f(x)=ax+b with minimum squared error, type :<\/p><pre>a = reg.coef_\nb = reg.intercept.<\/pre>\t\t\n\t\t\t<h4>Make predictions<\/h4>\t\t\n\t\t<p>To plot the linear regression curve with Python, simply type the code below:<\/p><pre>ordonne = np.linspace\nplt.scatter(x,y)\nplt.plot(ordonne,a*ordonne+b,color='r')<\/pre>\t\t\n\t\t\t<h2>Master linear regression with Python<\/h2><figure class=\"wp-block-image size-large\" style=\"margin-top:var(--wp--preset--spacing--columns);margin-bottom:var(--wp--preset--spacing--columns)\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"572\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/computer-programming-data-analysis-1024x572.jpg\" alt=\"A computer screen displaying code and a data analysis chart.\" class=\"wp-image-207556\" srcset=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/computer-programming-data-analysis-56x56.jpg 56w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/computer-programming-data-analysis-115x64.jpg 115w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/computer-programming-data-analysis-150x150.jpg 150w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/computer-programming-data-analysis-210x117.jpg 210w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/computer-programming-data-analysis-300x167.jpg 300w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/computer-programming-data-analysis-410x270.jpg 410w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/computer-programming-data-analysis-440x246.jpg 440w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/computer-programming-data-analysis-448x448.jpg 448w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/computer-programming-data-analysis-587x510.jpg 587w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/computer-programming-data-analysis-768x429.jpg 768w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/computer-programming-data-analysis-785x438.jpg 785w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/computer-programming-data-analysis-1024x572.jpg 1024w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/computer-programming-data-analysis-1250x590.jpg 1250w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/computer-programming-data-analysis-1440x680.jpg 1440w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/computer-programming-data-analysis-1536x857.jpg 1536w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/computer-programming-data-analysis-2048x1143.jpg 2048w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/computer-programming-data-analysis.jpg 2560w\" sizes=\"(max-width: 1024px) 100vw, 1024px\"><\/figure>\t\t\n\t\t<p><strong>Linear regression<\/strong> is undoubtedly the algorithm to master in data science. And if using it <strong>via Python still seems complex, that&#8217;s only temporary.<\/strong><\/p><p>With the right training, you&#8217;ll be able to evaluate any machine learning algorithm across different programming languages.<\/p><p>But which course should you choose? Liora, of course. Discover our program.<\/p>\t\t\n\t\t\t\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/en\/courses\/data-ai\/data-scientist\">Learn all about linear regression in Python<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A key algorithm in Machine Learning, linear regression is used to establish relationships between one or more variables. To put this algorithm into practice with ease, data scientists can turn to programming languages, particularly Python. So how do you use linear regression with Python? Liora answers the question. What is linear regression? Before looking at [&hellip;]<\/p>\n","protected":false},"author":76,"featured_media":207711,"comment_status":"open","ping_status":"open","sticky":false,"template":"elementor_theme","format":"standard","meta":{"_acf_changed":false,"editor_notices":[],"footnotes":""},"categories":[2433],"class_list":["post-182969","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-ai"],"acf":[],"_links":{"self":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/182969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/users\/76"}],"replies":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/comments?post=182969"}],"version-history":[{"count":3,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/182969\/revisions"}],"predecessor-version":[{"id":207712,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/182969\/revisions\/207712"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media\/207711"}],"wp:attachment":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media?parent=182969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/categories?post=182969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}