Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
88 additions
and
128 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
samples/features/machine-learning-services/r/iris/0-setup schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
CREATE DATABASE sqlr; | ||
GO | ||
USE sqlr; | ||
GO | ||
/* Step 1: Setup schema */ | ||
drop table if exists iris_data, iris_models; | ||
go | ||
create table iris_data ( | ||
id int not null identity primary key | ||
, "Sepal.Length" float not null, "Sepal.Width" float not null | ||
, "Petal.Length" float not null, "Petal.Width" float not null | ||
, "Species" varchar(100) null | ||
); | ||
create table iris_models ( | ||
model_name varchar(30) not null primary key, | ||
model varbinary(max) not null, | ||
native_model varbinary(max) not null | ||
); | ||
go | ||
|
||
/* Step 2: Populate test data from iris dataset in R */ | ||
insert into iris_data | ||
("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species") | ||
execute sp_execute_external_script | ||
@language = N'R' | ||
, @script = N'iris_data <- iris;' | ||
, @input_data_1 = N'' | ||
, @output_data_1_name = N'iris_data'; | ||
go | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
samples/features/machine-learning-services/r/iris/2-R-iris-score.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
USE sqlr; | ||
GO | ||
|
||
/* Create procedure for scoring using the decision tree model */ | ||
create or alter procedure predict_iris_species (@model varchar(100)) | ||
as | ||
begin | ||
declare @rx_model varbinary(max) = (select model from iris_models where model_name = @model); | ||
-- Predict based on the specified model: | ||
exec sp_execute_external_script | ||
@language = N'R' | ||
, @script = N' | ||
# Unserialize model from SQL Server | ||
irismodel<-unserialize(rx_model); | ||
# Predict species for new data using rxDTree model | ||
OutputDataSet <-rxPredict(irismodel, iris_rx_data, extraVarsToWrite = c("Species", "id")); | ||
' | ||
, @input_data_1 = N' | ||
select id, "Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species" | ||
from iris_data' | ||
, @input_data_1_name = N'iris_rx_data' | ||
, @params = N'@rx_model varbinary(max)' | ||
, @rx_model = @rx_model | ||
with result sets ( ("setosa_Pred" float, "versicolor_Pred" float, "virginica_Pred" float, "Species.Actual" varchar(100), "id" int)); | ||
end; | ||
go | ||
|
||
/* Test scoring of model */ | ||
exec predict_iris_species 'iris.dtree'; | ||
go |
22 changes: 22 additions & 0 deletions
22
samples/features/machine-learning-services/r/iris/3-iris-native-scoring.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Use sqlr; | ||
GO | ||
|
||
create or alter procedure native_predict_iris_species | ||
as | ||
begin | ||
declare @native_model varbinary(max) = (select native_model from iris_models where model_name = 'iris.dtree'); | ||
-- Predict using native scoring on the specified model: | ||
/*Generate predictions using PREDICT function */ | ||
|
||
select p.*, d.Species as "Species.Actual", d.id | ||
from PREDICT(MODEL = @native_model, DATA = dbo.iris_data as d) | ||
with(setosa_Pred float, versicolor_Pred float, virginica_Pred float) as p; | ||
|
||
end; | ||
go | ||
|
||
EXECUTE native_predict_iris_species; | ||
|
||
|
||
|
||
|
94 changes: 0 additions & 94 deletions
94
samples/features/machine-learning-services/r/iris/iris.sql
This file was deleted.
Oops, something went wrong.