How to edit the rule sets of decision tree in R

Sometimes it is desirable to edit the rule sets derived from the training data before making prediction for the test data. There are many packages for decision tree classification in R.  Today I explored the way to edit the rule sets.  See the sample codes and illustration below.  The way to do it is to save the rule sets as ASCII file, edit the rule sets in the ASCII file, then import back to R to do prediction.See the sample codes and illustration below.

####################################################

library(C50)

data(churn)

ruleModel <- C5.0(churn ~ ., data = churnTrain, rules = TRUE)       #construct the rule sets

ruleModel

summary(ruleModel)               #print out the rule sets

ruleText = ruleModel$rules

write(ruleText, file=”ruleText.txt”)              #save the rule sets to ASCII file, edit the rule sets as needed.

ruleModel$rules = “”                                      #empty the original rule sets

predict(ruleModel,newdata=churnTest)                     #no rules in the model, will get error on this line

ruleText = paste(readLines(“ruleText.txt”),collapse=”\n”)           #imported the modified rule sets to R

ruleModel$rules=ruleText                                        #assign the modified rules back to the rule model

predict(ruleModel,newdata=churnTest)                #do prediction based on the new rule sets

####################################################

rules

 

Advertisements

About Qiusheng Wu

Assistant Professor of Geography at Binghamton University, State University of New York

Posted on June 6, 2014, in Algorithms, GIS/RS, Tutorials and tagged , . Bookmark the permalink. 3 Comments.

  1. Hello,
    I get the error: “Error: unexpected input in “ruleModel$rules = “” on the below line:
    ruleModel$rules = “” #empty the original rule sets

    is there supposed to be a file name between the quotes ? if so what will it be ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Alex Tereshenkov

Programming and managing GIS

REDD+ for the Guiana Shield

Technical Cooperation Project

LidarBlog.com

Dr. Qiusheng Wu @ SUNY Binghamton

Writing Science

How to write papers that get cited and proposals that get funded

GIS In Ecology

Providing Training, Advice And Consultation On The Use Of GIS In Ecology

metrhispanic

On cities, land, ...

GeoAcademy

Open GIS: No Bounds

Scientia Plus Conscientia

Thoughts on Science and Nature

r4hydrology

Learning hydrology with R

Karl Hennermann

GIS at the University of Manchester

GIS and Science

Applications of geospatial technology for scientific research and understanding.

GISblog.com

GIS, Mapping, Remote Sensing, Geodata, Geospatial news

Whitebox Geospatial Analysis Tools

Open-source GIS development and spatial analysis with Whitebox GAT

TopoToolbox

MATLAB-based software for topographic analysis

Anything Geospatial – AnyGeo

Dr. Qiusheng Wu @ SUNY Binghamton

GeospatialPython.com

Dr. Qiusheng Wu @ SUNY Binghamton

Another GIS Blog

Dr. Qiusheng Wu @ SUNY Binghamton

%d bloggers like this: