-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_lime.py
More file actions
57 lines (46 loc) · 1.75 KB
/
debug_lime.py
File metadata and controls
57 lines (46 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Debug LIME explainer to understand the issue."""
import numpy as np
import pandas as pd
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
try:
from pyinterpret import LIMEExplainer
# Create synthetic classification dataset
X, y = make_classification(
n_samples=1000,
n_features=10,
n_informative=5,
n_redundant=2,
n_clusters_per_class=1,
random_state=42
)
# Create feature names
feature_names = [f'feature_{i}' for i in range(X.shape[1])]
X_df = pd.DataFrame(X, columns=feature_names)
# Split data
X_train, X_test, y_train, y_test = train_test_split(
X_df, y, test_size=0.2, random_state=42
)
# Train a Random Forest model
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
print("Creating LIME explainer...")
lime_explainer = LIMEExplainer(model, mode='classification')
print("LIME explainer created successfully")
print("Mode attribute before fit:", hasattr(lime_explainer, 'mode'), getattr(lime_explainer, 'mode', 'NOT_FOUND'))
print("Calling fit...")
lime_explainer.fit(X_train)
print("Fit completed")
print("Mode attribute after fit:", hasattr(lime_explainer, 'mode'), getattr(lime_explainer, 'mode', 'NOT_FOUND'))
# Get single instance
instance = X_test.iloc[0]
print("Instance shape:", instance.shape)
print("Calling explain_instance...")
lime_result = lime_explainer.explain_instance(instance)
print("Success!")
except Exception as e:
import traceback
print("Error:", e)
print("Traceback:")
traceback.print_exc()