Last active
June 3, 2019 04:43
-
-
Save jameskyle/729945f6fa38a343b8ab to your computer and use it in GitHub Desktop.
This file contains 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
library(caret) | |
set.seed(300) | |
wine.r <- read.csv('https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv', sep=';') | |
wine.w <- read.csv('https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-white.csv', sep=';') | |
wine.r$style <- "red" | |
wine.w$style <- "white" | |
wine <- rbind(wine.r, wine.w) | |
wine$style <- as.factor(wine$style) | |
formula <- as.formula(quality ~ .) | |
dummies <- dummyVars(formula, data = wine) | |
dummied <- data.frame(predict(dummies, newdata = wine)) | |
dummied$quality <- wine$quality | |
wine <- dummied | |
numCols <- !colnames(wine) %in% c('quality', 'style.red', 'style.white') | |
low <- wine$quality <= 6 | |
high <- wine$quality > 6 | |
wine$quality[low] = "low" | |
wine$quality[high] = "high" | |
wine$quality <- as.factor(wine$quality) | |
indxTrain <- createDataPartition(y = wine[, names(wine) == "quality"], p = 0.7, list = F) | |
train <- wine[indxTrain,] | |
test <- wine[-indxTrain,] | |
corrMat <- cor(train[, numCols]) | |
correlated <- findCorrelation(corrMat, cutoff = 0.6) | |
ctrl <- trainControl( | |
method="repeatedcv", | |
repeats=5, | |
number=10, | |
classProbs = T | |
) | |
t1 <- train[, -correlated] | |
grid <- expand.grid(.k = c(1:20)) | |
knnModel <- train(formula, | |
data = t1, | |
method = 'knn', | |
trControl = ctrl, | |
tuneGrid = grid, | |
preProcess = 'range' | |
) | |
t2 <- test[, -correlated] | |
knnPred <- predict(knnModel, newdata = t2) | |
# How do I render the decision boundary? | |
# Make knnPred numeric | |
knnPredNumeric <- as.numeric(sample(knnPred, 100)) | |
# choose X, Y axis -> We'll plot by Alcohol & Total Sulfur Dioxide features, | |
al <- seq(min(test$alcohol), max(test$alcohol), by=0.1) | |
chl <- seq(min(test$chlorides), max(test$chlorides), by=.001) | |
probs <- matrix(knnPredNumeric, length(al), length(chl)) | |
contour(al, chl, probs, labels="", xlab="", ylab="", main="X-Nearest Neighbor", axes=F) | |
gd <- expand.grid(x=al, y=chl) | |
points(gd, pch=".", cex=5, col=ifelse(probs==1, "coral", "cornflowerblue")) | |
box() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment