Forums

Lime explainer return empty graph

Hi I have create ML web app for taking input from user and show result to web app.User will fill my form on web app page and then flask will use model to predicted and then model will send result to web app page.Text result working but graph doesn't. Now i want to show explainer using LIME but when i save LIME graph it return empty graph,I already checked my error log.There are no error details

model = pickle.load(open("./model/hr.pkl", "rb"))
  app = flask.Flask(__name__, template_folder='templates')


  @app.route('/', methods=['GET', 'POST'])
  def main():
if flask.request.method == 'GET':
    # Just render the initial form, to get input
    return (flask.render_template('main.html'))

if flask.request.method == 'POST':
    # Extract the input
    TotalWorkingYears = flask.request.form['TotalWorkingYears']
    OverTime_code = flask.request.form['OverTime_code']
    JobInvolvement = flask.request.form['JobInvolvement']
    JobRole_code = flask.request.form['JobRole_code']
    Age = flask.request.form['Age']
    WorkLifeBalance = flask.request.form['WorkLifeBalance']
    Gender_code = flask.request.form['Gender_code']
    DistanceFromHome = flask.request.form['DistanceFromHome']
    MaritalStatus_code = flask.request.form['MaritalStatus_code']
    YearsSinceLastPromotion = flask.request.form['YearsSinceLastPromotion']
    Education = flask.request.form['Education']
    PercentSalaryHike = flask.request.form['PercentSalaryHike']
    TrainingTimesLastYear = flask.request.form['TrainingTimesLastYear']
    JobLevel = flask.request.form['JobLevel']
    YearsAtCompany = flask.request.form['YearsAtCompany']
    DailyRate = flask.request.form['DailyRate']
    YearsWithCurrManager = flask.request.form['YearsWithCurrManager']
    MonthlyIncome = flask.request.form['MonthlyIncome']
    JobSatisfaction = flask.request.form['JobSatisfaction']
    EducationField_code = flask.request.form['EducationField_code']
    RelationshipSatisfaction = flask.request.form['RelationshipSatisfaction']
    MonthlyRate = flask.request.form['MonthlyRate']
    BusinessTravel_code = flask.request.form['BusinessTravel_code']

 # Make DataFrame for model
    input_variables = pd.DataFrame([[TotalWorkingYears, OverTime_code, JobInvolvement,JobRole_code, Age, WorkLifeBalance,
                                     Gender_code, DistanceFromHome, MaritalStatus_code, YearsSinceLastPromotion,
                                     Education,PercentSalaryHike, TrainingTimesLastYear, JobLevel, YearsAtCompany, DailyRate,
                                     YearsWithCurrManager, MonthlyIncome, JobSatisfaction, EducationField_code,
                                     RelationshipSatisfaction, MonthlyRate, BusinessTravel_code]],
                                   columns=['TotalWorkingYears', 'OverTime_code', 'JobInvolvement', 'JobRole_code',
                                            'Age','WorkLifeBalance', 'Gender_code', 'DistanceFromHome','MaritalStatus_code',
                                            'YearsSinceLastPromotion','Education','PercentSalaryHike','TrainingTimesLastYear','JobLevel',
                                            'YearsAtCompany','DailyRate','YearsWithCurrManager','MonthlyIncome','JobSatisfaction',
                                            'EducationField_code','RelationshipSatisfaction','MonthlyRate','BusinessTravel_code'],
                                   dtype=float,
                                   index=['input'])

    # Get the model's prediction
    prediction = model.predict(input_variables)[0]
    prediction_percentage = model.predict_proba(input_variables)[:,1]

    row_to_show = 1
    data_for_prediction = input_variables.iloc[1]  # use 1 row of data here. Could use multiple rows if desired
    data_for_prediction_array = data_for_prediction.values.reshape(1, -1)

    model.predict_proba(data_for_prediction_array)
    X_featurenames = input_variables.columns

    categorical_features = np.argwhere(np.array([len(set(input_variables.values[0]))]))


    predict_fn = lambda x: model.predict_proba(x).astype(float)

    explainer = lime.lime_tabular.LimeTabularExplainer(input_variables.values,
    feature_names=X_featurenames,
    class_names=['Yes','No'],
    categorical_features=categorical_features,
    verbose=True, mode='classification')

    exp = explainer.explain_instance(input_variables.values[0], predict_fn, num_features=5)
    fig = exp.as_pyplot_figure()






    if os.path.isfile("/home/Domemakarov2013/smart_hr/static/images/shap_graph/graph.svg"):
        os.remove("/home/Domemakarov2013/smart_hr/static/images/shap_graph/graph.svg")

          plt.savefig("/home/Domemakarov2013/smart_hr/static/images/shap_graph/graph.svg",

            dpi = 150,
            bbox_inches = 'tight')
       # plt.savefig('/home/Domemakarov2013/smart_hr/static/images/shap_graph/graph.svg')
    else:
       # plt.savefig('/home/Domemakarov2013/smart_hr/static/images/shap_graph/graph.svg')
          plt.savefig("/home/Domemakarov2013/smart_hr/static/images/shap_graph/graph.svg",

            dpi = 150,
            bbox_inches = 'tight')`

[edited by admin: formatting]

I see your code that saves the graph. Where is your code that shows your graph to your users?

main.html src="{{ url_for ('static' , filename='images/shap_graph/graph.svg') }}"

If you start the JavaScript console in your browser (in Chrome, go to the "Hamburger" menu, then "More tools", then "Developer tools", and then go to the page that should be showing the graph, do you get any error messages?