import sys
sys.path.append('/usr/local/cellar/pil/1.1.7/lib/python2.7/site-packages/PIL/')

# Script to demonstrate the CART-like DT classifier from
# Chapter 7 of "Programming Collective Intelligence" by
# T. Segaran, O'Reilly, (c) 2007
#
import treepredict
import fileinput
import Image
import ImageDraw


# If the last parameter is set to 0, then all attributes other than 'age' and 'war' would be used.

train_data, test_data = fileinput.loadDataset(5, ['age','gender','occupation','fantasy','film-noir', 'drama', 'western'], 1)

tree=treepredict.buildtree(train_data,gain_increment=0,gain_threshold=0,instance_minimum=100)

# Let's see what it looks like...
print "\nFinal tree...\n"
treepredict.printtree(tree)

trainConfMat, crTrain = treepredict.testTree(train_data, tree)
print 'Training set confusion matrix (Classification rate:', crTrain,'):'
for row in trainConfMat:
    print '\t'.join(map(lambda x:str(x), row))

print ''
  
示例#2
0
#!/usr/bin/python
import sys
sys.path.append('/usr/local/cellar/pil/1.1.7/lib/python2.7/site-packages/PIL/')

# Script to demonstrate the CART-like DT classifier from
# Chapter 7 of "Programming Collective Intelligence" by
# T. Segaran, O'Reilly, (c) 2007
#
import treepredict
import fileinput
import Image
import ImageDraw

# If the last parameter is set to 0, then all attributes other than 'age' and 'war' would be used.

train_data, test_data = fileinput.loadDataset(
    5, ['age', 'fantasy', 'film-noir', 'horror', 'western'], 1)

tree = treepredict.buildtree(train_data,
                             gain_increment=0,
                             gain_threshold=0,
                             instance_minimum=1)

trainConfMat, crTrain = treepredict.testTree(train_data, tree)
print 'Training set confusion matrix (Classification rate:', crTrain, '):'
for row in trainConfMat:
    print '\t'.join(map(lambda x: str(x), row))

print ''

testConfMat, crTest = treepredict.testTree(test_data, tree)
print 'Test set confusion matrix (Classification rate:', crTest, '):'
示例#3
0
import sys
sys.path.append('/usr/local/cellar/pil/1.1.7/lib/python2.7/site-packages/PIL/')

# Script to demonstrate the CART-like DT classifier from
# Chapter 7 of "Programming Collective Intelligence" by
# T. Segaran, O'Reilly, (c) 2007
#
import treepredict
import fileinput
import Image
import ImageDraw


# If the last parameter is set to 0, then all attributes other than 'age' and 'war' would be used.

train_data, test_data = fileinput.loadDataset(2, ['age', 'gender','occupation','unknown genre','film-noir', 'horror', 'western'], 1)

tree=treepredict.buildtree(train_data,gain_increment=0,gain_threshold=0,instance_minimum=0)

# Let's see what it looks like...
print "\nFinal tree...\n"
treepredict.printtree(tree)

trainConfMat, crTrain = treepredict.testTree(train_data, tree)
print 'Training set confusion matrix (Classification rate:', crTrain,'):'
for row in trainConfMat:
    print '\t'.join(map(lambda x:str(x), row))

print ''
  
import sys
sys.path.append('/usr/local/cellar/pil/1.1.7/lib/python2.7/site-packages/PIL/')

# Script to demonstrate the CART-like DT classifier from
# Chapter 7 of "Programming Collective Intelligence" by
# T. Segaran, O'Reilly, (c) 2007
#
import treepredict
import fileinput
import Image
import ImageDraw


# If the last parameter is set to 0, then all attributes other than 'age' and 'war' would be used.

train_data, test_data = fileinput.loadDataset(5, ['age', 'fantasy','film-noir', 'horror', 'western'], 1)


def testing_gain_increments(increments=[]):
  classresults={}
  
  for increment in increments:
    tree=treepredict.buildtree(train_data,gain_increment=increment,gain_threshold=0,instance_minimum=1)

    trainConfMat, crTrain = treepredict.testTree(train_data, tree)
    print 'Training set confusion matrix (Classification rate:', crTrain,'):'
    for row in trainConfMat:
      print '\t'.join(map(lambda x:str(x), row))

    print ''