# # Written by: # -- # John L. Weatherwax 2009-04-21 # # email: wax@alum.mit.edu # # Please send comments and especially bug reports to the # above email address. # # EPage 787 # #----- restaurant_problem_gen_data = function( N ){ # # A function to generate data from the decision tree given on Figure~18.2 # alternate = sample( c("No","Yes"), N, replace=TRUE ) bar = sample( c("No","Yes"), N, replace=TRUE ) fri_sat = sample( c("No","Yes"), N, replace=TRUE ) hungry = sample( c("No","Yes"), N, replace=TRUE ) patrons = sample( c("None","Some","Full"), N, replace=TRUE ) price = sample( c("$","$$","$$$"), N, replace=TRUE ) raining = sample( c("No","Yes"), N, replace=TRUE ) reservation = sample( c("No","Yes"), N, replace=TRUE ) type = sample( c("French","Italian","Thai","Burger"), N, replace=TRUE ) waitestimate = sample( c("0-10","10-30","30-60",">60"), N, replace=TRUE ) # Compute the result given the known truth model: # willwait = as.logical( rep(1,N) ) # by default we will wait ==True (won't wait ==False) inds = patrons == "None" willwait[inds] = FALSE inds = ( patrons == "Full" ) & ( waitestimate == ">60" ) willwait[inds] = FALSE inds = ( patrons == "Full" ) & ( waitestimate == "30-60" ) & ( alternate == "No" ) & ( reservation == "No" ) & ( bar == "No" ) willwait[inds] = FALSE inds = ( patrons == "Full" ) & ( waitestimate == "30-60" ) & ( alternate == "Yes" ) & ( fri_sat == "No" ) willwait[inds] = FALSE inds = ( patrons == "Full" ) & ( waitestimate == "10-30" ) & ( hungry == "Yes" ) & ( alternate == "Yes" ) & ( raining == "No" ) willwait[inds] = FALSE df = data.frame( alternate=alternate, bar=bar, fri_sat=fri_sat, hungry=hungry, patrons=patrons, price=price, raining=raining, reservation=reservation, type=type, waitestimate=waitestimate ) list( data=df, willwait=willwait ) } map_features_to_doubles = function( X ){ # # Maps the given True/False, string, and character features to real numbers [0,1] # for( jj in 1:dim(X)[2] ){ num_column = as.numeric(X[,jj]) mx = max(num_column) X[,jj] = num_column/mx } X }