Skip to content

Commit 48dd84f

Browse files
Irid demo scripts updated
1 parent f7e8e83 commit 48dd84f

File tree

5 files changed

+88
-128
lines changed

5 files changed

+88
-128
lines changed
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: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,7 @@
1-
Use sqlr;
1+
USE sqlr;
22
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
283

29-
/* Step 3: Create procedure for training model */
4+
/* Step 1: Create procedure for training model */
305
create or alter procedure generate_iris_model
316
(@trained_model varbinary(max) OUTPUT, @native_trained_model varbinary(max) OUTPUT)
327
as
@@ -55,7 +30,7 @@ from iris_data'
5530
end;
5631
go
5732

58-
/* Step 3: Train & store a decision tree model that will predict species of flowers */
33+
/* Step 2: Train & store a decision tree model that will predict species of flowers */
5934
declare @model varbinary(max), @native_model varbinary(max);
6035
exec generate_iris_model @model OUTPUT, @native_model OUTPUT;
6136
delete from iris_models where model_name = 'iris.dtree';
@@ -64,10 +39,4 @@ select model_name, datalength(model)/1024. as model_size_kb, datalength(native_m
6439
from iris_models;
6540
go
6641

67-
/* Step 4: Generate predictions using PREDICT function */
68-
declare @native_model varbinary(max) =
69-
(select native_model from iris_models where model_name = 'iris.dtree');
70-
select p.*, d.Species as "Species.Actual", d.id
71-
from PREDICT(MODEL = @native_model, DATA = dbo.iris_data as d)
72-
with(setosa_Pred float, versicolor_Pred float, virginica_Pred float) as p;
7342

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+

samples/features/machine-learning-services/r/iris/iris.sql

Lines changed: 0 additions & 94 deletions
This file was deleted.

0 commit comments

Comments
 (0)