Interactive Features¶
In this chapter, we'll discover how to add user interactivity to our application using Streamlit widgets:
- ⚡ Sidebar for organizing widgets in a collapsible panel
- ⚡ Select box for choosing categorical features
- ⚡ Slider for adjusting numerical values
- ⚡ Radio buttons for making single-choice selections
- ⚡ Checkbox for toggle options
These widgets will allow users to dynamically modify model feature values, which will then update our model's predictions in real-time.
Build a Sidebar¶
Streamlit's sidebar provides a convenient way for users to interactively filter and select input features for our machine learning model, enhancing the app's functionality and user experience. To implement this, let's update the $TUTORIAL_HOME/streamlit_app.py
file with the following code:
Feature Data Preprocessing¶
We will now filter the data
DataFrame based on the selected features and use pd.concat
, ensuring the new data undergoes the same preprocessing steps as our training data.
Edit and update the $TUTORIAL_HOME/streamlit_app.py
with the following code,
streamlit_app.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
|
Now that we've prepared our input data, let's handle the categorical variables using encoding techniques. Feature encoding is crucial for converting categorical data into a format suitable for machine learning models.
Helpful resources for encoding:
- Scikit-learn encoding guide: https://scikit-learn.org/stable/modules/preprocessing.html#encoding-categorical-features
- Pandas
get_dummies
documentation: https://pandas.pydata.org/docs/reference/api/pandas.get_dummies.html