Challenge - 5 Problems
Linear Regression Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this linear regression coefficient calculation?
Given the following code that fits a simple linear regression model, what is the value of the coefficient for
x?Data Analysis Python
import numpy as np from sklearn.linear_model import LinearRegression x = np.array([[1], [2], [3], [4], [5]]) y = np.array([2, 4, 6, 8, 10]) model = LinearRegression().fit(x, y) coef = model.coef_[0] print(round(coef, 2))
Attempts:
2 left
💡 Hint
Think about the relationship between x and y in the data.
✗ Incorrect
The y values are exactly twice the x values, so the slope (coefficient) is 2.0.
❓ data_output
intermediate2:00remaining
What is the predicted value for x=6 using this linear regression model?
Using the model trained on the data below, what is the predicted y value when x=6?
Data Analysis Python
import numpy as np from sklearn.linear_model import LinearRegression x = np.array([[1], [2], [3], [4], [5]]) y = np.array([3, 5, 7, 9, 11]) model = LinearRegression().fit(x, y) pred = model.predict([[6]])[0] print(round(pred, 2))
Attempts:
2 left
💡 Hint
Look at the pattern in y values as x increases.
✗ Incorrect
The model fits y = 2 * x + 1, so for x=6, y = 2*6 + 1 = 13.
🔧 Debug
advanced2:00remaining
Why does this linear regression code raise an error?
Examine the code below. Why does it raise an error when fitting the model?
Data Analysis Python
import numpy as np from sklearn.linear_model import LinearRegression x = np.array([1, 2, 3, 4, 5]) y = np.array([2, 4, 6, 8, 10]) model = LinearRegression().fit(x, y)
Attempts:
2 left
💡 Hint
Check the shape of x expected by scikit-learn.
✗ Incorrect
scikit-learn expects x to be 2D (samples, features). Here x is 1D, so it raises ValueError.
🧠 Conceptual
advanced1:30remaining
Which statement best describes the intercept in linear regression?
In a simple linear regression model, what does the intercept represent?
Attempts:
2 left
💡 Hint
Think about the point where the line crosses the y-axis.
✗ Incorrect
The intercept is the value of y when x equals zero, the starting point of the line.
🚀 Application
expert2:30remaining
Which option correctly calculates the R-squared score for this model?
Given a fitted linear regression model and test data, which code snippet correctly computes the R-squared score?
Data Analysis Python
import numpy as np from sklearn.linear_model import LinearRegression from sklearn.metrics import r2_score x_train = np.array([[1], [2], [3], [4], [5]]) y_train = np.array([2, 4, 6, 8, 10]) model = LinearRegression().fit(x_train, y_train) x_test = np.array([[6], [7]]) y_test = np.array([12, 14])
Attempts:
2 left
💡 Hint
Remember the order of arguments for r2_score is (true values, predicted values).
✗ Incorrect
Option C correctly predicts y_test from x_test, then computes r2_score(y_test, predictions).