0
0
Data Analysis Pythondata~20 mins

Linear regression basics in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linear Regression Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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))
A10.0
B1.0
C0.5
D2.0
Attempts:
2 left
💡 Hint
Think about the relationship between x and y in the data.
data_output
intermediate
2: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))
A13.0
B12.0
C11.0
D15.0
Attempts:
2 left
💡 Hint
Look at the pattern in y values as x increases.
🔧 Debug
advanced
2: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)
Ax must be 2D array but is 1D, causing a ValueError
BThe model cannot fit because y values are not normalized
CLinearRegression requires integer inputs, but x is float
Dy must be 2D array but is 1D, causing a TypeError
Attempts:
2 left
💡 Hint
Check the shape of x expected by scikit-learn.
🧠 Conceptual
advanced
1:30remaining
Which statement best describes the intercept in linear regression?
In a simple linear regression model, what does the intercept represent?
AThe maximum value y can take in the model
BThe predicted value of y when x is zero
CThe slope of the line showing change in y per unit x
DThe error term in the regression equation
Attempts:
2 left
💡 Hint
Think about the point where the line crosses the y-axis.
🚀 Application
expert
2: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])
A
r2 = model.score(y_test, x_test)
print(round(r2, 2))
B
pred = model.predict(y_test)
r2 = r2_score(x_test, pred)
print(round(r2, 2))
C
pred = model.predict(x_test)
r2 = r2_score(y_test, pred)
print(round(r2, 2))
D
r2 = r2_score(model.predict(x_test), y_test)
print(round(r2, 2))
Attempts:
2 left
💡 Hint
Remember the order of arguments for r2_score is (true values, predicted values).