In the code below, this part doesnt work ... any ideas ?
# Create a bar plot of SHAP values
summary_plot = shap.summary_plot(shap_values, df_normalized, plot_type='bar', show=False)
# Save the SHAP plot as an image
plot_file = '/home/patriciadubray/api/shap_plot.png'
shap.save_html(plot_file, summary_plot)
The whole code is here :
import joblib
import pandas as pd
import shap
import os
from flask import Flask, jsonify, request
os.environ["MKL_NUM_THREADS"] = "1"
os.environ["NUMEXPR_NUM_THREADS"] = "1"
os.environ["OMP_NUM_THREADS"] = "1"
app = Flask(__name__)
# Load the model
model = joblib.load('/home/patriciadubray/api/model.pkl')
# Load the MinMaxScaler from mms.pkl
mms = joblib.load('/home/patriciadubray/api/mms.pkl')
# Load the explainer SHAP from shap.pkl
explainer = joblib.load('/home/patriciadubray/api/shap.pkl')
# Define the route for making predictions
@app.route('/predict', methods=['POST'])
def predict():
# Retrieve the data sent as JSON
data = request.get_json()
# Create a DataFrame from the data
df = pd.DataFrame([data], index=[0])
# Normalize the input data using MinMaxScaler
df_normalized = pd.DataFrame(mms.transform(df), columns=df.columns)
# Make predictions
proba = model.predict_proba(df_normalized)
class_0_proba = proba[0][0]
class_1_proba = proba[0][1]
class_0_percentage = round(class_0_proba * 100, 1)
class_1_percentage = round(class_1_proba * 100, 1)
class_0 = f"Classe 0 : {class_0_percentage}% de probabilité que le remboursement soit ok"
class_1 = f"Classe 1 : {class_1_percentage}% de probabilité d'avoir des problèmes de remboursement"
# Calculate SHAP values for the observation
shap_values = explainer.shap_values(df_normalized)
# Create a bar plot of SHAP values
summary_plot = shap.summary_plot(shap_values, df_normalized, plot_type='bar', show=False)
# Save the SHAP plot as an image
plot_file = '/home/patriciadubray/api/shap_plot.png'
shap.save_html(plot_file, summary_plot)
result = {
"class_0": class_0,
"class_1": class_1,
"shap_plot_path": plot_file
}
return jsonify(result)
# Route for the home page
@app.route('/')
def home():
return "API dashboard prêt à dépenser"
if __name__ == '__main__':
# Display a message when the URL is launched
print("API dashboard prêt à dépenser")