Skip to content

Commit ac38ef5

Browse files
authored
Merge pull request #329 from NelGson/master
Adding iris demo scripts
2 parents cd9b83f + 48dd84f commit ac38ef5

4 files changed

Lines changed: 127 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
CREATE DATABASE sqlr;
3+
GO
4+
USE sqlr;
5+
GO
6+
/* Step 1: Setup schema */
7+
drop table if exists iris_data, iris_models;
8+
go
9+
create table iris_data (
10+
id int not null identity primary key
11+
, "Sepal.Length" float not null, "Sepal.Width" float not null
12+
, "Petal.Length" float not null, "Petal.Width" float not null
13+
, "Species" varchar(100) null
14+
);
15+
create table iris_models (
16+
model_name varchar(30) not null primary key,
17+
model varbinary(max) not null,
18+
native_model varbinary(max) not null
19+
);
20+
go
21+
22+
/* Step 2: Populate test data from iris dataset in R */
23+
insert into iris_data
24+
("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
25+
execute sp_execute_external_script
26+
@language = N'R'
27+
, @script = N'iris_data <- iris;'
28+
, @input_data_1 = N''
29+
, @output_data_1_name = N'iris_data';
30+
go
31+
32+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
USE sqlr;
2+
GO
3+
4+
/* Step 1: Create procedure for training model */
5+
create or alter procedure generate_iris_model
6+
(@trained_model varbinary(max) OUTPUT, @native_trained_model varbinary(max) OUTPUT)
7+
as
8+
begin
9+
execute sp_execute_external_script
10+
@language = N'R'
11+
, @script = N'
12+
# Build decision tree model to predict species based on sepal/petal attributes
13+
iris_model <- rxDTree(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, data = iris_rx_data);
14+
15+
# Serialize model to binary format for storage in SQL Server
16+
trained_model <- as.raw(serialize(iris_model, connection=NULL));
17+
18+
# Serialize model to native binary format for scoring using PREDICT function in SQL Server
19+
native_trained_model <- rxSerializeModel(iris_model, realtimeScoringOnly = TRUE)
20+
'
21+
, @input_data_1 = N'
22+
select "Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species"
23+
from iris_data'
24+
, @input_data_1_name = N'iris_rx_data'
25+
26+
, @params = N'
27+
@trained_model varbinary(max) OUTPUT, @native_trained_model varbinary(max) OUTPUT'
28+
, @trained_model = @trained_model OUTPUT
29+
, @native_trained_model = @native_trained_model OUTPUT;
30+
end;
31+
go
32+
33+
/* Step 2: Train & store a decision tree model that will predict species of flowers */
34+
declare @model varbinary(max), @native_model varbinary(max);
35+
exec generate_iris_model @model OUTPUT, @native_model OUTPUT;
36+
delete from iris_models where model_name = 'iris.dtree';
37+
insert into iris_models (model_name, model, native_model) values('iris.dtree', @model, @native_model);
38+
select model_name, datalength(model)/1024. as model_size_kb, datalength(native_model)/1024. as native_model_size_kb
39+
from iris_models;
40+
go
41+
42+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
USE sqlr;
2+
GO
3+
4+
/* Create procedure for scoring using the decision tree model */
5+
create or alter procedure predict_iris_species (@model varchar(100))
6+
as
7+
begin
8+
declare @rx_model varbinary(max) = (select model from iris_models where model_name = @model);
9+
-- Predict based on the specified model:
10+
exec sp_execute_external_script
11+
@language = N'R'
12+
, @script = N'
13+
# Unserialize model from SQL Server
14+
irismodel<-unserialize(rx_model);
15+
16+
# Predict species for new data using rxDTree model
17+
OutputDataSet <-rxPredict(irismodel, iris_rx_data, extraVarsToWrite = c("Species", "id"));
18+
'
19+
, @input_data_1 = N'
20+
select id, "Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species"
21+
from iris_data'
22+
, @input_data_1_name = N'iris_rx_data'
23+
, @params = N'@rx_model varbinary(max)'
24+
, @rx_model = @rx_model
25+
with result sets ( ("setosa_Pred" float, "versicolor_Pred" float, "virginica_Pred" float, "Species.Actual" varchar(100), "id" int));
26+
end;
27+
go
28+
29+
/* Test scoring of model */
30+
exec predict_iris_species 'iris.dtree';
31+
go
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Use sqlr;
2+
GO
3+
4+
create or alter procedure native_predict_iris_species
5+
as
6+
begin
7+
declare @native_model varbinary(max) = (select native_model from iris_models where model_name = 'iris.dtree');
8+
-- Predict using native scoring on the specified model:
9+
/*Generate predictions using PREDICT function */
10+
11+
select p.*, d.Species as "Species.Actual", d.id
12+
from PREDICT(MODEL = @native_model, DATA = dbo.iris_data as d)
13+
with(setosa_Pred float, versicolor_Pred float, virginica_Pred float) as p;
14+
15+
end;
16+
go
17+
18+
EXECUTE native_predict_iris_species;
19+
20+
21+
22+

0 commit comments

Comments
 (0)