Get startedGet started for free

Apply a machine learning model

1. Apply a machine learning model

The last step in building a machine learning model is to apply the model to new data to make actual predictions!

2. Model Recap

Remember that we built our model as a pipeline containing a StandardScaler and a LogisticRegression estimator. We then fitted the model to the train data, and scored the model against the test dataset.

3. Predict

We will now use the .predict() method of the pipeline to predict our test-data. Note that the DataFrame used here needs to have the same features as the training dataset, but can have a different amount of observations. The return variable is an array of the same length than the DataFrame passed in, X_test, and contains the predicted Classes for each observation.

4. Record conversation

Let's now return to the streaming example we saw before. Remember from previous lessons that one message in the MQTT topic is a json object which looks like the one you see on screen. To pass this into the machine learning model, we need to convert it into a DataFrame. This can be done by using pandas.DataFrame.from_records(), which converts a dictionary into a DataFrame. We first assign the list of columns from the train Dataset to cols, so we can reuse this variable later. We then use the data-object, converted to a list as the first argument, specify timestamp as the index, and cols as column list. This ensures that the resulting DataFrame has the columns in the correct order, which is a requirement for the model to work.

5. Apply to datastream

As before, we define the callback function on_message() with the parameters client, userdata and message. We parse the message.payload as json string into data. Next, we convert the json object into a DataFrame using pd.DataFrame.from_records() We then pass this DataFrame to the pipelines .predict() method and store the result as category. We then call the function maybe_alert(), with the first element of category as parameter. Remember that .predict() returns an array - since we passed in an array of length one, we get an array with length one back. This function contains business logic to create alerts on certain categories, but not on others. This could be alerts for bad weather conditions, or notifying a line-manager of potential quality problems in the production facility. Lastly, we connect the callback-function on_message to the MQTT topic, as we did in previous lessons. We now have a pipeline, which receives elements in real-time from an MQTT data stream, predicts the class and implements business-logic to act on the predicted classes.

6. Let's practice!

And now, it's your turn to apply the model to a data stream!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.