1. Learn
  2. /
  3. Courses
  4. /
  5. Analyzing IoT Data in Python

Exercise

Apply model to data stream

Let's now apply your trained machine learning Pipeline to streaming data, and categorize the values immediately.

You'll then use predict() on the incoming messages to determine the category. Based on the result of the prediction you will take action, and close the windows in your house (or not).

Remember that category 1 means good weather, whereas category 0 signifies bad, cold weather.

Additionally, the pipeline returns an array of predictions. As you passed in only one element, you need to access the first element using category[0].

The function close_window() will handle this for you, and will additionally log the record for further study.

pandas as pd and json have been preloaded the session for you, and the model is available as pl.

Instructions

100 XP
  • Parse the dictionary into a pandas DataFrame with DataFrame.from_records() "timestamp" as index, and cols as columns.
  • Determine the category of this record by using predict() from the pipeline object and assign the result to category.
  • Call close_window() with the DataFrame df as the first argument, and category as the 2nd argument.