Prediction Behavior with NumPy Arrays
In scikit-learn (1.0+), a Pipeline will not trigger a warning or error when you pass a NumPy array to the predict method, even if the model was trained on a pandas DataFrame with explicit feature names. The pipeline falls back to positional indexing, treating the input as an anonymous array.
Internal Validation and Silent Casting
The transition from named columns to anonymous arrays is handled via check_array(). Because NumPy arrays lack metadata, the pipeline cannot validate that the columns in your array correspond to the same features used during training. This creates a risk of silent casting issues:
- Positional Mapping: The pipeline maps input data to transformers based purely on column order. If the NumPy array's columns are not in the exact sequence as the training DataFrame, the model will process the wrong features without alerting you.
- ColumnTransformer Behavior: If your pipeline includes a
ColumnTransformer that was configured using column names, it will ignore those names when receiving a NumPy array and instead apply transformations based on the integer indices of the columns.
- Shape Validation: The only hard failure that occurs is a
ValueError if the number of columns in the NumPy array differs from the number of features the estimator expects.
Verification Steps
To ensure your pipeline is handling the transition correctly, perform these three checks:
- Consistency Check: Pass a DataFrame to
predict, then pass the equivalent NumPy array (df.values). The predictions must be identical.
- Order Sensitivity Check: Pass a NumPy array with columns reordered. If the predictions change (which they likely will), it confirms the pipeline is relying on positional mapping.
- Transformer Audit: If using
ColumnTransformer, verify that the array indices match the training column indices exactly.
Diagnostic Requirement
To provide a more specific recommendation: Are you using a ColumnTransformer with named column selectors in your pipeline? If so, the risk of silent data misalignment is significantly higher than in a linear pipeline of global transformers.