Bird
Raised Fist0
ML Pythonml~20 mins

Logistic regression in ML Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Logistic Regression Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the output of logistic regression
What does the output of a logistic regression model represent?
AThe probability that the input belongs to the positive class
BThe exact class label predicted by the model
CThe distance of the input from the decision boundary in feature space
DThe sum of squared errors between predicted and actual values
Attempts:
2 left
💡 Hint
Think about what logistic regression outputs before applying a threshold.
❓ Predict Output
intermediate
2:00remaining
Output of logistic regression prediction code
What is the output of this Python code using scikit-learn's LogisticRegression?
ML Python
from sklearn.linear_model import LogisticRegression
import numpy as np

X = np.array([[1, 2], [2, 3], [3, 4], [4, 5]])
y = np.array([0, 0, 1, 1])
model = LogisticRegression()
model.fit(X, y)
preds = model.predict(np.array([[1, 2], [3, 5]]))
print(preds.tolist())
A[1, 1]
B[1, 0]
C[0, 0]
D[0, 1]
Attempts:
2 left
💡 Hint
The model learns to separate classes based on the training data pattern.
❓ Hyperparameter
advanced
2:00remaining
Choosing the regularization parameter in logistic regression
Which effect does increasing the regularization strength (C parameter) in scikit-learn's LogisticRegression have?
AIt decreases regularization, allowing the model to fit training data more closely
BIt changes the number of iterations during training
CIt changes the learning rate of the model
DIt increases regularization, forcing the model to be simpler
Attempts:
2 left
💡 Hint
Remember that in scikit-learn, C is the inverse of regularization strength.
❓ Metrics
advanced
2:00remaining
Interpreting logistic regression evaluation metrics
If a logistic regression model has a high accuracy but low recall on the positive class, what does this imply?
AThe model performs well on both positive and negative classes
BThe model correctly identifies most positive cases but misclassifies many negatives
CThe model misses many positive cases but correctly identifies most negatives
DThe model has overfitted the training data
Attempts:
2 left
💡 Hint
Recall measures how many actual positives are found by the model.
🔧 Debug
expert
2:00remaining
Identifying the cause of poor logistic regression convergence
Given this code snippet, why might the logistic regression model fail to converge?
ML Python
from sklearn.linear_model import LogisticRegression
import numpy as np

X = np.random.rand(1000, 50)
y = np.random.randint(0, 2, 1000)
model = LogisticRegression(max_iter=10)
model.fit(X, y)
AThe input features X contain random values causing errors
Bmax_iter is too low for the model to converge on this data
CThe target y has only two classes which logistic regression cannot handle
DThe model requires normalization of X before fitting
Attempts:
2 left
💡 Hint
Consider the number of iterations allowed for training.

Practice

(1/5)
1. What is the main purpose of logistic regression in machine learning?
easy
A. To cluster data points into groups
B. To predict binary outcomes like yes/no or true/false
C. To reduce the number of features in data
D. To generate random data samples

Solution

  1. Step 1: Understand logistic regression goal

    Logistic regression is used to predict categories with two possible outcomes, such as yes/no or true/false.
  2. Step 2: Compare with other options

    Clustering groups data (not logistic regression), feature reduction simplifies data, and random sampling is unrelated.
  3. Final Answer:

    To predict binary outcomes like yes/no or true/false -> Option B
  4. Quick Check:

    Logistic regression = binary prediction [OK]
Hint: Logistic regression predicts yes/no answers fast [OK]
Common Mistakes:
  • Confusing logistic regression with clustering
  • Thinking it reduces features
  • Assuming it generates data
2. Which of the following is the correct way to import logistic regression from scikit-learn in Python?
easy
A. import logistic_regression from sklearn.linear_model
B. import LogisticRegression from sklearn.linear_model
C. from sklearn import LogisticRegression
D. from sklearn.linear_model import LogisticRegression

Solution

  1. Step 1: Recall correct import syntax

    In Python, the correct import uses 'from module import class' format, so 'from sklearn.linear_model import LogisticRegression' is correct.
  2. Step 2: Identify incorrect syntax

    Options B, C, and D use wrong order or wrong names causing syntax errors.
  3. Final Answer:

    from sklearn.linear_model import LogisticRegression -> Option D
  4. Quick Check:

    Correct import = from sklearn.linear_model import LogisticRegression [OK]
Hint: Use 'from module import class' for sklearn imports [OK]
Common Mistakes:
  • Using wrong import order
  • Wrong class or module names
  • Using lowercase instead of correct class name
3. What will be the output of this Python code snippet?
from sklearn.linear_model import LogisticRegression
import numpy as np

X = np.array([[0], [1], [2], [3]])
y = np.array([0, 0, 1, 1])
model = LogisticRegression()
model.fit(X, y)
pred = model.predict([[1.5]])
print(pred)
medium
A. [1]
B. [0]
C. [2]
D. Error

Solution

  1. Step 1: Understand training data and labels

    X has values 0,1 labeled 0 and 2,3 labeled 1. The model learns to separate near 1.5 between classes 0 and 1.
  2. Step 2: Predict for input 1.5

    Since 1.5 is closer to class 1 range, logistic regression predicts class 1.
  3. Final Answer:

    [1] -> Option A
  4. Quick Check:

    Input 1.5 predicts class 1 [OK]
Hint: Predict near boundary to guess class [OK]
Common Mistakes:
  • Assuming prediction is 0 for 1.5
  • Expecting error due to input shape
  • Confusing output format
4. Identify the error in this logistic regression code snippet:
from sklearn.linear_model import LogisticRegression
X = [[0], [1], [2], [3]]
y = [0, 0, 1, 1]
model = LogisticRegression()
model.fit(X, y)
pred = model.predict([1.5])
print(pred)
medium
A. Input to predict should be 2D array, not 1D
B. Labels y should be strings, not integers
C. Model must be initialized with parameters
D. fit method requires numpy arrays, not lists

Solution

  1. Step 1: Check input shape for prediction

    Model expects 2D array for predict input, but [1.5] is 1D. It should be [[1.5]].
  2. Step 2: Verify other parts

    Labels as integers are fine, model can be default, and fit accepts lists converted internally.
  3. Final Answer:

    Input to predict should be 2D array, not 1D -> Option A
  4. Quick Check:

    Predict input shape = 2D array [OK]
Hint: Always use double brackets for single sample prediction [OK]
Common Mistakes:
  • Passing 1D array to predict
  • Thinking labels must be strings
  • Assuming model needs parameters to initialize
5. You have a dataset with features that are not scaled and vary widely in range. What is the best practice before applying logistic regression to improve model performance?
hard
A. Use logistic regression without any preprocessing
B. Remove half of the features randomly
C. Scale or normalize features to similar ranges
D. Increase the number of training samples without scaling

Solution

  1. Step 1: Understand feature scaling importance

    Logistic regression uses weights sensitive to feature scale; large differences can cause poor training.
  2. Step 2: Choose best preprocessing

    Scaling or normalizing features to similar ranges helps the model learn better and converge faster.
  3. Final Answer:

    Scale or normalize features to similar ranges -> Option C
  4. Quick Check:

    Feature scaling improves logistic regression [OK]
Hint: Always scale features before logistic regression [OK]
Common Mistakes:
  • Ignoring feature scaling
  • Randomly removing features
  • Assuming more data fixes scaling issues
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