title | description | author | keywords | ms.service | services | ms.topic | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|
Weather forecast using Machine Learning Studio (classic) with IoT Hub data |
Use ML Studio (classic) to predict the chance of rain based on the temperature and humidity data your IoT hub collects from a sensor. |
kgremban |
weather forecast machine learning |
iot-hub |
iot-hub |
conceptual |
10/26/2021 |
kgremban |
[!INCLUDE iot-hub-get-started-note]
Machine learning is a technique of data science that helps computers learn from existing data to forecast future behaviors, outcomes, and trends. ML Studio (classic) is a cloud predictive analytics service that makes it possible to quickly create and deploy predictive models as analytics solutions. In this article, you learn how to use ML Studio (classic) to do weather forecasting (chance of rain) using the temperature and humidity data from your Azure IoT hub. The chance of rain is the output of a prepared weather prediction model. The model is built upon historic data to forecast chance of rain based on temperature and humidity.
[!INCLUDE ML Studio (classic) retirement]
- Complete the Raspberry Pi online simulator tutorial or one of the device tutorials. For example, you can go to Raspberry Pi with Node.js or to one of the Send telemetry quickstarts. These articles cover the following requirements:
- An active Azure subscription.
- An Azure IoT hub under your subscription.
- A client application that sends messages to your Azure IoT hub.
- An ML Studio (classic) account.
- An Azure Storage account, A General-purpose v2 account is preferred, but any Azure Storage account that supports Azure Blob storage will also work.
Note
This article uses Azure Stream Analytics and several other paid services. Extra charges are incurred in Azure Stream Analytics when data must be transferred across Azure regions. For this reason, it would be good to ensure that your Resource Group, IoT Hub, and Azure Storage account -- as well as the Machine Learning Studio (classic) workspace and Azure Stream Analytics Job added later in this tutorial -- are all located in the same Azure region. You can check regional support for ML Studio (classic) and other Azure services on the Azure product availability by region page.
In this section you get the weather prediction model from the Azure AI Library. Then you add an R-script module to the model to clean the temperature and humidity data. Lastly, you deploy the model as a predictive web service.
In this section you get the weather prediction model from the Azure AI Gallery and open it in ML Studio (classic).
-
Go to the weather prediction model page.
-
Select Open in Studio (classic) to open the model in Microsoft ML Studio (classic). Select a region near your IoT hub and the correct workspace in the Copy experiment from Gallery pop-up.
For the model to behave correctly, the temperature and humidity data must be convertible to numeric data. In this section, you add an R-script module to the weather prediction model that removes any rows that have data values for temperature or humidity that cannot be converted to numeric values.
-
On the left-side of the ML Studio (classic) window, select the arrow to expand the tools panel. Enter "Execute" into the search box. Select the Execute R Script module.
-
Drag the Execute R Script module near the Clean Missing Data module and the existing Execute R Script module on the diagram. Delete the connection between the Clean Missing Data and the Execute R Script modules and then connect the inputs and outputs of the new module as shown.
-
Select the new Execute R Script module to open its properties window. Copy and paste the following code into the R Script box.
# Map 1-based optional input ports to variables data <- maml.mapInputPort(1) # class: data.frame data$temperature <- as.numeric(as.character(data$temperature)) data$humidity <- as.numeric(as.character(data$humidity)) completedata <- data[complete.cases(data), ] maml.mapOutputPort('completedata')
When you're finished, the properties window should look similar to the following:
In this section, you validate the model, set up a predictive web service based on the model, and then deploy the web service.
-
Select Run to validate the steps in the model. This step might take a few minutes to complete.
-
Select SET UP WEB SERVICE > Predictive Web Service. The predictive experiment diagram opens.
-
In the predictive experiment diagram, delete the connection between the Web service input module and the Select Columns in Dataset at the top. Then drag the Web service input module somewhere near the Score Model module and connect it as shown:
-
Select RUN to validate the steps in the model.
-
Select DEPLOY WEB SERVICE to deploy the model as a web service.
-
On the dashboard of the model, download the Excel 2010 or earlier workbook for REQUEST/RESPONSE.
[!Note] Make sure that you download the Excel 2010 or earlier workbook even if you are running a later version of Excel on your computer.
-
Open the Excel workbook, make a note of the WEB SERVICE URL and ACCESS KEY.
[!INCLUDE iot-hub-get-started-create-consumer-group]
-
In the Azure portal, select Create a resource. Type "stream analytics job" in the Search box, and select Stream Analytics job from the results dropdown. When the Stream Analytics job pane opens, select Create.
-
Enter the following information for the job.
Job name: The name of the job. The name must be globally unique.
Subscription: Select your subscription if it is different than the default.
Resource group: Use the same resource group that your IoT hub uses.
Location: Use the same location as your resource group.
Leave all other fields at their default.
-
Select Create.
-
Open the Stream Analytics job.
-
Under Job topology, select Inputs.
-
In the Inputs pane, select Add stream input, and then select IoT Hub from the dropdown. On the New input pane, choose the Select IoT Hub from your subscriptions and enter the following information:
Input alias: The unique alias for the input.
Subscription: Select your subscription if it is different than the default.
IoT Hub: Select the IoT hub from your subscription.
Shared access policy name: Select service. (You can also use iothubowner.)
Consumer group: Select the consumer group you created.
Leave all other fields at their default.
-
Select Save.
-
Under Job topology, select Outputs.
-
In the Outputs pane, select Add, and then select Blob storage/Data Lake Storage from the dropdown. On the New output pane, choose the Select storage from your subscriptions and enter the following information:
Output alias: The unique alias for the output.
Subscription: Select your subscription if it is different than the default.
Storage account: The storage account for your blob storage. You can create a storage account or use an existing one.
Container: The container where the blob is saved. You can create a container or use an existing one.
Event serialization format: Select CSV.
-
Select Save.
-
Under Job Topology, select Functions.
-
In the Functions pane, select Add, and then select Azure ML Studio from the dropdown. (Make sure you select Azure ML Studio, not Azure ML Service.) On the New function pane, choose the Provide Azure Machine Learning function settings manually and enter the following information:
Function Alias: Enter
machinelearning
.URL: Enter the WEB SERVICE URL that you noted down from the Excel workbook.
Key: Enter the ACCESS KEY that you noted down from the Excel workbook.
-
Select Save.
-
Under Job topology, select Query.
-
Replace the existing code with the following code:
WITH machinelearning AS ( SELECT EventEnqueuedUtcTime, temperature, humidity, machinelearning(temperature, humidity) as result from [YourInputAlias] ) Select System.Timestamp time, CAST (result.[temperature] AS FLOAT) AS temperature, CAST (result.[humidity] AS FLOAT) AS humidity, CAST (result.[scored probabilities] AS FLOAT ) AS 'probabalities of rain' Into [YourOutputAlias] From machinelearning
Replace
[YourInputAlias]
with the input alias of the job.Replace
[YourOutputAlias]
with the output alias of the job. -
Select Save query.
Note
If you select Test query, you'll be presented with the following message: Query testing with Machine Learning functions is not supported. Please modify the query and try again. You can safely ignore this message and select OK to close the message box. Make sure you save the query before proceeding to the next section.
In the Stream Analytics job, select Overview on the left pane. Then select Start > Now > Start. Once the job successfully starts, the job status changes from Stopped to Running.
Run the client application to start collecting and sending temperature and humidity data to your IoT hub. For each message that your IoT hub receives, the Stream Analytics job calls the weather forecast web service to produce the chance of rain. The result is then saved to your Azure blob storage. Azure Storage Explorer is a tool that you can use to view the result.
-
Open Azure Storage Explorer.
-
Sign in to your Azure account.
-
Select your subscription.
-
Select your subscription > Storage Accounts > your storage account > Blob Containers > your container.
-
Download a .csv file to see the result. The last column records the chance of rain.
You’ve successfully used ML Studio (classic) to produce the chance of rain based on the temperature and humidity data that your IoT hub receives.
[!INCLUDE iot-hub-get-started-next-steps]