| title | Lesson 6 Operationalize the R model| Microsoft Docs |
|---|---|
| ms.prod | sql |
| ms.technology | machine-learning |
| ms.date | 04/15/2018 |
| ms.topic | tutorial |
| author | HeidiSteen |
| ms.author | heidist |
| manager | cgronlun |
[!INCLUDEappliesto-ss-xxxx-xxxx-xxx-md-winonly]
This article is part of a tutorial for SQL developers on how to use R in SQL Server.
In this step, you learn to operationalize the model using a stored procedure. This stored procedure can be called directly by other applications, to make predictions on new observations. The walkthrough demonstrates two ways to perform scoring using an R model in a stored procedure:
-
Batch scoring mode: Use a SELECT query as an input to the stored procedure. The stored procedure returns a table of observations corresponding to the input cases.
-
Individual scoring mode: Pass a set of individual parameter values as input. The stored procedure returns a single row or value.
First, let's see how scoring works in general.
The stored procedure PredictTip illustrates the basic syntax for wrapping a prediction call in a stored procedure.
CREATE PROCEDURE [dbo].[PredictTip] @inquery nvarchar(max)
AS
BEGIN
DECLARE @lmodel2 varbinary(max) = (SELECT TOP 1 model FROM nyc_taxi_models);
EXEC sp_execute_external_script @language = N'R',
@script = N'
mod <- unserialize(as.raw(model));
print(summary(mod))
OutputDataSet<-rxPredict(modelObject = mod, data = InputDataSet, outData = NULL, predVarNames = "Score", type = "response", writeModelVars = FALSE, overwrite = TRUE);
str(OutputDataSet)
print(OutputDataSet)
',
@input_data_1 = @inquery,
@params = N'@model varbinary(max)',
@model = @lmodel2
WITH RESULT SETS ((Score float));
END
GO-
The SELECT statement gets the serialized model from the database, and stores the model in the R variable
modfor further processing using R. -
The new cases for scoring are obtained from the [!INCLUDEtsql] query specified in
@inquery, the first parameter to the stored procedure. As the query data is read, the rows are saved in the default data frame,InputDataSet. This data frame is passed to therxPredictfunction in R, which generates the scores.OutputDataSet<-rxPredict(modelObject = mod, data = InputDataSet, outData = NULL, predVarNames = "Score", type = "response", writeModelVars = FALSE, overwrite = TRUE);Because a data.frame can contain a single row, you can use the same code for batch or single scoring.
-
The value returned by the
rxPredictfunction is a float that represents the probability that the driver gets a tip of any amount.
Now let's see how batch scoring works.
-
Let's start by getting a smaller set of input data to work with. This query creates a "top 10" list of trips with passenger count and other features needed to make a prediction.
SELECT TOP 10 a.passenger_count AS passenger_count, a.trip_time_in_secs AS trip_time_in_secs, a.trip_distance AS trip_distance, a.dropoff_datetime AS dropoff_datetime, dbo.fnCalculateDistance(pickup_latitude, pickup_longitude, dropoff_latitude,dropoff_longitude) AS direct_distance FROM (SELECT medallion, hack_license, pickup_datetime, passenger_count,trip_time_in_secs,trip_distance, dropoff_datetime, pickup_latitude, pickup_longitude, dropoff_latitude, dropoff_longitude FROM nyctaxi_sample)a LEFT OUTER JOIN (SELECT medallion, hack_license, pickup_datetime FROM nyctaxi_sample TABLESAMPLE (70 percent) REPEATABLE (98052) )b ON a.medallion=b.medallion AND a.hack_license=b.hack_license AND a.pickup_datetime=b.pickup_datetime WHERE b.medallion IS NULL
Sample results
passenger_count trip_time_in_secs trip_distance dropoff_datetime direct_distance 1 283 0.7 2013-03-27 14:54:50.000 0.5427964547 1 289 0.7 2013-02-24 12:55:29.000 0.3797099614 1 214 0.7 2013-06-26 13:28:10.000 0.6970098661This query can be used as input to the stored procedure, PredictTipBatchMode, provided as part of the download.
-
Take a minute to review the code of the stored procedure PredictTipBatchMode in [!INCLUDEssManStudio].
/****** Object: StoredProcedure [dbo].[PredictTipBatchMode] ******/ CREATE PROCEDURE [dbo].[PredictTipBatchMode] @inquery nvarchar(max) AS BEGIN DECLARE @lmodel2 varbinary(max) = (SELECT TOP 1 model FROM nyc_taxi_models); EXEC sp_execute_external_script @language = N'R', @script = N' mod <- unserialize(as.raw(model)); print(summary(mod)) OutputDataSet<-rxPredict(modelObject = mod, data = InputDataSet, outData = NULL, predVarNames = "Score", type = "response", writeModelVars = FALSE, overwrite = TRUE); str(OutputDataSet) print(OutputDataSet) ', @input_data_1 = @inquery, @params = N'@model varbinary(max)', @model = @lmodel2 WITH RESULT SETS ((Score float)); END
-
Provide the query text in a variable and pass it as a parameter to the stored procedure:
-- Define the input data DECLARE @query_string nvarchar(max) SET @query_string='SELECT TOP 10 a.passenger_count as passenger_count, a.trip_time_in_secs AS trip_time_in_secs, a.trip_distance AS trip_distance, a.dropoff_datetime AS dropoff_datetime, dbo.fnCalculateDistance(pickup_latitude, pickup_longitude, dropoff_latitude,dropoff_longitude) AS direct_distance FROM (SELECT medallion, hack_license, pickup_datetime, passenger_count,trip_time_in_secs,trip_distance, dropoff_datetime, pickup_latitude, pickup_longitude, dropoff_latitude, dropoff_longitude FROM nyctaxi_sample )a LEFT OUTER JOIN (SELECT medallion, hack_license, pickup_datetime FROM nyctaxi_sample TABLESAMPLE (70 percent) REPEATABLE (98052))b ON a.medallion=b.medallion AND a.hack_license=b.hack_license AND a.pickup_datetime=b.pickup_datetime WHERE b.medallion is null' -- Call the stored procedure for scoring and pass the input data EXEC [dbo].[PredictTip] @inquery = @query_string;
-
The stored procedure returns a series of values representing the prediction for each of the top 10 trips. However, the top trips are also single-passenger trips with a relatively short trip distance, for which the driver is unlikely to get a tip.
Tip
Rather than returning just the "yes-tip" and "no-tip" results, you could also return the probability score for the prediction, and then apply a WHERE clause to the Score column values to categorize the score as "likely to tip" or "unlikely to tip", using a threshold value such as 0.5 or 0.7. This step is not included in the stored procedure but it would be easy to implement.
Sometimes you want to pass in individual values from an application and get a single result based on those values. For example, you could set up an Excel worksheet, web application, or Reporting Services report to call the stored procedure and provide inputs typed or selected by users.
In this section, you learn how to create single predictions using a stored procedure.
-
Take a minute to review the code of the stored procedure PredictTipSingleMode, which was included as part of the download.
CREATE PROCEDURE [dbo].[PredictTipSingleMode] @passenger_count int = 0, @trip_distance float = 0, @trip_time_in_secs int = 0, @pickup_latitude float = 0, @pickup_longitude float = 0, @dropoff_latitude float = 0, @dropoff_longitude float = 0 AS BEGIN DECLARE @inquery nvarchar(max) = N'SELECT * FROM [dbo].[fnEngineerFeatures](@passenger_count, @trip_distance, @trip_time_in_secs, @pickup_latitude, @pickup_longitude, @dropoff_latitude, @dropoff_longitude)'; DECLARE @lmodel2 varbinary(max) = (SELECT TOP 1 model FROM nyc_taxi_models); EXEC sp_execute_external_script @language = N'R', @script = N' mod <- unserialize(as.raw(model)); print(summary(mod)); OutputDataSet<-rxPredict(modelObject = mod, data = InputDataSet, outData = NULL, predVarNames = "Score", type = "response", writeModelVars = FALSE, overwrite = TRUE); str(OutputDataSet); print(OutputDataSet); ', @input_data_1 = @inquery, @params = N'@model varbinary(max),@passenger_count int,@trip_distance float,@trip_time_in_secs int , @pickup_latitude float ,@pickup_longitude float ,@dropoff_latitude float ,@dropoff_longitude float', @model = @lmodel2, @passenger_count =@passenger_count, @trip_distance=@trip_distance, @trip_time_in_secs=@trip_time_in_secs, @pickup_latitude=@pickup_latitude, @pickup_longitude=@pickup_longitude, @dropoff_latitude=@dropoff_latitude, @dropoff_longitude=@dropoff_longitude WITH RESULT SETS ((Score float)); END
-
This stored procedure takes multiple single values as input, such as passenger count, trip distance, and so forth.
If you call the stored procedure from an external application, make sure that the data matches the requirements of the R model. This might include ensuring that the input data can be cast or converted to an R data type, or validating data type and data length.
-
The stored procedure creates a score based on the stored R model.
-
-
Try it out, by providing the values manually.
Open a new Query window, and call the stored procedure, providing values for each of the parameters. The parameters represent feature columns used by the model and are required.
EXEC [dbo].[PredictTipSingleMode] @passenger_count = 0, @trip_distance = 2.5, @trip_time_in_secs = 631, @pickup_latitude = 40.763958, @pickup_longitude = -73.973373, @dropoff_latitude = 40.782139, @dropoff_longitude = 73.977303Or, use this shorter form supported for parameters to a stored procedure:
EXEC [dbo].[PredictTipSingleMode] 1, 2.5, 631, 40.763958,-73.973373, 40.782139,-73.977303
-
The results indicate that the probability of getting a tip is low on these top 10 trips, since all are single-passenger trips over a relatively short distance.
This concludes the tutorial. Now that you have learned to embed R code in stored procedures, you can extend these practices to build models of your own. The integration with [!INCLUDEtsql] makes it much easier to deploy R models for prediction and to incorporate model retraining as part of an enterprise data workflow.