Skip to content

Instantly share code, notes, and snippets.

@Gerjo
Last active December 30, 2015 00:49
Show Gist options
  • Save Gerjo/7751771 to your computer and use it in GitHub Desktop.
Save Gerjo/7751771 to your computer and use it in GitHub Desktop.
R nearest neighbour (knn) demo.
cat("\014")
library(class)
# I like boolean support.
no <- 0
yes <- 1
# Entry to clasify
test <- c(
"cat", # The correct class
4, # Number of legs
no, # has tail
yes, # has lungs
yes # has fur
)
test2 <- c(
"fish", # The correct class
0, # Number of legs
yes, # has tail
no, # has lungs
no # has fur
)
# Merge them into a matrix, we'll test multiple.
alltests <- rbind(test, test2)
# Training data set
data <- rbind(
# class legs tail lungs fur
c("human", 2, no, yes, no),
c("fish", 0, yes, no, no),
c("cat", 4, yes, yes, yes),
c("spider", 8, no, no, yes),
c("spider", 6, no, no, no), # rare spider
c("wasp", 6, no, no, yes),
c("alien", 5, no, no, yes), # alien variant 1
c("alien", 5, yes, no, no) # alien variant 2
)
# Extract columns
class <- data[, 1] # First column contains class names
train <- data[, 2:ncol(data)] # Other columns, discriptive data
# (CNN) Self test, each pattern is used as input. If it cannot predict
# itself, it is dropped. E.g., when a cat pattern cannot predict
# it's a cat, it's dropped as a rule.
keep <- condense(train, class)
# Remove entries that do not change the outcome.
keep2 <- reduce.nn(train, keep, class)
# Nearest neighbour
print("Statistically speaking, you are a:")
knn(train[keep, , drop=FALSE], alltests[,2:length(test)], class[keep], k=1, TRUE)
if(length(keep2) > 0) {
print("After reducing the training data, you are a:")
knn(train[keep2, , drop=FALSE], alltests[,2:length(test)], class[keep2], k=1, TRUE)
} else {
print("Reducing the training data resulted into an empty set:");
keep2
}
print("Information provided:")
print(alltests)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment