Exercise: Visualizing Regression Plots
Practice with some lab exercises.
We'll cover the following
Exercise 1
Create a scatter plot with the Sales
variable plotted against TV
. Include a red trendline of the line of best fit using Ordinary Least Squares (OLS). What is the value?
Solution
-
A scatter plot is created using the
px.scatter()
function on line 2 with theadvertising
DataFrame. Thex
parameter is set toTV
to specify the x-axis data, and they
parameter is set toSales
to specify the y-axis data. Thetrendline
parameter is set tools
to add a linear regression line to the plot. Thetrendline_color_override
parameter is set tored
to override the default color of the trendline with red color. -
The
update_layout()
function on line 7 is used to set the title of the figure to “Sales vs. TV Advertising (with OLS line estimate).”
Note: From the graph, we see that the is 0.81.
Get hands-on with 1400+ tech skills courses.