TheAlgorithms/Python · error · TypeError

Actual result should be float. Value passed is a list

Error message

Actual result should be float. Value passed is a list

What it means

Raised as TypeError by data_safety_checker when actual_result is not a float. The function compares each vote against a scalar actual value with abs(); a list (or int/str) would either do elementwise comparison or raise later, so the type is enforced up front. Note the check is strict isinstance, so ints are rejected too.

Source

Thrown at machine_learning/forecasting/run.py:111

    iqr = q3 - q1
    low_lim = q1 - (iqr * 0.1)
    return float(low_lim)


def data_safety_checker(list_vote: list, actual_result: float) -> bool:
    """
    Used to review all the votes (list result prediction)
    and compare it to the actual result.
    input : list of predictions
    output : print whether it's safe or not
    >>> data_safety_checker([2, 3, 4], 5.0)
    False
    """
    safe = 0
    not_safe = 0

    if not isinstance(actual_result, float):
        raise TypeError("Actual result should be float. Value passed is a list")

    for i in list_vote:
        if i > actual_result:
            safe = not_safe + 1
        elif abs(abs(i) - abs(actual_result)) <= 0.1:
            safe += 1
        else:
            not_safe += 1
    return safe > not_safe


if __name__ == "__main__":
    """
    data column = total user in a day, how much online event held in one day,
    what day is that(sunday-saturday)
    """
    data_input_df = pd.read_csv("ex_data.csv")

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass the arguments in the right order: list_vote first, then the scalar actual_result.
  2. Coerce to float explicitly: data_safety_checker(votes, float(actual_value)).
  3. If actual_result is an array of one element, index it first (as run.py does with test_user[0]).

Example fix

# before
data_safety_checker([2, 3, 4], test_user)  # test_user is a list

# after
data_safety_checker([2, 3, 4], float(test_user[0]))
Defensive patterns

Strategy: type-guard

Validate before calling

actual = float(np.asarray(actual_result).reshape(-1)[0])
safe = data_safety_checker(list_vote, actual)

Type guard

def is_scalar_float(v) -> bool:
    return isinstance(v, float) and not isinstance(v, bool)

Try / catch

try:
    data_safety_checker(votes, actual_result)
except TypeError as e:
    if "should be float" in str(e):
        data_safety_checker(votes, float(np.ravel(actual_result)[0]))
    else:
        raise

Prevention

When it happens

Trigger: Calling data_safety_checker(votes, [1.0, 2.0]) by swapping arguments, or passing an int like data_safety_checker(votes, 5) — both fail isinstance(actual_result, float).

Common situations: Argument order confusion since both parameters are positional; passing numpy floats (np.float64 IS a float subclass, so it passes) but passing np.float32 fails; passing the raw prediction list instead of the scalar actual value.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/b8edf328482d8f43. Report an issue: GitHub.