Skip to content

Instantly share code, notes, and snippets.

@sureshdontha
Forked from jameskyle/test.R
Created June 3, 2019 04:43
Show Gist options
  • Save sureshdontha/5755061c6c4e11b673edc99b1fefafb1 to your computer and use it in GitHub Desktop.
Save sureshdontha/5755061c6c4e11b673edc99b1fefafb1 to your computer and use it in GitHub Desktop.
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