Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import necessary libraries
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
import streamlit as st
|
5 |
+
from sklearn.datasets import load_iris
|
6 |
+
from sklearn.model_selection import train_test_split
|
7 |
+
from sklearn.ensemble import RandomForestClassifier
|
8 |
+
from lime.lime_tabular import LimeTabularExplainer
|
9 |
+
|
10 |
+
# Load dataset
|
11 |
+
data = load_iris()
|
12 |
+
X = data.data
|
13 |
+
y = data.target
|
14 |
+
|
15 |
+
# Split dataset into training and testing sets
|
16 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
|
17 |
+
|
18 |
+
# Train a Random Forest classifier
|
19 |
+
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
20 |
+
model.fit(X_train, y_train)
|
21 |
+
|
22 |
+
# Create an explainer using LIME
|
23 |
+
explainer = LimeTabularExplainer(X_train, mode='classification', training_labels=y_train, feature_names=data.feature_names, class_names=data.target_names, discretize_continuous=True)
|
24 |
+
|
25 |
+
# Streamlit UI
|
26 |
+
st.title("Explainable AI with LIME")
|
27 |
+
st.write("This application demonstrates how to make AI models more interpretable using LIME.")
|
28 |
+
|
29 |
+
# User input for test instance index
|
30 |
+
idx = st.number_input("Select a test instance index to explain", min_value=0, max_value=len(X_test)-1, value=0)
|
31 |
+
|
32 |
+
# Choose a test instance to explain
|
33 |
+
instance = X_test[idx].reshape(1, -1)
|
34 |
+
|
35 |
+
# Get the explanation for the chosen instance
|
36 |
+
exp = explainer.explain_instance(instance[0], model.predict_proba)
|
37 |
+
|
38 |
+
# Display the explanation
|
39 |
+
st.write(f"Explanation for instance {idx}:")
|
40 |
+
st.write(exp.as_list())
|