Bird
Raised Fist0
Data Analysis Pythondata~20 mins

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

Choose your learning style10 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).

Practice

(1/5)
1. What is the main goal of linear regression in data analysis?
easy
A. To find a straight line that best fits the data points
B. To group data points into clusters
C. To calculate the average of all data points
D. To sort data points in ascending order

Solution

  1. Step 1: Understand the purpose of linear regression

    Linear regression aims to find a straight line that best represents the relationship between two variables.
  2. Step 2: Compare options with the goal

    Only To find a straight line that best fits the data points describes finding a straight line that fits data points, which matches the goal of linear regression.
  3. Final Answer:

    To find a straight line that best fits the data points -> Option A
  4. Quick Check:

    Linear regression = best fit line [OK]
Hint: Linear regression fits a line to data points [OK]
Common Mistakes:
  • Confusing regression with clustering
  • Thinking regression sorts or averages data
  • Mixing up regression with classification
2. Which of the following is the correct way to import the LinearRegression class from scikit-learn in Python?
easy
A. from sklearn.linear_model import LinearRegression
B. import LinearRegression from sklearn.linear_model
C. from sklearn import LinearRegression
D. import linear_model.LinearRegression

Solution

  1. Step 1: Recall correct import syntax in Python

    Python uses 'from module import class' to import specific classes.
  2. Step 2: Match with scikit-learn syntax

    Scikit-learn's LinearRegression is in sklearn.linear_model, so the correct import is 'from sklearn.linear_model import LinearRegression'.
  3. Final Answer:

    from sklearn.linear_model import LinearRegression -> Option A
  4. Quick Check:

    Correct import = from sklearn.linear_model import LinearRegression [OK]
Hint: Use 'from module import class' for specific imports [OK]
Common Mistakes:
  • Using wrong import order
  • Trying to import class directly from sklearn
  • Using incorrect module names
3. What will be the output of this Python code using scikit-learn's LinearRegression?
from sklearn.linear_model import LinearRegression
import numpy as np

X = np.array([[1], [2], [3], [4]])
y = np.array([2, 4, 6, 8])
model = LinearRegression()
model.fit(X, y)
pred = model.predict([[5]])
print(round(pred[0], 1))
medium
A. 12.0
B. 9.0
C. 10.0
D. 8.0

Solution

  1. Step 1: Understand the data and model fitting

    X values are 1 to 4, y values are 2, 4, 6, 8, which is y = 2 * x perfectly.
  2. Step 2: Predict for X=5 using the fitted model

    Since y = 2 * x, prediction for 5 is 2 * 5 = 10. The code rounds to 1 decimal place, so output is 10.0.
  3. Final Answer:

    10.0 -> Option C
  4. Quick Check:

    Prediction = 2 * 5 = 10.0 [OK]
Hint: Check if data fits y=mx+b, then predict easily [OK]
Common Mistakes:
  • Ignoring rounding in print
  • Confusing input shape for predict
  • Assuming wrong slope or intercept
4. Identify the error in this code snippet for fitting a linear regression model:
from sklearn.linear_model import LinearRegression
X = [1, 2, 3, 4]
y = [2, 4, 6, 8]
model = LinearRegression()
model.fit(X, y)
medium
A. LinearRegression cannot be imported from sklearn.linear_model
B. y should be a 2D array, not a list
C. model.fit requires no arguments
D. X should be a 2D array, not a 1D list

Solution

  1. Step 1: Check input shapes for model.fit

    LinearRegression expects X as 2D array (samples, features). Here X is 1D list, which causes error.
  2. Step 2: Confirm y shape is acceptable

    y can be 1D array/list for target values, so y is fine.
  3. Final Answer:

    X should be a 2D array, not a 1D list -> Option D
  4. Quick Check:

    X must be 2D for fit() [OK]
Hint: X must be 2D array for sklearn fit() [OK]
Common Mistakes:
  • Passing 1D X array causes shape error
  • Thinking y must be 2D
  • Misunderstanding import statements
5. You have data of house sizes (in square meters) and their prices (in thousands). You want to predict the price of a house with 120 square meters using linear regression. Given these data points:
sizes = [50, 80, 100, 150]
prices = [150, 200, 250, 350]
Which code snippet correctly fits the model and predicts the price for 120 square meters?
hard
A. from sklearn.linear_model import LinearRegression X = sizes y = prices model = LinearRegression() model.fit(X, y) pred = model.predict([120]) print(pred)
B. from sklearn.linear_model import LinearRegression import numpy as np X = np.array(sizes).reshape(-1,1) y = np.array(prices) model = LinearRegression() model.fit(X, y) pred = model.predict([[120]]) print(round(pred[0], 2))
C. import LinearRegression X = sizes model = LinearRegression() model.fit(X, prices) pred = model.predict(120) print(pred)
D. from sklearn.linear_model import LinearRegression import numpy as np X = np.array(sizes) y = np.array(prices) model = LinearRegression() model.fit(X, y) pred = model.predict(np.array([120])) print(pred)

Solution

  1. Step 1: Prepare data in correct shape for fitting

    LinearRegression requires X as 2D array, so sizes must be reshaped with .reshape(-1,1).
  2. Step 2: Fit model and predict correctly

    from sklearn.linear_model import LinearRegression import numpy as np X = np.array(sizes).reshape(-1,1) y = np.array(prices) model = LinearRegression() model.fit(X, y) pred = model.predict([[120]]) print(round(pred[0], 2)) reshapes X, fits model, predicts with [[120]] (2D), and rounds output, which is correct usage.
  3. Final Answer:

    from sklearn.linear_model import LinearRegression import numpy as np X = np.array(sizes).reshape(-1,1) y = np.array(prices) model = LinearRegression() model.fit(X, y) pred = model.predict([[120]]) print(round(pred[0], 2)) -> Option B
  4. Quick Check:

    Reshape X and predict with 2D input [OK]
Hint: Always reshape X to 2D before fitting and predicting [OK]
Common Mistakes:
  • Not reshaping X to 2D array
  • Passing 1D array to predict
  • Incorrect import or usage of LinearRegression
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard