-
Notifications
You must be signed in to change notification settings - Fork 3
/
create_model.r
64 lines (47 loc) · 1.4 KB
/
create_model.r
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
62
63
64
# IMPORTANT: set the working directory to the location of this script
#setwd("<add your script directory here>")
library(jsonlite)
### Get and Analyze Data
set.seed(123)
x <- 0:49
y <- 5 * x + 2 + rnorm(50, sd = 20)
plot(x, y, col=adjustcolor(col="darkblue", alpha.f=0.3), pch=20)
### Train Model
model <- lm(y ~ x)
### Test Model
abline(model, col="red")
### Azure Machine Learning Services-related
# define an init() function
# note: we'll let the score.py script call this function when the prediction service is
# initialized
init <- function() {
}
# define a run() function for AMLS
# note: we'll let the score.py script call this function when the prediction service is used
run <- function(inputJsonString) {
# load required libraries
library(jsonlite)
# get input
inputJson <- fromJSON(inputJsonString)
x <- inputJson$x
# apply model
y <- as.numeric(predict(model, data.frame(x = x)))
# return result
outputJsonString <- paste0('{"y": ', as.character(y), '}')
return(outputJsonString)
}
# Save model (workspace)
# note: depending on the objects in your workspace, it might make sense
# to cleanup unused variables first using rm(). alternatively,
# saving only the required variables.
#
save.image("model.RData")
### Testing
# clean workspace
rm(list=ls())
# load model
load("model.RData")
# invoke init() method
init()
# invoke run() method
cat(run('{"x": 123.45}'))