Description
In the Polynomial Regression section of 2-Regression/3-Linear/README.md, the prediction curve appears smooth in the included graph image. However, if learners run the code plt.plot(X_test, pred) (as taught previously in the lesson) to replicate the polynomial regression prediction plot, they get a zigzagged, scribbled line instead of a smooth curve.
In isolation the curve looks like:
This happens because:
train_test_split() shuffles the data randomly before splitting into train/test sets
- The values in
X_test are therefore not in sequential order
- When
plt.plot() connects points in the order they appear in the array, it creates a zigzag pattern rather than a smooth curve
Expected Behavior
The plot should display a smooth polynomial curve that correctly visualizes the model's prediction across the data range.
Solution
Instead of plotting directly on unsorted test data, create a uniform range of input values using np.linspace:
X_range = np.linspace(X_test.min(), X_test.max(), 100).reshape(-1,1)
y_range = pipeline.predict(X_range)
plt.scatter(X_test, y_test)
plt.plot(X_range, y_range)
This creates 100 evenly-spaced points across the test data range, producing a smooth curve that properly represents the polynomial regression model.
Description
In the Polynomial Regression section of
2-Regression/3-Linear/README.md, the prediction curve appears smooth in the included graph image. However, if learners run the codeplt.plot(X_test, pred)(as taught previously in the lesson) to replicate the polynomial regression prediction plot, they get a zigzagged, scribbled line instead of a smooth curve.In isolation the curve looks like:
This happens because:
train_test_split()shuffles the data randomly before splitting into train/test setsX_testare therefore not in sequential orderplt.plot()connects points in the order they appear in the array, it creates a zigzag pattern rather than a smooth curveExpected Behavior
The plot should display a smooth polynomial curve that correctly visualizes the model's prediction across the data range.
Solution
Instead of plotting directly on unsorted test data, create a uniform range of input values using
np.linspace:This creates 100 evenly-spaced points across the test data range, producing a smooth curve that properly represents the polynomial regression model.