Part 2; The Salary Predictor

Daniel Boadzie
2 min readNov 21, 2019

--

This is the second of a series of articles showing the awesomeness of
Streamlit; a framework for deploying Data Science and Machine
Learning apps(You can read the first article here).
In this second article, we will build an app to predict the salaries of
techies. We ill start by installing our dependencies. For this app we will
need the following;

pip install sklearn
pip install streamlit

I will not go into the details of building the model. I will assume you
have built your model with learn and saved it into a pickle file. Thus,
we will focus on using Streamlit to deploy our model into a web app
people can interact with.

We will use Streamlit’s markdown method to add a header to our
app.py file.

The app.py file will now look like the following;

st.markdown(‘## App 2: Salary Predictor For Techies’)

Next we will import our model

model = pickle.load(open(‘model.pkl’, ‘rb’)) # load the model

So in the following code snippet, we created a variable called model
and using pickle, loaded our model into it.
We are next going to receive user inputs corresponding with our
features for which we will pass to our model. Fortunately, doing this in
Streamlit is a breeze.

# Take the users input
experience = st.number_input(‘Years of Experience’)
test_score = st.number_input(‘Aptitude Test score’)
interview_score = st.number_input(‘Interview Score’)
# store the inputs
features = [experience, test_score, interview_score]
# convert user inputs into an array fr the model
int_features = [int(x) for x in features]
final_features = [np.array(int_features)]

The only thing left to do now is to feed these inputs into our model.
We do this in the following code;

if st.button(‘Predict’): # when the submit button is
pressed
prediction = model.predict(final_features)
st.balloons()
st.success(f’Your Salary per anum is: Ghc {round(prediction[0], 2)}’)

In the code above, we check if a submit button is hit, then we pass our
features to the model’s predict method, added animation and printed
out our prediction in a Bootstrap panel to the user.

Now, let’s run our app to check if everything is working as expected.

streamlit run app.py # if app is the name of your file

The app should launch in our default browser at http://localhost:8501.
( The entire code for this article and many others to come can be
found at my github page)

And just like that, we have our finished Salary Predictor. How easy was
that! That is why Streamlit is the best framework for deploying Data
Science and Machine Learning apps. In the final article of this series,
we will build an Iris Flower classifier and deploy it to Heroku using
Streamlit. So stay tuned.

--

--

Daniel Boadzie
Daniel Boadzie

Written by Daniel Boadzie

Data scientist | AI Engineer |Software Engineering|Trainer|Svelte Entusiast. Find out more about my me here https://www.linkedin.com/in/boadzie/

No responses yet