This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Split train/test with rsample | |
set.seed(100) | |
initSplit <- initial_split(X, prop = .9, | |
strata = "Y") | |
trainSet <- training(initSplit) | |
testSet <- testing(initSplit) | |
# Create 5-fold cross-validation, convert to caret class | |
set.seed(100) | |
myFolds <- vfold_cv(trainSet, v = 5, repeats = 5, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# binary vars | |
binVars <- which(sapply(X, function(x){all(x %in% 0:1)})) | |
missingVars <- which(apply(X, 2, function(k){any(is.na(k))})) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# binary vars | |
binVars <- which(sapply(X, function(x){all(x %in% 0:1)})) | |
missingVars <- which(apply(X, 2, function(k){any(is.na(k))})) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Design recipe | |
myRec <- recipe(Y ~ ., data = trainSet) %>% | |
step_YeoJohnson(all_predictors(), -binVars) %>% | |
step_center(all_predictors(), -binVars) %>% | |
step_scale(all_predictors(), -binVars) %>% | |
step_meanimpute(missingVars) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# simple PCA, plot | |
pcaRec <- myRec %>% | |
step_pca(all_predictors()) | |
myPCA <- prep(pcaRec, training = trainSet, retain = T) %>% | |
juice() | |
colGrad <- trainSet$Y/100 # add color | |
plot(myPCA$PC1, myPCA$PC2, | |
col = rgb(1 - colGrad, 0, colGrad,.5), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Train | |
doMC::registerDoMC(10) | |
knnMod <- train(myRec, data = trainSet, | |
method = "knn", | |
tuneGrid = data.frame(k = seq(5, 25, by = 4)), | |
trControl = ctrl) | |
enetMod <- train(myRec, data = trainSet, | |
method = "glmnet", | |
tuneGrid = expand.grid(alpha = seq(0, 1, length.out = 5), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bwplot(resamples(modelList), | |
metric = "RMSE") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Validate on test set with ensemble | |
allPreds <- sapply(modelList, predict, newdata = testSet) | |
ensemblePred <- rowSums(allPreds) / length(modelList) | |
# Plot predicted vs. observed; create PNG | |
plot(ensemblePred, testSet$Y, | |
xlim = c(0,100), ylim = c(0,100), | |
xlab = "Predicted", ylab = "Observed", | |
pch = 16, col = rgb(0, 0, 0, .25)) | |
abline(a=0, b=1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##### Process image ##### | |
library(keras) | |
library(EBImage) | |
library(stringr) | |
library(pbapply) | |
secondCat <- readImage("train/cat.1.jpg") | |
display(secondCat) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set image size | |
width <- 50 | |
height <- 50 | |
extract_feature <- function(dir_path, width, height, labelsExist = T) { | |
img_size <- width * height | |
## List images in path | |
images_names <- list.files(dir_path) | |