Convert logistic regression coefficients to odds ratios with NumPy

Quick Overview

Write a NumPy function that turns a vector of logistic regression log-odds coefficients into odds ratios without mutating the input. It tests how coefficients map to odds, NumPy dtype handling, and how to build a confidence interval on the odds ratio scale.

Convert logistic regression coefficients to odds ratios with NumPy

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Write `coef_to_odds_ratio(coefs: np.ndarray) -> np.ndarray`. Given a 1-D array of log-odds coefficients from a fitted logistic regression, return a new 1-D array of odds ratios: the element-wise $e^{\beta}$ of each coefficient $\beta$. Requirements: - The result has the same shape as the input and `dtype == np.float64`. - Do not mutate the input array. Example: ```python coefs = np.array([0.0, 0.156, -0.4, 1.2]) coef_to_odds_ratio(coefs) # array([1. , 1.16882..., 0.67032..., 3.32011...]) ``` ```hint Watch the dtype Check which dtype NumPy returns for a float32 or small integer input, and make sure nothing you call writes back into the caller's array. ``` ### What a Strong Answer Covers - A vectorized element-wise computation instead of a Python loop - A float64 result for any numeric input dtype, and a reason why the cast is needed - Why the chosen operations leave the input array untouched - How to read an odds ratio relative to 1, per unit change in the feature - Numeric edge cases such as overflow for very large coefficients ### Follow-up Questions - How would you also return a confidence interval for each odds ratio? - Why is an odds ratio not the same as the change in probability, and how would you report the effect on probability instead? - How does the interpretation change if the feature was standardized before the model was fit? - What does a coefficient large enough to overflow tell you about the fitted model?

Overview: Write a NumPy function that turns a vector of logistic regression log-odds coefficients into odds ratios without mutating the input. It tests how coefficients map to odds, NumPy dtype handling, and how to build a confidence interval on the odds ratio scale.

Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Write coef_to_odds_ratio(coefs: np.ndarray) -> np.ndarray. Given a 1-D array of log-odds coefficients from a fitted logistic regression, return a new 1-D array of odds ratios: the element-wise eβe^{\beta} of each coefficient β\beta.

Requirements:

  • The result has the same shape as the input and dtype == np.float64 .
  • Do not mutate the input array.

Example:

coefs = np.array([0.0, 0.156, -0.4, 1.2])
coef_to_odds_ratio(coefs)
# array([1.        , 1.16882..., 0.67032..., 3.32011...])

What a Strong Answer Covers Guidance

  • A vectorized element-wise computation instead of a Python loop
  • A float64 result for any numeric input dtype, and a reason why the cast is needed
  • Why the chosen operations leave the input array untouched
  • How to read an odds ratio relative to 1, per unit change in the feature
  • Numeric edge cases such as overflow for very large coefficients

Follow-up Questions Guidance

  • How would you also return a confidence interval for each odds ratio?
  • Why is an odds ratio not the same as the change in probability, and how would you report the effect on probability instead?
  • How does the interpretation change if the feature was standardized before the model was fit?
  • What does a coefficient large enough to overflow tell you about the fitted model?