Detect single-feature complete separation for logistic regression

Quick Overview

Implement a NumPy check that reports whether any single feature column completely separates a binary label, without fitting a model. It tests vectorized array logic, the difference between complete and quasi-complete separation, and why separation matters for logistic regression.

Detect single-feature complete separation for logistic regression

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement a function that reports whether any single feature column completely separates the two classes of a binary label. Complete separation is the condition under which the logistic regression maximum likelihood estimate does not exist, so it is a useful check to run before fitting. Do not fit any model. ```python def detect_complete_separation(X: np.ndarray, y: np.ndarray) -> bool: ``` `X` has shape `(n_samples, n_features)` and holds float64 values. `y` has shape `(n_samples,)` and holds labels in `{0, 1}`. The function must follow these rules: - Column `j` separates when `max(X[y==0, j]) < min(X[y==1, j])` or `max(X[y==1, j]) < min(X[y==0, j])`. - Return `True` if any column separates, else `False`. - The inequality is strict: ranges that touch (quasi-complete separation) return `False`. - Return a native Python `bool`, not `np.bool_`. Example: ```python X = np.array([[1.0], [2.0], [5.0], [6.0]]) y = np.array([0, 0, 1, 1]) detect_complete_separation(X, y) # True (2.0 < 5.0) X = np.array([[1.0], [3.0], [3.0], [6.0]]) y = np.array([0, 0, 1, 1]) detect_complete_separation(X, y) # False (max class0 == min class1 == 3.0) ``` ```hint Work on all columns at once NumPy reductions take an `axis` argument, so the per-class statistics for every column can come from one call per statistic instead of a Python loop. ``` ```hint Check what you return Comparisons between NumPy arrays produce `np.bool_` values, and the class coded 1 can sit at either end of a column. Make sure both orderings are tested and the final value is a plain `bool`. ``` ### Constraints & Assumptions - Both classes are present in `y`. - Only univariate separation is in scope: linear combinations of features are not considered. - No model fitting of any kind is allowed; the answer must come from array logic alone. ### What a Strong Answer Covers - The separation condition for one column, tested in both directions with a strict inequality, and checked against both examples. - An implementation that runs in time linear in the size of `X`, with its memory cost stated. - The return type and edge cases such as an empty class, constant columns and NaN values. - Why touching ranges count as quasi-complete separation and why separation causes trouble for logistic regression. ### Follow-up Questions - How would you detect separation along a linear combination of features, which a single-column check misses? - Once separation is detected, how do you still fit a usable model? - What does a standard logistic regression fit look like in its output when the data are separated?

Overview: Implement a NumPy check that reports whether any single feature column completely separates a binary label, without fitting a model. It tests vectorized array logic, the difference between complete and quasi-complete separation, and why separation matters for logistic regression.

General logo
General
Nov 25, 2026
mediumData ScientistOtherMachine Learning
0
0

Implement a function that reports whether any single feature column completely separates the two classes of a binary label. Complete separation is the condition under which the logistic regression maximum likelihood estimate does not exist, so it is a useful check to run before fitting. Do not fit any model.

def detect_complete_separation(X: np.ndarray, y: np.ndarray) -> bool:

X has shape (n_samples, n_features) and holds float64 values. y has shape (n_samples,) and holds labels in {0, 1}. The function must follow these rules:

  • Column j separates when max(X[y==0, j]) < min(X[y==1, j]) or max(X[y==1, j]) < min(X[y==0, j]) .
  • Return True if any column separates, else False .
  • The inequality is strict: ranges that touch (quasi-complete separation) return False .
  • Return a native Python bool , not np.bool_ .

Example:

X = np.array([[1.0], [2.0], [5.0], [6.0]])
y = np.array([0, 0, 1, 1])
detect_complete_separation(X, y)  # True  (2.0 < 5.0)

X = np.array([[1.0], [3.0], [3.0], [6.0]])
y = np.array([0, 0, 1, 1])
detect_complete_separation(X, y)  # False (max class0 == min class1 == 3.0)

Constraints & Assumptions

  • Both classes are present in y .
  • Only univariate separation is in scope: linear combinations of features are not considered.
  • No model fitting of any kind is allowed; the answer must come from array logic alone.

What a Strong Answer Covers Guidance

  • The separation condition for one column, tested in both directions with a strict inequality, and checked against both examples.
  • An implementation that runs in time linear in the size of X , with its memory cost stated.
  • The return type and edge cases such as an empty class, constant columns and NaN values.
  • Why touching ranges count as quasi-complete separation and why separation causes trouble for logistic regression.

Follow-up Questions Guidance

  • How would you detect separation along a linear combination of features, which a single-column check misses?
  • Once separation is detected, how do you still fit a usable model?
  • What does a standard logistic regression fit look like in its output when the data are separated?