决策树DecisionTree

2021-01-14 09:49:52 浏览数 (1)

决策树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:

0 人点赞