决策树Python代码实现
1.DecisionTree.py
代码语言:javascript
复制#! /usr/bin/env python2.8
# -*- coding: utf-8 -*-
# __author__ = "errrolyan"
# __Date__: 18-12-10
# __Describe__ = "决策树ID3算法算法Python实现版本”
import math
#find item in a list
def find(item, list):
for i in list:
if item(i):
return True
else:
return False
#find most common value for an attribute
def majority(attributes, data, target):
#find target attribute
valFreq = {}
#find target in data
index = attributes.index(target)
#calculate frequency of values in target attr
for tuple in data:
if (tuple[index] in valFreq.keys()):
valFreq[tuple[index]] = 1
else:
valFreq[tuple[index]] = 1
max = 0
major = ""
for key in valFreq.keys():
if valFreq[key]>max: