Challenge - 5 Problems
Trend Line Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of scatter plot with linear trend line
What will be the output of this code snippet that plots a scatter plot and adds a linear trend line?
Matplotlib
import matplotlib.pyplot as plt import numpy as np x = np.array([1, 2, 3, 4, 5]) y = np.array([2, 4, 5, 4, 5]) plt.scatter(x, y) coeffs = np.polyfit(x, y, 1) trend = np.poly1d(coeffs) plt.plot(x, trend(x), color='red') plt.show()
Attempts:
2 left
💡 Hint
Look at how np.polyfit with degree 1 creates a straight line fit.
✗ Incorrect
The code fits a linear (degree 1) polynomial to the points and plots it as a red line. The line approximates the trend of the scatter points.
❓ data_output
intermediate1:30remaining
Number of points above the trend line
Given the following data and trend line, how many points lie above the linear trend line?
Matplotlib
import numpy as np x = np.array([1, 2, 3, 4, 5]) y = np.array([2, 4, 5, 4, 5]) coeffs = np.polyfit(x, y, 1) trend = np.poly1d(coeffs) points_above = sum(y > trend(x)) print(points_above)
Attempts:
2 left
💡 Hint
Compare each y value to the trend line value at the same x.
✗ Incorrect
The trend line values are approximately [2.8, 3.4, 4.0, 4.6, 5.2]. Points with y > trend(x) are at x=2 (4>3.4), x=3 (5>4.0), so 2 points are above.
❓ visualization
advanced2:30remaining
Identify the correct polynomial trend line plot
Which option shows the correct plot of a scatter plot with a quadratic (degree 2) trend line for the data below?
Matplotlib
import matplotlib.pyplot as plt import numpy as np x = np.array([1, 2, 3, 4, 5]) y = np.array([1, 4, 9, 16, 25]) plt.scatter(x, y) coeffs = np.polyfit(x, y, 2) trend = np.poly1d(coeffs) plt.plot(x, trend(x), color='green') plt.show()
Attempts:
2 left
💡 Hint
Degree 2 polynomial fits a parabola shape.
✗ Incorrect
The data points are perfect squares, so a quadratic trend line fits exactly and passes through all points.
🔧 Debug
advanced2:00remaining
Identify the error in trend line code
What error will this code raise when trying to plot a trend line on a scatter plot?
Matplotlib
import matplotlib.pyplot as plt import numpy as np x = np.array([1, 2, 3, 4, 5]) y = np.array([2, 3, 5, 7, 11]) plt.scatter(x, y) coeffs = np.polyfit(x, y, 1) trend = np.poly1d(coeffs) plt.plot(trend(x), x, color='blue') plt.show()
Attempts:
2 left
💡 Hint
Check the order of arguments in plt.plot(x, y).
✗ Incorrect
plt.plot(trend(x), x) swaps x and y axes but does not raise an error. The plot will show a line but axes are reversed.
🚀 Application
expert3:00remaining
Calculate and plot residuals from trend line
Given data points and a linear trend line, which code correctly calculates residuals and plots them as a bar chart?
Matplotlib
import matplotlib.pyplot as plt import numpy as np x = np.array([1, 2, 3, 4, 5]) y = np.array([2, 3, 5, 7, 11]) coeffs = np.polyfit(x, y, 1) trend = np.poly1d(coeffs) residuals = y - trend(x) plt.bar(x, residuals) plt.xlabel('x') plt.ylabel('Residuals') plt.title('Residuals from Linear Trend Line') plt.show()
Attempts:
2 left
💡 Hint
Residuals are actual minus predicted values and bar plot shows magnitude.
✗ Incorrect
Residuals are differences y - predicted y. The code correctly computes and plots them as bars.