# # 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. # #----- set.seed(0) source('../Chapter18/restaurant_problem_gen_data.R') source('../Chapter18/decision_tree_learning.R') source('./perceptron_learning.R') training_set_sizes = seq( 10, 100, by=10 ) tree_testing_set_accuracy = c() perc_testing_set_accuracy = c() for( tss in training_set_sizes ){ mcs_tree = c() mcs_perc = c() for( mci in 1:10 ){ if( mci!=1 ){ print( sprintf("REST: Train set size %10d; mci= %5d; tree_accuracy= %5.3f; perc_accuracy= %5.3f", tss, mci, mean(mcs_tree), mean(mcs_perc) ) ) } # Get some training data from the restaurant problem (taken from chap_18_prob_11.R): # D_train = restaurant_problem_gen_data( tss ) data = D_train$data willwait = D_train$willwait # Create the initial attributes variable (an R list with items and keys as above): # Note our training set must have seen the possible values for each attribute. # attributes = list() for( cn in colnames(data) ){ attributes[[cn]] = sort(as.matrix(unique(data[cn]))) } # Compute the default classification: # default = majority_vote(willwait) # Build a model using the complete data and then use it to predict on the training data: # tree = decision_tree_learning( data, willwait, attributes, default ) # Learn a PERCEPTRON with a training set of the given size: # X = map_features_to_doubles( data ) y = willwait + 0 # convert T/F to 0's and 1's perceptron = perceptron_build_model( X, y ) # Generate new data to test with: # D_test = restaurant_problem_gen_data( tss ) data_test = D_test$data willwait_test = D_test$willwait yhat = decision_tree_predict_multiple_samples( tree, data_test ) acc = sum(willwait_test == yhat)/tss mcs_tree = c(mcs_tree,acc) X = map_features_to_doubles( data_test ) yhat = perceptron_predict( perceptron, X ) acc = sum(willwait_test == yhat)/tss mcs_perc = c(mcs_perc,acc) } tree_testing_set_accuracy = c(tree_testing_set_accuracy, mean(mcs_tree)) perc_testing_set_accuracy = c(perc_testing_set_accuracy, mean(mcs_perc)) } #postscript("../../WriteUp/Graphics/Chapter20/chap_20_dup_fig_22_restaurant_problem.eps", onefile=FALSE, horizontal=FALSE) plot( training_set_sizes, tree_testing_set_accuracy, col='red', type='l', xlab='training set size', ylab='Proporation correct on test set', ylim=c(0.4,1.0) ) lines( training_set_sizes, perc_testing_set_accuracy, col='green', type='l' ) grid() legend( 10, 1.0, c('decision tree test accuracy','perceptron test accuracy'), lty=1, col=c('red','green') ) #dev.off()