Skip to content

Comments

Fix RFI bug: Handle scalar/array diffs without to_numpy() method (AttributeError: 'float' object has no attribute 'to_numpy')#90

Open
jemus42 wants to merge 1 commit intogcskoenig:masterfrom
jemus42:fix-rfi-to-numpy-bug
Open

Fix RFI bug: Handle scalar/array diffs without to_numpy() method (AttributeError: 'float' object has no attribute 'to_numpy')#90
jemus42 wants to merge 1 commit intogcskoenig:masterfrom
jemus42:fix-rfi-to-numpy-bug

Conversation

@jemus42
Copy link

@jemus42 jemus42 commented Jun 14, 2025

  • Use np.asarray() for scalars and numpy arrays
  • Use to_numpy() only for pandas objects that have it

Disclaimer: I barely know what I'm doing but doing this... helped?

Reprex:

import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from fippy.explainers import Explainer
from fippy.samplers import GaussianSampler

# Generate simple test data
np.random.seed(123)
n_samples = 100
X = pd.DataFrame({
    'feature1': np.random.normal(0, 1, n_samples),
    'feature2': np.random.normal(0, 1, n_samples),
    'feature3': np.random.normal(0, 1, n_samples),
})
# Simple linear relationship
y = pd.Series(2 * X['feature1'] + X['feature2'] + np.random.normal(0, 0.1, n_samples))

# Train/test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=123)

# Train model
model = RandomForestRegressor(n_estimators=50, random_state=123)
model.fit(X_train, y_train)

# Set up fippy explainer
sampler = GaussianSampler(X_train)
explainer = Explainer(model.predict, X_train, loss=mean_squared_error, sampler=sampler)

# RFI with conditioning set - this will trigger the bug
conditioning_set = ["feature1"]  # Condition on feature1

explainer.rfi(conditioning_set, X_test.head(10), y_test.head(10))

- Replace diffs.to_numpy() with conditional logic
- Use np.asarray() for scalars and numpy arrays
- Use to_numpy() only for pandas objects that have it
- Fixes AttributeError: 'float' object has no attribute 'to_numpy'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant