Build a Machine Learning simulation tool with Dash | by Pierre-Louis Bescond | Towards Data Science

You have done it! It all started with a business issue raised by your colleagues and you went through the dark valleys of data consolidation, cleansing, feature engineering, and modeling. Your model’s robustness is checked and you would like to share your findings.
However, not everyone is familiar with RMSE, Confusion Matrix or Shapey values...
As an example, operational teams in Industry are facing daily challenges to optimize production or supply-chain and Machine Learning is offering a new way to understand complex processes behaviors... as long as it can be translated into an intelligible analysis. And when comes the time to share some insights brought by A.I., a Python Notebook is probably not the best choice!
More than the model predicting power itself, teams are always keen to understand what the most important factors are among the numerous production parameters involved and how each of them might influence the model’s behavior.
The concept is pretty simple:
You create a .py script where your “classic” machine learning tasks are combined with a coded web-layout design, close from HTML code.
When executed, the python script generates a dynamic web page on a local address (http://127.0.0.1:8050/) where components interact with each other.
First, we need to design a “look-a-like” industrial use-case! “make_regression” from Scikit-learn will generate our data source. We will fine-tune some of the features to get realistic figures in the example:
Once the “Machine Learning” tasks are done, we still need to prepare the dynamic information to be displayed on our web browser. For this example, we will create:
A bar chart showing the model features’ importance
Three sliders allowing the user to change the values of the three most important features and understand their impact on the model prediction
You might have noticed that the bar chart creation is similar to the one used in Plotly except that the .show() function is not called. The reason is that the chart will be rendered in the “Layout” section.
The “Layout” section (below) requires some practice for users who are not familiar with both Python and HTML.
The philosophy here is to combine HTML elements (called by htm.xxx) with Dynamic Dash Components (dcc.xxx).
We will keep it very simple for this example:
<H1>  Title<DCC> Features Importance Chart<H4>  #1 Importance Feature Name<DCC> #1 Feature slider<H4>  #2 Importance Feature Name<DCC> #2 Feature slider<H4>  #2 Importance Feature Name<DCC> #2 Feature slider<H2>  Updated predictions
Here comes the “touchiest” part of the script: the app.callback function allows you to encapsulate a standard python function so that it will interact with the web page components designed above. Its mechanism can be simplified as follows:
@app.callback(Output_on_webpage,              Input_1_from_webpage,              Input_2_from_webpage,              Input_3_from_webpage)python_function(Input_1, Input_2, Input_3):       Output = model_evaluation([Input_1, Input_2, Input_3])       return Output
You might have noticed that we considered here that only the three most important features were worth being updated. Thus, we considered the mean value of every other feature when the model is evaluating the array. This is obviously a design choice for simplification purposes.
We open the command prompt and type “python dashboard.py”
(base) PS C:\Users\...\> jupyter dashboard.py * Serving Flask app "__main__" (lazy loading) * Environment: production * Debug mode: off * Running on http://127.0.0.1:8050/ (Press CTRL+C to quit)
Now, we just have to open our favorite web-browser at http://127.0.0.1:8050/ and check the result:
Every time a slider moves, the app.callback function runs the python script to re-evaluate the prediction!
The complete script is stored here on GitHub.
As you can imagine, it becomes a very powerful tool to have non-data-scientist teams interact with the model and understand its behavior. This is also a very good platform to test the model’s accuracy: operational teams will be able to check and confirm that the model’s behavior does correspond to their field experience.
A few recommendations before we close this article:
If you are not familiar with Plotly and Dash, you should start by training yourself on standard Plotly charts (the steps are extremely well explained):
plotly.com
The online course below is really well designed and it will guide you through Python basics (Pandas / NumPy), Plotly chart syntax as well as Dash design. I hardly recommend it: