Skip to content

shravya312/explainableai

Β 
Β 

Repository files navigation

ExplainableAI πŸš€

PyPI version License: MIT Python Versions Downloads GitHub stars

ExplainableAI is a powerful Python package that combines state-of-the-art machine learning techniques with advanced explainable AI methods and LLM-powered explanations. 🌟

This project is now OFFICIALLY accepted for

GSSoC 2024 Extd
Hacktober fest 2024

🌟 Key Features

Feature Description
πŸ“Š Automated EDA Gain quick insights into your dataset.
🧠 Model Performance Evaluation Comprehensive metrics for model assessment.
πŸ“ˆ Feature Importance Analysis Understand which features drive your model's decisions.
πŸ” SHAP Integration Deep insights into model behavior using SHAP (SHapley Additive exPlanations).
πŸ“Š Interactive Visualizations Explore model insights through intuitive charts and graphs.
πŸ€– LLM-Powered Explanations Get human-readable explanations for model results and individual predictions.
πŸ“‘ Automated Report Generation Create professional PDF reports with a single command.
πŸ”€ Multi-Model Support Compare and analyze multiple ML models simultaneously.
βš™οΈ Easy-to-Use Interface Simple API for model fitting, analysis, and prediction.

πŸš€ Quick Start

pip install explainableai

ExplainableAI Example: Iris Dataset with Random Forest

πŸ“ Code Overview

This example demonstrates how to use the ExplainableAI package to fit a Random Forest model on the Iris dataset, analyze model behavior, and generate an LLM-powered explanation and PDF report.

from explainableai import XAIWrapper
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier

# Load dataset
X, y = load_iris(return_X_y=True, as_frame=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize and fit model
xai = XAIWrapper()
model = RandomForestClassifier(n_estimators=100, random_state=42)
xai.fit(model, X_train, y_train)

# Analyze and explain results
results = xai.analyze(X_test, y_test)
print(results['llm_explanation'])

# Generate report
xai.generate_report('iris_analysis.pdf')

πŸ› οΈ Installation & Setup

Install ExplainableAI via pip:

pip install explainableai

To use LLM-powered explanations, you need to set up the following environment variable:

GEMINI_API_KEY=your_api_key_here

πŸ–₯️ Usage Examples

Multimodal Example Usage for ExplainableAI

To create a multimodal example usage for your ExplainableAI project, we can incorporate various modes of interaction and output that enhance user engagement and understanding. This includes:

  1. Text Explanations: Providing clear and concise explanations for model predictions.
  2. Dynamic Visualizations: Integrating libraries to create real-time visualizations of model performance metrics and feature importance.
  3. Interactive Elements: Utilizing libraries to create an interactive interface where users can input data for real-time predictions and view explanations.

Implementation Steps

Example Code

Here’s a sample implementation that incorporates these multimodal elements:

from explainableai import XAIWrapper
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
import streamlit as st

# Load your dataset (Replace 'your_dataset.csv' with the actual file)
df = pd.read_csv('your_dataset.csv')
X = df.drop(columns=['target_column'])
y = df['target_column']

# Initialize the model
model = RandomForestClassifier(n_estimators=100, random_state=42)

# Initialize XAIWrapper
xai = XAIWrapper()
xai.fit(model, X, y)

# Streamlit UI
st.title("Explainable AI Model Prediction")
st.write("This application provides explanations for model predictions and visualizations.")

# User Input for Prediction
user_input = {}
for feature in X.columns:
    user_input[feature] = st.number_input(feature, value=0.0)

# Make prediction
if st.button("Predict"):
    new_data = pd.DataFrame(user_input, index=[0])
    prediction, probabilities, explanation = xai.explain_prediction(new_data)
    
    st.write(f"**Prediction:** {prediction}")
    st.write(f"**Probabilities:** {probabilities}")
    st.write(f"**Explanation:** {explanation}")

    # Dynamic Visualization
    st.subheader("Feature Importance")
    st.pyplot(xai.plot_feature_importance(model))

    st.subheader("SHAP Values")
    st.pyplot(xai.plot_shap_values(model))

# Generate report button
if st.button("Generate Report"):
    xai.generate_report('model_analysis_report.pdf')
    st.write("Report generated!")

πŸ€– Explaining Individual Predictions

# After fitting the model

# New data to be explained
new_data = {'feature_1': value1, 'feature_2': value2, ...}  # Dictionary of feature values

# Make a prediction with explanation
prediction, probabilities, explanation = xai.explain_prediction(new_data)

print(f"Prediction: {prediction}")
print(f"Probabilities: {probabilities}")
print(f"Explanation: {explanation}")

πŸ“Š Feature Overview

Module Description
explore() Automated exploratory data analysis (EDA) to uncover hidden insights.
fit() Train and analyze models with a simple API. Supports multiple models.
analyze() Evaluate model performance with SHAP and LLM-based explanations.
explain_prediction() Explain individual predictions in plain English using LLMs.
generate_report() Create professional PDF reports with visuals, explanations, and analysis.

🌍 Running Locally

To run ExplainableAI locally:

  1. Clone the repository:

    git clone https://github.com/ombhojane/explainableai.git
    cd explainableai

2.Install Dependencies:

To install the required dependencies, run the following command:

pip install -r requirements.txt

3.Set up your environment variables:

Add your GEMINI_API_KEY to the .env file.

GEMINI_API_KEY=your_api_key_here

🀝 Contributing

We welcome contributions to ExplainableAI! Please check out our Contributing Guidelines to get started. Contributions are what make the open-source community an incredible place to learn, inspire, and create.

πŸ“„ License

ExplainableAI is licensed under the MIT License.


πŸ™Œ Acknowledgements

ExplainableAI builds upon several open-source libraries, including:

πŸŽ‰ Thank You to All Our Amazing Contributors! πŸŽ‰

Contributors

We are incredibly grateful for your dedication and hard work. Your contributions have been invaluable in making this project a success. Thank you for being a part of our journey!

Let's continue to build great things together! πŸš€

Connect with Us 🀝

Thank you for exploring this project! We’d love to connect and hear from you. Reach out through any of the platforms below:

Twitter Email GitHub

About

Increase interpretability of your models!

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Jupyter Notebook 92.8%
  • Python 7.2%