Bird
Raised Fist0
ML Pythonml~20 mins

First ML prediction (linear regression) in ML 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
1:30remaining
Output of simple linear regression prediction
What is the predicted value of y for x = 4 using the model y = 2x + 1?
ML Python
def predict(x):
    return 2 * x + 1

result = predict(4)
print(result)
A8
B7
C9
D10
Attempts:
2 left
💡 Hint
Multiply x by 2 and then add 1.
❓ Model Choice
intermediate
1:30remaining
Choosing the right model for linear data
You have data points that form a straight line. Which model is best to predict new points?
ANeural network with many layers
BDecision tree
CK-means clustering
DLinear regression
Attempts:
2 left
💡 Hint
Think about the simplest model that fits a straight line.
❓ Metrics
advanced
2:00remaining
Interpreting mean squared error (MSE)
If a linear regression model has an MSE of 0.01 on test data, what does this mean?
AThe average squared difference between predicted and actual values is 0.01
BThe model predicts perfectly with no errors
CThe model's predictions are off by 1 on average
DThe model's accuracy is 1%
Attempts:
2 left
💡 Hint
MSE measures average squared errors, not accuracy percentage.
🔧 Debug
advanced
2:00remaining
Why does this linear regression code fail?
What error will this code produce? from sklearn.linear_model import LinearRegression model = LinearRegression() X = [1, 2, 3, 4] y = [2, 4, 6, 8] model.fit(X, y)
ML Python
from sklearn.linear_model import LinearRegression
model = LinearRegression()
X = [1, 2, 3, 4]
y = [2, 4, 6, 8]
model.fit(X, y)
ANo error, code runs fine
BValueError because X must be 2D array
CTypeError because y is not a numpy array
DNameError because LinearRegression is not imported
Attempts:
2 left
💡 Hint
Check the shape of X expected by sklearn.
🧠 Conceptual
expert
2:00remaining
Effect of learning rate in linear regression training
What happens if the learning rate in gradient descent for linear regression is set too high?
AThe model may fail to converge and loss can increase or oscillate
BThe model will converge faster without any issues
CThe model will ignore the learning rate and converge normally
DThe model will always find the global minimum immediately
Attempts:
2 left
💡 Hint
Think about what happens when steps are too big in a hill-climbing problem.

Practice

(1/5)
1. What is the main goal of linear regression in machine learning?
easy
A. To classify data into categories
B. To find a line that best fits the data points
C. To cluster data points into groups
D. To reduce the number of features

Solution

  1. Step 1: Understand linear regression purpose

    Linear regression aims to find a straight line that best fits the relationship between input and output data.
  2. Step 2: Compare with other options

    Classification, clustering, and feature reduction are different tasks, not the goal of linear regression.
  3. Final Answer:

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

    Linear regression = line fitting [OK]
Hint: Linear regression fits a line, not groups or categories [OK]
Common Mistakes:
  • Confusing regression with classification
  • Thinking linear regression clusters data
  • Believing it reduces features
2. Which of the following is the correct Python syntax to create a simple linear regression model using scikit-learn?
easy
A. model = LinearRegression()
B. model = LinearRegression.fit()
C. model = LinearRegression.fit(X, y)
D. model = LinearRegression(X, y)

Solution

  1. Step 1: Recall model creation syntax

    In scikit-learn, you first create the model object with LinearRegression(), without arguments.
  2. Step 2: Understand fitting method

    The fit method is called on the model object with data, e.g., model.fit(X, y), not during creation.
  3. Final Answer:

    model = LinearRegression() -> Option A
  4. Quick Check:

    Model creation uses class name with parentheses [OK]
Hint: Create model with class(), fit separately [OK]
Common Mistakes:
  • Calling fit during model creation
  • Passing data to constructor
  • Using fit as a class method
3. Given the code below, what will be the predicted value for X_new = [[5]]?
from sklearn.linear_model import LinearRegression
X = [[1], [2], [3], [4]]
y = [2, 4, 6, 8]
model = LinearRegression()
model.fit(X, y)
prediction = model.predict([[5]])
print(prediction)
medium
A. [9.]
B. [12.]
C. [8.]
D. [10.]

Solution

  1. Step 1: Understand the training data pattern

    The output y doubles the input X: y = 2 * X.
  2. Step 2: Predict for X_new = 5

    Using the pattern, prediction = 2 * 5 = 10.
  3. Final Answer:

    [10.] -> Option D
  4. Quick Check:

    2 x 5 = 10 [OK]
Hint: Check pattern in training data, apply to new input [OK]
Common Mistakes:
  • Ignoring the pattern and guessing
  • Using last y value as prediction
  • Confusing input and output
4. What is wrong with the following code snippet for training 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. X should be a 2D array, not a 1D list
B. y should be a 2D array, not a list
C. LinearRegression() requires parameters in constructor
D. fit() method should be called with no arguments

Solution

  1. Step 1: Check input shape requirements

    scikit-learn expects X as a 2D array (list of lists), but X is a 1D list here.
  2. Step 2: Confirm y shape and method usage

    y can be 1D list, constructor needs no parameters, fit requires X and y arguments.
  3. Final Answer:

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

    X must be 2D for fit() [OK]
Hint: X must be 2D array for sklearn fit() [OK]
Common Mistakes:
  • Passing 1D list for X
  • Thinking y must be 2D
  • Adding parameters to LinearRegression()
5. You trained a linear regression model on data where y = 3x + 2. After training, you want to predict the output for x = 10. Which code snippet correctly predicts and prints the result?
hard
A. model.predict([10])\nprint(prediction)
B. prediction = model.predict([10, 2])\nprint(prediction)
C. prediction = model.predict([[10]])\nprint(prediction[0])
D. prediction = model.predict(10)\nprint(prediction)

Solution

  1. Step 1: Understand input shape for prediction

    scikit-learn expects a 2D array for prediction input, so [[10]] is correct.
  2. Step 2: Access prediction output correctly

    predict returns an array; to print the single value, use prediction[0].
  3. Final Answer:

    prediction = model.predict([[10]]) print(prediction[0]) -> Option C
  4. Quick Check:

    Input 2D array, print first prediction [OK]
Hint: Use 2D input for predict, print first element [OK]
Common Mistakes:
  • Passing 1D list or scalar to predict
  • Printing prediction array directly without indexing
  • Passing wrong shaped input with extra values
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