Styxis
Company
Knowledge

scikit-learn class_weight string keys: preserve the type of numeric-looking labels

Parsing a string label as an integer can create a different dictionary key even when the printed characters look identical.

Class identity is shared across teams

A class-label contract includes key type as well as value. Do not convert labels just because they look numeric. The team preparing labels and the team assigning weights can see the same characters while one uses strings and the other integers. Comparing only the printed error message makes that change easy to miss. [1, 2, 3]

balanced can still pass through a dictionary path

scikit-learn issue 34883 reports forest class_weight processing in 1.9 converting integer-like string classes into integers. It explains that balanced can first produce a dictionary keyed by original labels and then pass through another lookup path. Distinguish estimators that use the dedicated balanced path directly; the report does not establish the same effect for every string label or model. [1]

Reconcile values together with types

Compare source labels with weight keys by both type and value, correcting missing-class mappings before training. Preserve a type column even when exporting the audit table through CSV. Converting both sides to strings while discarding type information can hide exactly the mismatch under investigation. [1, 3]

String '1' and integer 1 are different lookups

Suppose an illustrative weight dictionary has string key '1' and the source class is also string '1'. Expected lookup state is matched. A type-preserving matched result passes. Converting to integer 1 and receiving missing looks up a different key and fails. Other correctly named classes do not make this class's missing weight acceptable. The fixture does not assess whether the weight magnitude is appropriate. [1, 3]

Keep type boundaries in the Truthound reference table

ReferentialIntegrityValidator in Truthound Core 3.3.2 compares input-column values with a reference column in prepared reference_data. A consistently normalized key that preserves both class type and value in both tables can expose missing references. Verify separately that normalization retained type distinctions. The validator does not repair forest coercion or certify the fairness of a weighting policy, so retest the actual training path after correction. [1, 2, 3]

Application conditions

Prepare original_class_text, original_class_type, weight_key_type and weight_lookup_status from raw data and approved references, preserving row identity.

Alternatives

Use an explicit conversion that preserves labels' natural types and run regression comparisons against a stable version.

Limitations

  • Matching keys does not establish appropriate weight magnitudes or the validity of a fairness policy.

Evidence

Event checked
Subject checked
Modified