forked from CODAIT/r4ml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr4ml.demo.mlogit.R
80 lines (67 loc) · 2.3 KB
/
r4ml.demo.mlogit.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#
# (C) Copyright IBM Corp. 2017
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# load library R4ML
library(R4ML)
r4ml.session()
# example paths for custom dataset
# path = "/user/data-scientist/airline/1987.csv"
#path = "/user/data-scientist/airline/*1987.csv"
# df <- R4ML:::r4ml.read.csv(path, inferSchema=TRUE, header=TRUE)
# we would like to limit the dataset to a size so that we can run test faster
# df_max_size <- 100
df_max_size <- 100000
# set the predictors and response variables
predictors <- c("Month", "DayofMonth", "DayOfWeek", "Year")
response <- c("Cancelled")
D_names <- append(predictors, response)
# read in data
airline_hf = as.r4ml.frame(airline)
# limit the number of rows so that we can control the size
airline_hf <- limit(airline_hf, df_max_size)
ignore <- cache(airline_hf)
#convert to r4ml frame
airline_hf <- SparkR::select(airline_hf, D_names)
airline_hf <- as.r4ml.frame(airline_hf)
ignore <- cache(airline_hf)
# preprocessing
airline_transform <- r4ml.ml.preprocess(
airline_hf,
transformPath = "/tmp",
recodeAttrs=D_names,
#dummycodeAttrs = c("Month"),
binningAttrs = c(), numBins=4,
#missingAttrs = D_names,
omit.na = D_names
)
# sample dataset into train and test
sampled_data <- r4ml.sample(airline_transform$data, perc=c(0.7, 0.3))
# collect metadata
metadata <- airline_transform$metadata
train <- as.r4ml.matrix(sampled_data[[1]])
ignore <- cache(train)
# create logistic regression model
ml.coltypes(train) <- c(rep("scale", length(names(train))-1), "nominal")
logistic_regression = r4ml.mlogit(Cancelled ~ ., data=train)
# compute predictions on new data
test <- as.r4ml.matrix(sampled_data[[2]])
ignore <- cache(test)
pred <- predict(logistic_regression, test)
# To print all outputs, just call pred
pred$probabilities[1:10,]
pred$statistics
# exit R/R4ML
r4ml.session.stop()
quit("no")