here you must define a function that returns a double value to evaluate the

Here you must define a function that returns a double

This preview shows page 15 - 19 out of 19 pages.

#here you must define a function that returns a double value to evaluate the given subset #consider high values for good evaluation and low values for bad evaluation. #k-fold cross validation k <- 5 splits <- runif(nrow(iris)) results = sapply(1:k, function(i) { test.idx <- (splits >= (i - 1) / k) & (splits < i / k) train.idx <- !test.idx test <- iris[test.idx, , drop=FALSE] train <- iris[train.idx, , drop=FALSE] tree <- rpart(as.simple.formula(subset, "Species"), train) error.rate = sum(test$Species != predict(tree, test, type="c")) / nrow(test) return(1 - error.rate) }) print(as.simple.formula(subset, "Species")) # print(subset) print(mean(results)) return(mean(results)) } ## perform the best subset search subset = best.first.search(names(iris)[-5], evaluator) ## Species ~ Sepal.Length ## <environment: 0x10df22878> ## [1] 0.6907014 ## Species ~ Sepal.Width ## <environment: 0x121b29c78> ## [1] 0.4980635 ## Species ~ Petal.Length
Image of page 15
## <environment: 0x126a59aa0> ## [1] 0.9399471 ## Species ~ Petal.Width ## <environment: 0x10ea73840> ## [1] 0.9540592 ## Species ~ Sepal.Length + Petal.Width ## <environment: 0x10da94708> ## [1] 0.9580261 ## Species ~ Sepal.Width + Petal.Width ## <environment: 0x114b10838> ## [1] 0.9546349 ## Species ~ Petal.Length + Petal.Width ## <environment: 0x10be67060> ## [1] 0.9437999 ## Species ~ Sepal.Length + Sepal.Width + Petal.Width ## <environment: 0x123635408> ## [1] 0.9493789 ## Species ~ Sepal.Length + Petal.Length + Petal.Width ## <environment: 0x110d66518> ## [1] 0.9353933 ## you can use different strategy for search the optimal subset: Best First Search (best.first.search), Exhaustive Search (exhaustive.search), Greedy Search (forward.search, backward.search), etc. ## prints the result f = as.simple.formula(subset, "Species") print(f) ## Species ~ Sepal.Length + Petal.Width ## <environment: 0x1263bfd08> Frequent patterns library(arules) ## assumig you have installed arules cat(readLines("toy-transaction.txt"),sep='\n') ## see what's inside the toy transactions ## A,B,C ## B,C ## A,B,D ## A,B,C,D ## A ## B tr = read.transactions("toy-transaction.txt",format="basket",sep=",") tr ## transactions in sparse format with ## 6 transactions (rows) and ## 4 items (columns) inspect(tr) ## items ## 1 {A, ## B, ## C} ## 2 {B, ## C} ## 3 {A, ## B, ## D} ## 4 {A, ## B,
Image of page 16
## C, ## D} ## 5 {A} ## 6 {B} image(tr) itemFrequencyPlot(tr, support = 0.1)
Image of page 17
length(tr) ## [1] 6 ## mine frequent itemsets by using the Apriori algorithm rules = apriori(tr, parameter= list(supp=0.5, conf=0.5)) ## ## parameter specification: ## confidence minval smax arem aval originalSupport support minlen maxlen ## 0.5 0.1 1 none FALSE TRUE 0.5 1 10 ## target ext ## rules FALSE ## ## algorithmic control: ## filter tree heap memopt load sort verbose ## 0.1 TRUE TRUE FALSE TRUE 2 TRUE ## ## apriori - find association rules with the apriori algorithm ## version 4.21 (2004.05.09) (c) 1996-2004 Christian Borgelt ## set item appearances ...[0 item(s)] done [0.00s]. ## set transactions ...[4 item(s), 6 transaction(s)] done [0.00s]. ## sorting and recoding items ... [3 item(s)] done [0.00s]. ## creating transaction tree ... done [0.00s]. ## checking subsets of size 1 2 done [0.00s]. ## writing ... [7 rule(s)] done [0.00s]. ## creating S4 object ... done [0.00s]. ## set up parameter for support and confidence inspect(rules) ## lhs rhs support confidence lift
Image of page 18
## 1 {} => {C} 0.5000000 0.5000000 1.0 ## 2 {} => {A} 0.6666667 0.6666667 1.0 ## 3 {} => {B} 0.8333333 0.8333333 1.0 ## 4 {C} => {B} 0.5000000 1.0000000 1.2 ## 5 {B} => {C} 0.5000000 0.6000000 1.2 ## 6 {A} => {B} 0.5000000 0.7500000 0.9 ## 7 {B} => {A} 0.5000000 0.6000000 0.9 summary(rules) ## set of 7 rules ## ## rule length distribution (lhs + rhs):sizes ## 1 2 ## 3 4 ## ## Min. 1st Qu. Median Mean 3rd Qu. Max.
Image of page 19

You've reached the end of your free preview.

Want to read all 19 pages?

  • Fall '17
  • yuru lin
  • Data Mining, Mean, Na Na Hey Hey Kiss Him Goodbye, Qu.

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes