Nov 16, 2026 | 622 words | 6 min read
12.1.2. PCA: Linear Regression Analysis#
Required Files#
Download the template
ENGR131_Python_Template.py and
save as 12_1_2_username.py.
Reminder
Review and reference the Python Style Guide to create a professionally formatted code file. For example, remember to provide detailed comments throughout your code so anyone can read them and understand what your program does!
Instructions#
Write a Python program that:
Creates a scatter plot of the provided data
Fits a linear regression model to the data
Plots the linear regression model with the original data
Reports the model equation and other regression statistics
Part 1#
Import the required libraries:
import numpy as np
import matplotlib.pyplot
Part 2#
Create NumPy arrays with the following values:
x: 1, 2, 3, 4, 5
y: 2.1, 3.9, 6.2, 7.8, 10.1
Part 3#
Create a scatter plot with the x and y NumPy arrays. The data markers should be
black circles. The plot should display a light grey grid.
The plot title is “Sample Data” while the x- and y-axes should be labeled “X” and “Y”, respectively.
Do not submit an image of your plot to Gradescope. The autograder will examine your python code and grade the plot.
Refer to the Data Analysis and Visualization pre-class materials for for creating professional and comprehensive plots.
Part 4#
Use np.polyfit() to fit a linear regression model to the data.
Part 5#
Display the slope, intercept, and model equation (see the sample output below for formatting preferences).
Part 6#
Create another NumPy array called y_predict that holds predicted y-values for the
regression model.
Part 7#
Calculate the sum of squared errors (SSE) and sum of squares total (SST) using the
predicted y-values, and compute the coefficient of determination (\(r^2\)) as
r_squared. Then display the r_squared value (see sample output below).
See the Linear Regression materials from our Excel unit to refresh your memory on Least Squares Regression.
Part 8#
Plot the regression line on the existing scatter plot. The model should be plotted as a solid red line, and the plot title should read “Linear Regression Fit.”
The plot legend should display the black data markers as “Sample Data” and the regression model (red line) as “Regression: [model equation], [r_squared value].” The slope, intercept, and \(r^2\) values should be formatted to three decimal places.
Do not submit an image of your plot to Gradescope. The autograder will examine your python code and grade the plot.
Sample Output#
Sample Output
Gradescope Submission#
Attention
Before submitting your python file, comment all lines with plt.show() for autograding purposes.
Save Deliverables: Ensure all required assignment files are saved and named exactly as specified in the assignment instructions. If a template file name contains the word
template, replace only that word with your Purdue username. When a task lists more than one deliverable, such as a workbook and a report, submit each deliverable to its own matching Gradescope assignment instead of combining them into one upload.Navigate to Gradescope: Log in to Gradescope. From your Dashboard, open the assignment.
Upload your work: Select the Upload submission method. Drag and drop all required files (or a compressed
.zipfolder) into Gradescope, and select Upload.Confirm Submission: When your upload is successful, you will see a confirmation message on your screen and receive an automated email receipt.
Add Team Members (if applicable): If this is a team assignment, add all team members to the submission. Each teammate should confirm they can see the submission in their own Gradescope account. For help, visit Gradescope’s documentation on adding team members.
Review Autograder Results: Wait a few minutes for the autograder to evaluate the relevant files. Scroll down to view any failed test cases. If you need assistance interpreting the output or errors, ask your teaching team.
Resubmit if Necessary: You can edit your files and select Resubmit as many times as you need before the assignment deadline. Only your final, active submission is graded. (Note: If you need to resubmit, you must upload all required files for the assignment again).
For a detailed walkthrough, review the official Gradescope Submission Guide.