-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadult-mpi.r
64 lines (48 loc) · 1.89 KB
/
adult-mpi.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
### adult-mpi.r
###
### Use k-nearest neighbors to develop a predictive model of who earns at least
### $50K/year in mid-1990s Census data
## -----------------------------------------------------------------------------
## Cluster setup
## -----------------------------------------------------------------------------
library("foreach")
library("doMPI")
options(error=quote(assign(".mpi.err", FALSE, env = .GlobalEnv)))
cl <- startMPIcluster()
registerDoMPI(cl)
## -----------------------------------------------------------------------------
## Data analysis
## -----------------------------------------------------------------------------
library("caret")
adult_data <- read.csv("adult-cleaned.csv")
## Baseline: run cross-validation sequentially
fit_seq <- train(income ~ age + race + sex + education + hours_per_week,
data = adult_data,
method = "knn",
preProcess = c("center", "scale"),
trControl = trainControl(
method = "cv",
number = 10,
allowParallel = FALSE
),
tuneLength = 5)
print(fit_seq)
print(fit_seq$times)
## Re-run in parallel
fit_par <- train(income ~ age + race + sex + education + hours_per_week,
data = adult_data,
method = "knn",
preProcess = c("center", "scale"),
trControl = trainControl(
method = "cv",
number = 10,
allowParallel = TRUE
),
tuneLength = 5)
print(fit_par)
print(fit_par$times)
## -----------------------------------------------------------------------------
## Cluster shutdown
## -----------------------------------------------------------------------------
closeCluster(cl)
mpi.quit()