Skip to content

Instantly share code, notes, and snippets.

@yabyzq
Last active October 28, 2016 14:00
Show Gist options
  • Save yabyzq/4a57c9f5a2f6dc4ea31fb6b05271e965 to your computer and use it in GitHub Desktop.
Save yabyzq/4a57c9f5a2f6dc4ea31fb6b05271e965 to your computer and use it in GitHub Desktop.
library(rpart)
#Generate binary Target
iris$isSetosa <- "N"
iris[iris$Species == "setosa",]$isSetosa <- "Y"
iris$isSetosa <- as.factor((iris$isSetosa))
levels(iris$isSetosa) <- c("N","Y")
head(iris)
#Create function
rpart_1F_precision <- function(feature_name, train, target_name, test, test_top = 50){
print(feature_name)
formula <- paste(target_name,"~", feature_name)
fit <- rpart(formula, train, method = "class")
prediction <- predict(fit, newdata=test, type = "prob")
result <- data.frame(target = test[,target_name], prediction = prediction[,2])
result.top <- result[order(-result$prediction),][1:test_top,]
sum(result.top[,1]=="Y")/nrow(result.top)
}
#Use the function
iris <- iris[sample(nrow(iris)),]#shuffle first
head(iris)
sapply(names(iris[1:5]),rpart_1F_precision, train = iris[1:75,], target_name = "isSetosa",
test = iris[76:150,], test_top = 25)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment