|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/simple-linear-regression-code/ |
| Python Tutorials →In-depth articles and video courses | https://realpython.com/search?kind=article&kind=course&order=newest |
| Learning Paths →Guided study plans for accelerated learning | https://realpython.com/learning-paths/ |
| Quizzes & Exercises →Check your learning progress | https://realpython.com/quizzes/ |
| Browse Topics →Focus on a specific area or skill level | https://realpython.com/tutorials/all/ |
| Community Chat →Learn with other Pythonistas | https://realpython.com/community/ |
| Office Hours →Live Q&A calls with Python experts | https://realpython.com/office-hours/ |
| Podcast →Hear what’s new in the world of Python | https://realpython.com/podcasts/rpp/ |
| Books →Round out your knowledge and learn offline | https://realpython.com/products/books/ |
| Reference →Concise definitions for common Python terms | https://realpython.com/ref/ |
| Code Mentor →BetaPersonalized code assistance & learning tools | https://realpython.com/mentor/ |
| Unlock All Content → | https://realpython.com/account/join/ |
|
More
| https://realpython.com/lessons/simple-linear-regression-code/ |
| Learner Stories | https://realpython.com/learner-stories/ |
| Python Newsletter | https://realpython.com/newsletter/ |
| Python Job Board | https://www.pythonjobshq.com |
| Meet the Team | https://realpython.com/team/ |
| Become a Tutorial Writer | https://realpython.com/write-for-us/ |
| Become a Video Instructor | https://realpython.com/become-an-instructor/ |
| Search | https://realpython.com/search |
| https://realpython.com/search |
| Join | https://realpython.com/account/join/ |
| Sign‑In | https://realpython.com/account/login/?next=%2Flessons%2Fsimple-linear-regression-code%2F |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-linear-regression |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-linear-regression |
| https://realpython.com/courses/python-linear-regression/#team |
| Starting With Linear Regression in Python | https://realpython.com/courses/python-linear-regression/ |
| Cesar Aguilar | https://realpython.com/courses/python-linear-regression/#team |
| Recommended Tutorial | https://realpython.com/linear-regression-in-python/ |
| Course Slides (.pdf) | https://realpython.com/courses/python-linear-regression/downloads/python-linear-regression-slides/ |
| Sample Code (.zip) | https://realpython.com/courses/python-linear-regression/downloads/python-linear-regression-sample-code/ |
| Ask a Question | https://realpython.com/lessons/simple-linear-regression-code/#discussion |
| https://realpython.com/feedback/survey/course/python-linear-regression/liked/?from=lesson-title |
| https://realpython.com/feedback/survey/course/python-linear-regression/disliked/?from=lesson-title |
| Transcript | https://realpython.com/lessons/simple-linear-regression-code/#transcript |
| Discussion (1) | https://realpython.com/lessons/simple-linear-regression-code/#discussion |
| 00:00 | https://realpython.com/lessons/simple-linear-regression-code/#t=0.51 |
| All right. If you already haven’t, fire up an instance of a Jupyter notebook, | https://realpython.com/lessons/simple-linear-regression-code/#t=0.51 |
| an editor, | https://realpython.com/lessons/simple-linear-regression-code/#t=4.83 |
| or any other terminal that you’re comfortable with to write your Python code. | https://realpython.com/lessons/simple-linear-regression-code/#t=5.7 |
| 00:11 | https://realpython.com/lessons/simple-linear-regression-code/#t=11.46 |
| First thing, let’s import the two modules that we’re going to need. | https://realpython.com/lessons/simple-linear-regression-code/#t=11.46 |
| We’re going to need NumPy, | https://realpython.com/lessons/simple-linear-regression-code/#t=14.79 |
| and we’re going to need a class from the sklearn module | https://realpython.com/lessons/simple-linear-regression-code/#t=20.76 |
| that’s going to implement linear regression. | https://realpython.com/lessons/simple-linear-regression-code/#t=24.24 |
| 00:39 | https://realpython.com/lessons/simple-linear-regression-code/#t=39.35 |
| Let’s create some dummy data to try out the LinearRegression class, | https://realpython.com/lessons/simple-linear-regression-code/#t=39.35 |
| the input array. | https://realpython.com/lessons/simple-linear-regression-code/#t=44.45 |
| We’re going to be using an array containing six data points. | https://realpython.com/lessons/simple-linear-regression-code/#t=47.63 |
| 00:55 | https://realpython.com/lessons/simple-linear-regression-code/#t=55.74 |
| And the linear regression object is going to be expecting for the input array | https://realpython.com/lessons/simple-linear-regression-code/#t=55.74 |
| a two-dimensional array. As we have it now | https://realpython.com/lessons/simple-linear-regression-code/#t=59.94 |
| this is a one-dimensional array containing six data points. | https://realpython.com/lessons/simple-linear-regression-code/#t=63.78 |
| 01:07 | https://realpython.com/lessons/simple-linear-regression-code/#t=67.47 |
| So let’s make this input array a two-dimensional array containing six rows | https://realpython.com/lessons/simple-linear-regression-code/#t=67.47 |
| in one column. To do that, we use the reshape() function. | https://realpython.com/lessons/simple-linear-regression-code/#t=72.45 |
| We pass it in a tuple: number of rows and the number of columns. | https://realpython.com/lessons/simple-linear-regression-code/#t=76.5 |
| 01:22 | https://realpython.com/lessons/simple-linear-regression-code/#t=82.17 |
| There’s a shortcut that we can use in reshape(). | https://realpython.com/lessons/simple-linear-regression-code/#t=82.17 |
| When we want reshape() to infer the number or the dimension | https://realpython.com/lessons/simple-linear-regression-code/#t=85.11 |
| size for one of the dimensions, we can pass in a -1 value. | https://realpython.com/lessons/simple-linear-regression-code/#t=90.03 |
| 01:35 | https://realpython.com/lessons/simple-linear-regression-code/#t=95.82 |
| And so what reshape() is going to do is that because this array contains six data | https://realpython.com/lessons/simple-linear-regression-code/#t=95.82 |
| points, | https://realpython.com/lessons/simple-linear-regression-code/#t=100.65 |
| and we’re asking that reshape() return us a two-dimensional array | https://realpython.com/lessons/simple-linear-regression-code/#t=101.85 |
| containing one column, because there are six data points, | https://realpython.com/lessons/simple-linear-regression-code/#t=106.44 |
| the number of rows is going to be computed automatically as six. | https://realpython.com/lessons/simple-linear-regression-code/#t=109.92 |
| 01:54 | https://realpython.com/lessons/simple-linear-regression-code/#t=114.57 |
| Now let’s create the output array. | https://realpython.com/lessons/simple-linear-regression-code/#t=114.57 |
| 02:06 | https://realpython.com/lessons/simple-linear-regression-code/#t=126.38 |
| So let’s verify that x has a shape of six by one. | https://realpython.com/lessons/simple-linear-regression-code/#t=126.38 |
| 02:16 | https://realpython.com/lessons/simple-linear-regression-code/#t=136.1 |
| And the shape of y … | https://realpython.com/lessons/simple-linear-regression-code/#t=136.1 |
| so here y is a one-dimensional NumPy array containing six data points. | https://realpython.com/lessons/simple-linear-regression-code/#t=141.32 |
| Now let’s build our regression model. | https://realpython.com/lessons/simple-linear-regression-code/#t=147.94 |
| 02:36 | https://realpython.com/lessons/simple-linear-regression-code/#t=156.97 |
| And then to actually compute the model—in other words, in this case, | https://realpython.com/lessons/simple-linear-regression-code/#t=156.97 |
| compute the coefficients— | https://realpython.com/lessons/simple-linear-regression-code/#t=159.97 |
| we need to use the .fit() method on the model object that we created using the | https://realpython.com/lessons/simple-linear-regression-code/#t=161.56 |
| LinearRegression class. | https://realpython.com/lessons/simple-linear-regression-code/#t=165.76 |
| 02:50 | https://realpython.com/lessons/simple-linear-regression-code/#t=170.77 |
| .fit() takes two required positional arguments, | https://realpython.com/lessons/simple-linear-regression-code/#t=170.77 |
| the first one, x, being the input variable, and y being the response. | https://realpython.com/lessons/simple-linear-regression-code/#t=173.53 |
| 02:59 | https://realpython.com/lessons/simple-linear-regression-code/#t=179.81 |
| Now that we’ve called the .fit() method on the model object, | https://realpython.com/lessons/simple-linear-regression-code/#t=179.81 |
| the model object contains attributes that contain all of the coefficients. | https://realpython.com/lessons/simple-linear-regression-code/#t=182.69 |
| The 𝑏₀ coefficient is the intercept_ attribute, | https://realpython.com/lessons/simple-linear-regression-code/#t=187.97 |
| 03:15 | https://realpython.com/lessons/simple-linear-regression-code/#t=195.87 |
| and the other coefficients in the model, that are the ones that are in front of | https://realpython.com/lessons/simple-linear-regression-code/#t=195.87 |
| the input variables—in this case, there’s only one input | https://realpython.com/lessons/simple-linear-regression-code/#t=199.26 |
| variable—is in the coefficient attribute, or coef_. | https://realpython.com/lessons/simple-linear-regression-code/#t=202.26 |
| 03:31 | https://realpython.com/lessons/simple-linear-regression-code/#t=211.59 |
| This attribute is a NumPy array in this case, | https://realpython.com/lessons/simple-linear-regression-code/#t=211.59 |
| which has only one data point or one value, which is the 𝑏₁ value. | https://realpython.com/lessons/simple-linear-regression-code/#t=213.93 |
| In regression, | https://realpython.com/lessons/simple-linear-regression-code/#t=220.51 |
| there’s a value that can be used to determine how good a linear model | https://realpython.com/lessons/simple-linear-regression-code/#t=221.2 |
| fits the data, | https://realpython.com/lessons/simple-linear-regression-code/#t=226.09 |
| and this is the 𝑅² value. To get the 𝑅² value, | https://realpython.com/lessons/simple-linear-regression-code/#t=227.35 |
| you use the .score() method on the model, and you input | https://realpython.com/lessons/simple-linear-regression-code/#t=231.88 |
| whatever x and y values you want. In this case, | https://realpython.com/lessons/simple-linear-regression-code/#t=236.56 |
| let’s use the actual observations to see what the 𝑅² value is on the data | https://realpython.com/lessons/simple-linear-regression-code/#t=240.13 |
| that we have. | https://realpython.com/lessons/simple-linear-regression-code/#t=244.99 |
| 04:16 | https://realpython.com/lessons/simple-linear-regression-code/#t=256.84 |
| An 𝑅² value that is close to one, | https://realpython.com/lessons/simple-linear-regression-code/#t=256.84 |
| or exactly equal to one, means that a linear model is a good fit for the data. | https://realpython.com/lessons/simple-linear-regression-code/#t=259.45 |
| We talked about how regression can be used for two main purposes: for prediction | https://realpython.com/lessons/simple-linear-regression-code/#t=266.14 |
| and for inference. | https://realpython.com/lessons/simple-linear-regression-code/#t=271.03 |
| 04:32 | https://realpython.com/lessons/simple-linear-regression-code/#t=272.65 |
| Let’s use our model to predict what the responses are for the | https://realpython.com/lessons/simple-linear-regression-code/#t=272.65 |
| observed inputs for x. | https://realpython.com/lessons/simple-linear-regression-code/#t=277.36 |
| 04:50 | https://realpython.com/lessons/simple-linear-regression-code/#t=290.69 |
| Another way to get these values is to manually evaluate the inputs x on | https://realpython.com/lessons/simple-linear-regression-code/#t=290.69 |
| our model. So if you recall, the model is 𝑏₀ | https://realpython.com/lessons/simple-linear-regression-code/#t=295.67 |
| plus 𝑏₁, | https://realpython.com/lessons/simple-linear-regression-code/#t=305.06 |
| evaluated at the inputs x. | https://realpython.com/lessons/simple-linear-regression-code/#t=309.77 |
| 05:15 | https://realpython.com/lessons/simple-linear-regression-code/#t=315.06 |
| Oh, forgot my t there. | https://realpython.com/lessons/simple-linear-regression-code/#t=315.06 |
| All right. So really a way to think about this abstractly is that this is f(x) | https://realpython.com/lessons/simple-linear-regression-code/#t=320.64 |
| evaluated at each of the individual x values, and in this case, | https://realpython.com/lessons/simple-linear-regression-code/#t=325.56 |
| because x is a NumPy array containing six data points, | https://realpython.com/lessons/simple-linear-regression-code/#t=329.55 |
| we’re going to get six responses. | https://realpython.com/lessons/simple-linear-regression-code/#t=332.82 |
| 05:36 | https://realpython.com/lessons/simple-linear-regression-code/#t=336.06 |
| The real power of prediction using a regression model is to evaluate the model | https://realpython.com/lessons/simple-linear-regression-code/#t=336.06 |
| at inputs x to determine the corresponding response. | https://realpython.com/lessons/simple-linear-regression-code/#t=340.77 |
| So let’s create a new input array, | https://realpython.com/lessons/simple-linear-regression-code/#t=345.48 |
| and we’ll use the arange() function in NumPy, | https://realpython.com/lessons/simple-linear-regression-code/#t=351.99 |
| which is similar to the range() function, which creates an array—in this case, | https://realpython.com/lessons/simple-linear-regression-code/#t=354.66 |
| a NumPy array from zero to five. | https://realpython.com/lessons/simple-linear-regression-code/#t=358.85 |
| 06:02 | https://realpython.com/lessons/simple-linear-regression-code/#t=362.48 |
| And we’re going to need this to be a two-dimensional array. | https://realpython.com/lessons/simple-linear-regression-code/#t=362.48 |
| And let’s run that actually now, so that we print the output. | https://realpython.com/lessons/simple-linear-regression-code/#t=368.99 |
| And now let’s use the model to predict what the responses are for those new | https://realpython.com/lessons/simple-linear-regression-code/#t=377.57 |
| inputs. | https://realpython.com/lessons/simple-linear-regression-code/#t=381.14 |
| 06:33 | https://realpython.com/lessons/simple-linear-regression-code/#t=393.58 |
| And there you go. | https://realpython.com/lessons/simple-linear-regression-code/#t=393.58 |
| You just created your first linear regression model and used the model to | https://realpython.com/lessons/simple-linear-regression-code/#t=394.42 |
| predict responses for desired inputs. | https://realpython.com/lessons/simple-linear-regression-code/#t=399.04 |
| Now that you know how to implement simple linear regression using scikit-learn, | https://realpython.com/lessons/simple-linear-regression-code/#t=404.11 |
| let’s now talk about multiple linear regression, | https://realpython.com/lessons/simple-linear-regression-code/#t=408.49 |
| and then we’ll come back to Jupyter to implement that using scikit-learn. | https://realpython.com/lessons/simple-linear-regression-code/#t=411.28 |
| March 2, 2022 | https://realpython.com/lessons/simple-linear-regression-code/#comment-04c5b29f-e889-4c67-a0b3-85d556f95219 |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/videos/simple-linear-regression-background/ |
| Overview | https://realpython.com/courses/python-linear-regression/ |
| https://realpython.com/lessons/multiple-linear-regression-background/ |
|
Starting With Linear Regression in Python (Overview) 04:19
| https://realpython.com/videos/python-linear-regression-overview/ |
|
Simple Linear Regression: Background 05:46
| https://realpython.com/videos/simple-linear-regression-background/ |
|
Simple Linear Regression: Code 06:57
| https://realpython.com/lessons/simple-linear-regression-code/ |
|
Multiple Linear Regression: Background 05:01
| https://realpython.com/lessons/multiple-linear-regression-background/ |
|
Multiple Linear Regression: Code 04:59
| https://realpython.com/lessons/multiple-linear-regression-code/ |
|
Polynomial Regression: Background 07:47
| https://realpython.com/lessons/polynomial-regression-background/ |
|
Simple Polynomial Regression: Code 06:35
| https://realpython.com/lessons/simple-polynomial-regression-code/ |
|
Multiple Polynomial Regression: Code 04:36
| https://realpython.com/lessons/multiple-polynomial-regression-code/ |
|
Linear Regression in Python (Quiz) 06:30
| https://realpython.com/lessons/linear-regression-in-python-quiz/ |
|
Starting With Linear Regression in Python (Summary) 00:53
| https://realpython.com/lessons/python-linear-regression-summary/ |
| Privacy Policy | https://realpython.com/privacy-policy/ |
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover