René's URL Explorer Experiment


Title: Simple Linear Regression: Code (Video) – Real Python

Open Graph Title: Simple Linear Regression: Code – Real Python

Description: All right. If you already haven’t, fire up an instance of a Jupyter notebook, an editor, or any other terminal that you’re comfortable with to write your Python code. First thing, let’s import the two modules that we’re going to need. We’re going to…

Open Graph Description: All right. If you already haven’t, fire up an instance of a Jupyter notebook, an editor, or any other terminal that you’re comfortable with to write your Python code. First thing, let’s import the two modules that we’re going to need. We’re going to…

Opengraph URL: https://realpython.com/lessons/simple-linear-regression-code/

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
  {
    "@context": "https://schema.org",
    "@type": "VideoObject",
    "name": "Simple Linear Regression: Code",
    "description": "All right. If you already haven’t, fire up an instance of a Jupyter notebook, an editor, or any other terminal that you’re comfortable with to write your Python code. First thing, let’s import the two modules that we’re going to need. We’re going to…",
    "thumbnailUrl": ["https://files.realpython.com/media/Linear-Regression-in-Python_Watermarked.479f82188ace.jpg"],
    "uploadDate": "2022-02-22T14:00:00+00:00",
    "duration": "PT6M57S",
    
    "potentialAction": {
      "@type": "SeekToAction",
      "target": "https://realpython.com/lessons/simple-linear-regression-code/#t={seek_to_second_number}",
      "startOffset-input": "required name=seek_to_second_number"
    }
  }
  

authorReal Python
twitter:cardsummary_large_image
twitter:imagehttps://files.realpython.com/media/Linear-Regression-in-Python_Watermarked.479f82188ace.jpg
og:imagehttps://files.realpython.com/media/Linear-Regression-in-Python_Watermarked.479f82188ace.jpg
twitter:creator@realpython
og:typevideo.episode

Links:

https://realpython.com/
Start Herehttps://realpython.com/start-here/
Learn Python https://realpython.com/lessons/simple-linear-regression-code/
Python Tutorials →In-depth articles and video courseshttps://realpython.com/search?kind=article&kind=course&order=newest
Learning Paths →Guided study plans for accelerated learninghttps://realpython.com/learning-paths/
Quizzes & Exercises →Check your learning progresshttps://realpython.com/quizzes/
Browse Topics →Focus on a specific area or skill levelhttps://realpython.com/tutorials/all/
Community Chat →Learn with other Pythonistashttps://realpython.com/community/
Office Hours →Live Q&A calls with Python expertshttps://realpython.com/office-hours/
Podcast →Hear what’s new in the world of Pythonhttps://realpython.com/podcasts/rpp/
Books →Round out your knowledge and learn offlinehttps://realpython.com/products/books/
Reference →Concise definitions for common Python termshttps://realpython.com/ref/
Code Mentor →BetaPersonalized code assistance & learning toolshttps://realpython.com/mentor/
Unlock All Content →https://realpython.com/account/join/
More https://realpython.com/lessons/simple-linear-regression-code/
Learner Storieshttps://realpython.com/learner-stories/
Python Newsletterhttps://realpython.com/newsletter/
Python Job Boardhttps://www.pythonjobshq.com
Meet the Teamhttps://realpython.com/team/
Become a Tutorial Writerhttps://realpython.com/write-for-us/
Become a Video Instructorhttps://realpython.com/become-an-instructor/
Searchhttps://realpython.com/search
https://realpython.com/search
Joinhttps://realpython.com/account/join/
Sign‑Inhttps://realpython.com/account/login/?next=%2Flessons%2Fsimple-linear-regression-code%2F
Unlock This Lessonhttps://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-linear-regression
Unlock This Lessonhttps://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 Pythonhttps://realpython.com/courses/python-linear-regression/
Cesar Aguilarhttps://realpython.com/courses/python-linear-regression/#team
Recommended Tutorialhttps://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 Questionhttps://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
Transcripthttps://realpython.com/lessons/simple-linear-regression-code/#transcript
Discussion (1)https://realpython.com/lessons/simple-linear-regression-code/#discussion
00:00https://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:11https://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 modulehttps://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:39https://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:55https://realpython.com/lessons/simple-linear-regression-code/#t=55.74
And the linear regression object is going to be expecting for the input arrayhttps://realpython.com/lessons/simple-linear-regression-code/#t=55.74
a two-dimensional array. As we have it nowhttps://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:07https://realpython.com/lessons/simple-linear-regression-code/#t=67.47
So let’s make this input array a two-dimensional array containing six rowshttps://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:22https://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 dimensionhttps://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:35https://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 datahttps://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 arrayhttps://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:54https://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:06https://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:16https://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:36https://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 thehttps://realpython.com/lessons/simple-linear-regression-code/#t=161.56
LinearRegression class.https://realpython.com/lessons/simple-linear-regression-code/#t=165.76
02:50https://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:59https://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:15https://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 ofhttps://realpython.com/lessons/simple-linear-regression-code/#t=195.87
the input variables—in this case, there’s only one inputhttps://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:31https://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 modelhttps://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 inputhttps://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 datahttps://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:16https://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 predictionhttps://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:32https://realpython.com/lessons/simple-linear-regression-code/#t=272.65
Let’s use our model to predict what the responses are for thehttps://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:50https://realpython.com/lessons/simple-linear-regression-code/#t=290.69
Another way to get these values is to manually evaluate the inputs x onhttps://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:15https://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:36https://realpython.com/lessons/simple-linear-regression-code/#t=336.06
The real power of prediction using a regression model is to evaluate the modelhttps://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:02https://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 newhttps://realpython.com/lessons/simple-linear-regression-code/#t=377.57
inputs.https://realpython.com/lessons/simple-linear-regression-code/#t=381.14
06:33https://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 tohttps://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, 2022https://realpython.com/lessons/simple-linear-regression-code/#comment-04c5b29f-e889-4c67-a0b3-85d556f95219
Become a Memberhttps://realpython.com/account/join/
https://realpython.com/videos/simple-linear-regression-background/
Overviewhttps://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 Policyhttps://realpython.com/privacy-policy/

Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover

Robots: max-image-preview:large


URLs of crawlers that visited me.