forked from CODAIT/r4ml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr4ml.demo.step.lm.R
92 lines (75 loc) · 2.88 KB
/
r4ml.demo.step.lm.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
81
82
83
84
85
86
87
88
89
90
91
92
#
# (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 <- 1000
df_max_size <- 100000
# read in data
airline_hf <- as.r4ml.frame(airline)
# remove CancellationCode from dataset
names <- colnames(airline_hf)
to_remove <- c("CancellationCode", "FlightNum", "TailNum", "Cancelled", "Diverted", "DepTime", "CRSDepTime", "CarrierDelay",
"CRSArrTime", "ArrDelay", "DepDelay", "SecurityDelay", "LateAircraftDelay", "NASDelay", "WeatherDelay",
"LateAircraftDelay", "WeatherDelay", "Origin", "Dest", "ArrTime")
selected_columns <- names[!(names %in% to_remove)]
airline_hf <- as.r4ml.frame(select(airline_hf, selected_columns))
# limit the number of rows so that we can control the size
airline_hf <- limit(airline_hf, df_max_size)
airline_hf <- cache(airline_hf)
#convert to r4ml frame
airline_hf <- as.r4ml.frame(airline_hf)
# do the preprocessing of the data set
airline_transform <- r4ml.ml.preprocess(
airline_hf,
transformPath = "/tmp",
recodeAttrs = c("Month", "DayOfWeek"),
omit.na = c("ActualElapsedTime"),
dummycodeAttrs = c("UniqueCarrier") # one hot encoding
)
# sample dataset into train and test
# actual data
sampled_data <- r4ml.sample(airline_transform$data, perc = c(0.7, 0.3))
# metadata to look at the decode value and other attributes
metadata <- airline_transform$metadata
train <- as.r4ml.matrix(sampled_data[[1]])
test <- as.r4ml.matrix(sampled_data[[2]])
ignore <- cache(train)
ignore <- cache(test)
# change coltypes to scale
ml.coltypes(train) <- rep("scale", ncol(train))
# train the step.lm model
step_lm <- r4ml.step.lm(formula = ActualElapsedTime ~ .,
data = train,
intercept = FALSE,
shiftAndRescale = FALSE,
threshold = 0.001,
directory = "~/"
)
coef(step_lm)
# run the prediction
test <- as.r4ml.matrix(test)
pred <- predict(step_lm, test)
# To print all outputs, just call pred
head(pred$predictions)
pred$statistics
r4ml.session.stop()
quit("no")