Typorithm · medium

Missing values, infinities and an honest denominator

pythonnumpynumerical-computingindependent-practice
Saved on this device
Full lesson · 4 learning steps Open any exercise for focused practice
1

Step 1 of 4 · intro card

Missing values, infinities and an honest denominator

0%

Concept and contract

Report how many measurements were actually summarized and keep an empty result distinct from zero.

Missing, infinite and ordinary zero values represent different facts. A zero can be a measured absence of activity, while None or NaN can mean that no measurement was available. Positive infinity is numerical but not a finite observed quantity for this report. The contract converts a vector to float64, keeps finite values, and returns the valid count, excluded count and an optional arithmetic mean.

An empty valid population has no observed mean. Returning None communicates that state without inventing zero. The two counts make the denominator reviewable. A summary should not quietly compare one country with mostly missing observations to another with nearly complete coverage as though both were equally representative.

Mechanism and worked example

The worked function uses isfinite to build one mask, selects the corresponding values, and computes a mean only when the selected count is nonzero.

The worked function uses isfinite to build one mask, selects the corresponding values, and computes a mean only when the selected count is nonzero. Explicit int and float conversions produce ordinary Python scalars at the report boundary. The examples include None, a string that converts to infinity, negative values and an empty vector so that each excluded-value decision is visible.

The numeric conversion policy is narrow and documented here. It is not a general-purpose parser for currency labels, locale-specific decimals or arbitrary source text. Those require separate validation before entering a numerical array. Errors in source parsing must not be hidden by treating every unrecognized token as a legitimate missing observation.

Complete worked example

python

import numpy as np

def inspect_example(values):
    data = np.asarray(values, dtype=np.float64)
    if data.ndim != 1:
        raise ValueError("expected a vector")
    valid = np.isfinite(data)
    selected = data[valid]
    return {"validCount": int(selected.size),
            "excludedCount": int(data.size - selected.size),
            "mean": float(selected.mean()) if selected.size else None}
Reasoning
The worked function uses isfinite to build one mask, selects the corresponding values, and computes a mean only when the selected count is nonzero. Explicit int and float conversions produce ordinary Python scalars at the report boundary. The examples include None, a string that converts to infinity, negative values and an empty vector so that each excluded-value decision is visible. The numeric conversion policy is narrow and documented here. It is not a general-purpose parser for currency labels, locale-specific decimals or arbitrary source text. Those require separate validation before entering a numerical array. Errors in source parsing must not be hidden by treating every unrecognized token as a legitimate missing observation.
Open in local Python playground ↗

Focused decision: context is in the complete example

python

valid = np.isfinite(data)
selected = data[valid]
mean = float(selected.mean()) if selected.size else None
Focused decision
Type this short numerical decision after reading its complete context. The complete imports, array construction, validation, and result checks remain in the worked example. Typing records practice; only the downloadable computer lab executes Python.

Counterexamples and common mistakes

The controlled mistake excludes NaN but allows infinity.

The controlled mistake excludes NaN but allows infinity. Its count can look plausible while its mean becomes infinite. Using nanmean alone would not establish the desired infinity policy either, and an all-missing input requires explicit handling rather than relying on a warning and a NaN return value. Replacing every missing value with zero is another policy change, not a neutral cleanup step.

Never divide by the original vector length after filtering values. That denominator describes a different population. Also avoid reporting an excluded count as if every exclusion were missing data; a later task may intentionally exclude valid but out-of-scope measurements.

Controlled mistake: inspect the failing numerical contract

python

import numpy as np

def inspect_example(values):
    data = np.asarray(values, dtype=np.float64)
    if data.ndim != 1:
        raise ValueError("expected a vector")
    valid = ~np.isnan(data)
    selected = data[valid]
    return {"validCount": int(selected.size),
            "excludedCount": int(data.size - selected.size),
            "mean": float(selected.mean()) if selected.size else None}
Reasoning
The controlled mistake excludes NaN but allows infinity. Its count can look plausible while its mean becomes infinite. Using nanmean alone would not establish the desired infinity policy either, and an all-missing input requires explicit handling rather than relying on a warning and a NaN return value. Replacing every missing value with zero is another policy change, not a neutral cleanup step. Never divide by the original vector length after filtering values. That denominator describes a different population. Also avoid reporting an excluded count as if every exclusion were missing data; a later task may intentionally exclude valid but out-of-scope measurements.
Open in local Python playground ↗

Independent practice

Limit the summary to strictly positive finite measurements.

Limit the summary to strictly positive finite measurements. Preserve the same three report fields, but interpret excludedCount as all observations outside that new scope. Keep zero separate from missingness: zero is excluded because of the positive-value policy, not because it was absent.

Predict the count and mean for [-1, 0, 1, None]. Only one measurement should remain in the changed task. An input containing only non-positive or missing values should produce zero valid observations and mean None, while still reporting the full excluded count.

Step 1: Summarize strictly positive finite measurements only. Step 2: Retain the working imports, input validation and array shapes. Change the stated policy, run the supplied practice cases, then explain the newly accepted or rejected boundary.

Practice starter: solve before reading the solution

python

import numpy as np

def inspect_example(values):
    data = np.asarray(values, dtype=np.float64)
    if data.ndim != 1:
        raise ValueError("expected a vector")
    valid = np.isfinite(data)
    selected = data[valid]
    return {"validCount": int(selected.size),
            "excludedCount": int(data.size - selected.size),
            "mean": float(selected.mean()) if selected.size else None}

# Independent task: Summarize strictly positive finite measurements only.
Reasoning
Summarize strictly positive finite measurements only.
Open in local Python playground ↗
  • Hint 1: The eligibility mask owns the population for both count and mean.
  • Hint 2: Combine np.isfinite(data) with (data > 0) using &.

Solution, tests and trade-offs

The solution adds data > 0 to the finite mask.

The solution adds data > 0 to the finite mask. The mean and validCount both use that exact selected vector, so their populations stay aligned. Tests check an empty input and an entirely excluded input, preventing a fallback that divides by one or reports a fake zero mean. Values that convert to positive infinity remain excluded.

This exercise teaches representation and population accounting, not the suitability of a mean for every dataset. Outliers, skew, weighted measurements and sampling bias require additional statistical reasoning. Preserving the counts now gives the later statistics course a reliable basis for those decisions.

Practice solution and acceptance checks

python

import numpy as np

def inspect_example(values):
    data = np.asarray(values, dtype=np.float64)
    if data.ndim != 1:
        raise ValueError("expected a vector")
    valid = np.isfinite(data) & (data > 0)
    selected = data[valid]
    return {"validCount": int(selected.size),
            "excludedCount": int(data.size - selected.size),
            "mean": float(selected.mean()) if selected.size else None}
Reasoning
The solution adds data > 0 to the finite mask. The mean and validCount both use that exact selected vector, so their populations stay aligned. Tests check an empty input and an entirely excluded input, preventing a fallback that divides by one or reports a fake zero mean. Values that convert to positive infinity remain excluded. This exercise teaches representation and population accounting, not the suitability of a mean for every dataset. Outliers, skew, weighted measurements and sampling bias require additional statistical reasoning. Preserving the counts now gives the later statistics course a reliable basis for those decisions.
Open in local Python playground ↗

Interview reasoning and next step

Explain the difference between zero, NaN, infinity and an empty sample.

Explain the difference between zero, NaN, infinity and an empty sample. Show why a missing-data policy changes both numerator and denominator. Discuss which report fields let another person check whether a comparison uses the same population. Avoid claiming that a finite numerical result automatically establishes a trustworthy estimate.

Type the finite mask and guarded mean, then reconstruct the decision for an empty selection. The app questions focus on these meanings. Next, perform elementwise division without calculating invalid branches and without leaving parts of an output array uninitialized.

Optional stretch — after the core task works: Add one independently reasoned boundary case and explain its expected value before running it.

Computer practice: https://applydjinn.com/typorithm/numpy-lab-20261106.zip. Install pinned dependencies in an isolated Python environment. Run verify.py, then change the working exercise baseline and run --practice np-missing. The real NumPy arrays, numerical contracts, random state checks and temporary numeric files use owned fixtures only. No production database, feed, account or paid service is used. This lab executes Python; the PC typing trainer and native reading progress do not execute it.

Report how many measurements were actually summarized and keep an empty result distinct from zero.