Implementing Visualizations
Hello again, dear readers! In our previous sessions, we understood LIME explanations in TrustyAI and implemented a simple linear model to explain loan approvals. While seeing the saliency values of each feature is insightful, a graphical representation can offer a clearer understanding of model decisions. Let's delve deeper!
Creating a LIME Feature Impact Visualization
Before we begin, ensure you've gone through Part 2 of our series.
1. Setting Up:
Ensure you've imported the necessary libraries:
import matplotlib.pyplot as plt
2. Generating LIME Explanations:
Using the TrustyAI library, generate the LIME explanations for your model as shown in the previous tutorial.
3. Visualizing Feature Impact:
Each feature's impact can be visualized as a bar in a bar chart. The height (positive or negative) indicates the magnitude of influence, and the color (blue for positive, red for negative, and green for the most influential) signifies the nature of the impact.
# Transform the explanation to a dataframe and sort by saliency
exp_df = lime_explanation['output-0'].sort_values(by="Saliency")
4. Extract feature names and their saliencies
y_axis = list(exp_df['Saliency'])
x_axis = ["Annual Income", "Number of Open Accounts", "Late Payments", "Debt-to-Income Ratio", "Credit Inquiries"]
# Color-coding bars
colors = ["green" if value == max(y_axis) else "blue" if value > 0 else "red" for value in y_axis]
# Plotting
fig, ax = plt.subplots()
ax.set_facecolor("#f2f2f2")
ax.bar(x_axis, y_axis, color=colors)
plt.title('LIME: Feature Impact on Loan Approval Decision')
plt.xticks(rotation=45, ha='right')
plt.axhline(0, color="black") # x-axis line
plt.show()
And there you go!
Like the blog? Do hit a Like, send me some unicorns, and don't forget to share it with your friends!
Thank you!
Top comments (2)
Super cool stuff, Sohini!
Appreciate you sharing this series on TrustyAI... it seems like a really cool app you're building. 🙌
Hey hey! Thanks so much Michael :)