Spaces:
Runtime error
Runtime error
File size: 1,419 Bytes
5656839 |
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 |
# Import necessary libraries
import numpy as np
import pandas as pd
import streamlit as st
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from lime.lime_tabular import LimeTabularExplainer
# Load dataset
data = load_iris()
X = data.data
y = data.target
# Split dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Train a Random Forest classifier
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
# Create an explainer using LIME
explainer = LimeTabularExplainer(X_train, mode='classification', training_labels=y_train, feature_names=data.feature_names, class_names=data.target_names, discretize_continuous=True)
# Streamlit UI
st.title("Explainable AI with LIME")
st.write("This application demonstrates how to make AI models more interpretable using LIME.")
# User input for test instance index
idx = st.number_input("Select a test instance index to explain", min_value=0, max_value=len(X_test)-1, value=0)
# Choose a test instance to explain
instance = X_test[idx].reshape(1, -1)
# Get the explanation for the chosen instance
exp = explainer.explain_instance(instance[0], model.predict_proba)
# Display the explanation
st.write(f"Explanation for instance {idx}:")
st.write(exp.as_list())
|