|
| 1 | +USE sqlr; |
| 2 | +GO |
| 3 | +/* Step 1: Setup schema */ |
| 4 | +drop table if exists iris_data, iris_models; |
| 5 | +go |
| 6 | +create table iris_data ( |
| 7 | + id int not null identity primary key |
| 8 | + , "Sepal.Length" float not null, "Sepal.Width" float not null |
| 9 | + , "Petal.Length" float not null, "Petal.Width" float not null |
| 10 | + , "Species" varchar(100) null |
| 11 | +); |
| 12 | +create table iris_models ( |
| 13 | + model_name varchar(30) not null primary key, |
| 14 | + model varbinary(max) not null, |
| 15 | + native_model varbinary(max) not null |
| 16 | +); |
| 17 | +go |
| 18 | + |
| 19 | +/* Step 2: Populate test data from iris dataset in R */ |
| 20 | +insert into iris_data |
| 21 | +("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species") |
| 22 | +execute sp_execute_external_script |
| 23 | + @language = N'R' |
| 24 | + , @script = N'iris_data <- iris;' |
| 25 | + , @input_data_1 = N'' |
| 26 | + , @output_data_1_name = N'iris_data'; |
| 27 | +go |
| 28 | + |
| 29 | +/* Step 3: Create procedure for training model */ |
| 30 | +create or alter procedure generate_iris_model |
| 31 | +(@trained_model varbinary(max) OUTPUT, @native_trained_model varbinary(max) OUTPUT) |
| 32 | +as |
| 33 | +begin |
| 34 | + execute sp_execute_external_script |
| 35 | + @language = N'R' |
| 36 | + , @script = N' |
| 37 | +# Build decision tree model to predict species based on sepal/petal attributes |
| 38 | +iris_model <- rxDTree(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, data = iris_rx_data); |
| 39 | +
|
| 40 | +# Serialize model to binary format for storage in SQL Server |
| 41 | +trained_model <- as.raw(serialize(iris_model, connection=NULL)); |
| 42 | +
|
| 43 | +# Serialize model to native binary format for scoring using PREDICT function in SQL Server |
| 44 | +native_trained_model <- rxSerializeModel(iris_model, realtimeScoringOnly = TRUE) |
| 45 | +' |
| 46 | + , @input_data_1 = N' |
| 47 | +select "Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species" |
| 48 | +from iris_data' |
| 49 | + , @input_data_1_name = N'iris_rx_data' |
| 50 | + |
| 51 | + , @params = N' |
| 52 | +@trained_model varbinary(max) OUTPUT, @native_trained_model varbinary(max) OUTPUT' |
| 53 | + , @trained_model = @trained_model OUTPUT |
| 54 | + , @native_trained_model = @native_trained_model OUTPUT; |
| 55 | +end; |
| 56 | +go |
| 57 | + |
| 58 | +/* Step 3: Train & store a decision tree model that will predict species of flowers */ |
| 59 | +declare @model varbinary(max), @native_model varbinary(max); |
| 60 | +exec generate_iris_model @model OUTPUT, @native_model OUTPUT; |
| 61 | +delete from iris_models where model_name = 'iris.dtree'; |
| 62 | +insert into iris_models (model_name, model, native_model) values('iris.dtree', @model, @native_model); |
| 63 | +select model_name, datalength(model)/1024. as model_size_kb, datalength(native_model)/1024. as native_model_size_kb |
| 64 | + from iris_models; |
| 65 | +go |
| 66 | + |
| 67 | +/* Step 4: Create procedure for scoring using the decision tree model */ |
| 68 | +create or alter procedure predict_iris_species (@model varchar(100)) |
| 69 | +as |
| 70 | +begin |
| 71 | + declare @rx_model varbinary(max) = (select model from iris_models where model_name = @model); |
| 72 | + -- Predict based on the specified model: |
| 73 | + exec sp_execute_external_script |
| 74 | + @language = N'R' |
| 75 | + , @script = N' |
| 76 | +# Unserialize model from SQL Server |
| 77 | +irismodel<-unserialize(rx_model); |
| 78 | +
|
| 79 | +# Predict species for new data using rxDTree model |
| 80 | +OutputDataSet <-rxPredict(irismodel, iris_rx_data, extraVarsToWrite = c("Species", "id")); |
| 81 | + ' |
| 82 | + , @input_data_1 = N' |
| 83 | +select id, "Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species" |
| 84 | +from iris_data' |
| 85 | + , @input_data_1_name = N'iris_rx_data' |
| 86 | + , @params = N'@rx_model varbinary(max)' |
| 87 | + , @rx_model = @rx_model |
| 88 | + with result sets ( ("setosa_Pred" float, "versicolor_Pred" float, "virginica_Pred" float, "Species.Actual" varchar(100), "id" int)); |
| 89 | +end; |
| 90 | +go |
| 91 | + |
| 92 | +/* Step 5: Test scoring of model */ |
| 93 | +exec predict_iris_species 'iris.dtree'; |
| 94 | +go |
0 commit comments