Bird
Raised Fist0

Implement Logistic Regression - Binary Cross-Entropy, Sigmoid

CODINGbeginnerInterview freq: very_high
Choose your preparation mode6 modes available
๐ŸŽฏ
Implement Logistic Regression with Binary Cross-Entropy Loss
30 min ยท junior
junior

You are tasked with implementing a logistic regression model from scratch using NumPy. The model should be able to handle binary classification tasks efficiently. You must ensure that the implementation is robust to edge cases such as empty datasets and single-class datasets. The model should be optimized for datasets with up to 10,000 samples and 100 features.

  • Use NumPy for all numerical operations.
  • The model should use the sigmoid function for predictions.
  • Binary cross-entropy should be used as the loss function.
  • The implementation should include gradient descent for optimization.
  • Handle edge cases like empty datasets and single-class datasets.
๐Ÿ“‹
Guided Steps
1/5
Step 1
Initialize Parameters
How will you initialize the weights and bias for the logistic regression model?
๐Ÿ’ก Hint
Consider the impact of initialization on convergence speed and stability.
Initialize weights to small random values to break symmetry and bias to zero for simplicity.
Mid: Initializes weights randomly and bias to zero. Senior: Explains the impact of initialization on convergence and potential issues with symmetry.
โš ๏ธCommon Wrong Answer
Initializing all weights and bias to zero, leading to symmetry and poor convergence.
Step 2
Implement Sigmoid Function
How will you implement the sigmoid function for logistic regression?
๐Ÿ’ก Hint
Think about numerical stability when implementing the sigmoid function.
Use a numerically stable version of the sigmoid function to prevent overflow.
Mid: Implements basic sigmoid function. Senior: Uses a numerically stable version to handle large input values.
โš ๏ธCommon Wrong Answer
Using a naive sigmoid implementation that can overflow with large inputs.
Step 3
Compute Binary Cross-Entropy Loss
How will you compute the binary cross-entropy loss for the model?
๐Ÿ’ก Hint
Consider how to handle cases where predictions are exactly 0 or 1.
Clip predictions to avoid log(0) errors in the loss computation.
Mid: Computes loss using basic formula. Senior: Implements clipping to handle edge cases and explains why it's necessary.
โš ๏ธCommon Wrong Answer
Not handling log(0) cases, leading to NaN values in loss computation.
Step 4
Implement Gradient Descent
How will you implement gradient descent to optimize the model parameters?
๐Ÿ’ก Hint
Think about the learning rate and its impact on convergence.
Choose a suitable learning rate to balance convergence speed and stability.
Mid: Implements basic gradient descent. Senior: Discusses learning rate selection and potential use of learning rate schedules.
โš ๏ธCommon Wrong Answer
Using a fixed learning rate without considering its impact on convergence.
Step 5
Handle Edge Cases
How will you ensure your implementation handles edge cases like empty datasets and single-class datasets?
๐Ÿ’ก Hint
Consider how your implementation should behave with no data or no variability in data.
Implement checks for dataset validity and handle single-class scenarios gracefully.
Mid: Implements basic checks for empty datasets. Senior: Provides robust handling for single-class datasets and explains the implications.
โš ๏ธCommon Wrong Answer
Ignoring edge cases, leading to runtime errors or incorrect model behavior.
๐Ÿ“Š
Scoring Rubric
โ–ผ Show
Must Have - 60%
  • Correctly implements the sigmoid function with numerical stability.
  • Computes binary cross-entropy loss with clipping to avoid log(0) errors.
  • Implements gradient descent with a reasonable learning rate.
  • Handles empty datasets without crashing.
  • Initializes weights and bias appropriately to ensure convergence.
Good to Have - 30%
  • Explains the impact of initialization on model convergence.
  • Uses a learning rate schedule to improve convergence.
  • Provides detailed handling for single-class datasets.
  • Implements additional checks for data validity.
Bonus - 10%
  • Discusses potential improvements like regularization.
  • Implements early stopping to prevent overfitting.
  • Explains the trade-offs of different optimization algorithms.
โœ…
Model Answer
โ–ผ Reveal

The ideal implementation initializes weights randomly and bias to zero, uses a numerically stable sigmoid function, computes binary cross-entropy loss with clipping, and implements gradient descent with a suitable learning rate. It handles edge cases like empty and single-class datasets gracefully.

  • Initialize weights to small random values to ensure symmetry breaking.
  • Implement a numerically stable sigmoid function to handle large inputs.
  • Clip predictions in the loss computation to avoid log(0) errors.
  • Choose a learning rate that balances convergence speed and stability.
  • Implement checks for empty datasets and handle single-class scenarios.
Using a naive sigmoid implementation that can overflow.
Ignoring edge cases, leading to runtime errors.
Initializing all weights and bias to zero, causing poor convergence.
Not clipping predictions, resulting in NaN values in loss computation.

Practice

(1/5)
1. What is the time complexity of computing the ROC curve using the sklearn library?
easy
A. O(n log n)
B. O(n^2)
C. O(n)
D. O(log n)

Solution

  1. Step 1: Understand ROC computation

    ROC curve computation involves sorting the scores, which is O(n log n).
  2. Step 2: Eliminate wrong options

    O(n^2) is incorrect as it suggests quadratic complexity, O(n) suggests linear complexity which ignores sorting, and O(log n) is too low.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    ROC curve involves sorting, hence O(n log n) [OK]
Hint: ROC curve involves sorting, hence O(n log n) [OK]
Common Mistakes:
2. You implemented PCA but the variance explained by the first principal component is unexpectedly low. What is the most likely cause?
medium
A. Data was not centered before computing the covariance matrix.
B. Eigenvectors were not normalized.
C. Covariance matrix was computed using rowvar=True.
D. Too few principal components were selected.

Solution

  1. Step 1: Identify the impact of centering data

    Centering data is crucial for PCA as it ensures the covariance matrix reflects true variance.
  2. Step 2: Eliminate wrong options

    Eigenvectors were not normalized. is incorrect because normalization affects direction, not variance. Covariance matrix was computed using rowvar=True. affects orientation, not variance. Too few principal components were selected. is irrelevant to the variance of the first component.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Center data before PCA to capture variance. [OK]
Hint: Center data before PCA to capture variance. [OK]
Common Mistakes:
3. A Random Forest implementation is producing poor results. The trees seem to be too similar. What is the most likely cause?
medium
A. Insufficient feature sampling
B. High variance in the data
C. Too much data in each bootstrap sample
D. Too many trees in the forest

Solution

  1. Step 1: Identify the Cause of Similar Trees

    Similar trees often result from insufficient randomness in feature selection.
  2. Step 2: Eliminate Other Causes

    Too many trees (A) or high variance (D) don't cause similarity; too much data (C) affects overfitting, not similarity.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Feature sampling adds diversity [OK]
Hint: Feature sampling adds diversity [OK]
Common Mistakes:
4. How can you optimize the memory usage of a learned positional encoding implementation in PyTorch?
hard
A. Use a sparse matrix for the encoding.
B. Reduce the number of dimensions in the encoding.
C. Share the encoding matrix across multiple layers.
D. Use half-precision floating point for the encoding matrix.

Solution

  1. Step 1: Consider memory optimization techniques

    Using half-precision floating point reduces memory usage while maintaining sufficient precision for many applications.
  2. Step 2: Eliminate wrong options

    Reduce the number of dimensions in the encoding. changes the model architecture. Use a sparse matrix for the encoding. is not applicable as the matrix is dense. Share the encoding matrix across multiple layers. may affect model performance and is not a direct memory optimization.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Half-precision reduces memory without major precision loss. [OK]
Hint: Half-precision reduces memory without major precision loss. [OK]
Common Mistakes:
5. How can you optimize the memory usage of a Random Forest implementation with bagging and feature sampling?
hard
A. Use a single decision tree instead of a forest
B. Use integer data types for feature values
C. Reduce the number of features sampled per tree
D. Store trees as serialized objects on disk

Solution

  1. Step 1: Identify Memory Optimization Techniques

    Using smaller data types can significantly reduce memory usage.
  2. Step 2: Evaluate Options

    Using a single tree (A) reduces model complexity, not memory; reducing features (B) affects accuracy; serialization (D) affects storage, not runtime memory.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Smaller data types save memory [OK]
Hint: Smaller data types save memory [OK]
Common Mistakes:
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