This repository was archived by the owner on Jun 10, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathscore_binaryclass_model.sql
More file actions
62 lines (53 loc) · 2.43 KB
/
Copy pathscore_binaryclass_model.sql
File metadata and controls
62 lines (53 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
SET ANSI_NULLS ON
GO
/****** Create the Stored Procedure for PM Template ******/
SET QUOTED_IDENTIFIER ON
GO
drop procedure if exists score_binaryclass_model;
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [score_binaryclass_model] @modelname varchar(20),
@connectionString varchar(300)
AS
BEGIN
DECLARE @inquery NVARCHAR(max) = N'SELECT * FROM score_Features_Normalized';
declare @model varbinary(max) = (select model from [PM_Models] where model_name = @modelname);
EXEC sp_execute_external_script @language = N'R',
@script = N'
####################################################################################################
## Get score table data for prediction
####################################################################################################
prediction_df <- InputDataSet
####################################################################################################
## Binary classification prediction
####################################################################################################
model <- unserialize(model)
prediction <- rxPredict(modelObject = model,
data = prediction_df,
type = "prob",
overwrite = TRUE)
threshold <- 0.5
names(prediction) <- c("Probability")
prediction$Prediction <- ifelse(prediction$Probability > threshold, 1, 0)
####################################################################################################
## Write score results to SQL
####################################################################################################
predictions <- cbind(prediction_df$id, prediction_df$cycle_orig, prediction)
colnames(predictions)[1] <- "id"
colnames(predictions)[2] <- "cycle"
prediction_table <- RxSqlServerData(table = paste("Binaryclass_score", modelname, sep = "_"),
connectionString = connection_string)
rxDataStep(inData = predictions,
outFile = prediction_table,
overwrite = TRUE)'
, @input_data_1 = @inquery
, @params = N'@model varbinary(max), @modelname varchar(20), @connection_string varchar(300)'
, @model = @model
, @modelname = @modelname
, @connection_string = @connectionString
END
GO