python實(shí)現(xiàn)決策樹ID3算法的示例代碼
在周志華的西瓜書和李航的統(tǒng)計(jì)機(jī)器學(xué)習(xí)中對決策樹ID3算法都有很詳細(xì)的解釋,如何實(shí)現(xiàn)呢?核心點(diǎn)有如下幾個(gè)步驟
step1:計(jì)算香農(nóng)熵
from math import log import operator # 計(jì)算香農(nóng)熵 def calculate_entropy(data): label_counts = {} for feature_data in data: laber = feature_data[-1] # 最后一行是laber if laber not in label_counts.keys(): label_counts[laber] = 0 label_counts[laber] += 1 count = len(data) entropy = 0.0 for key in label_counts: prob = float(label_counts[key]) / count entropy -= prob * log(prob, 2) return entropy
step2.計(jì)算某個(gè)feature的信息增益的方法
# 計(jì)算某個(gè)feature的信息增益 # index:要計(jì)算信息增益的feature 對應(yīng)的在data 的第幾列 # data 的香農(nóng)熵 def calculate_relative_entropy(data, index, entropy): feat_list = [number[index] for number in data] # 得到某個(gè)特征下所有值(某列) uniqual_vals = set(feat_list) new_entropy = 0 for value in uniqual_vals: sub_data = split_data(data, index, value) prob = len(sub_data) / float(len(data)) new_entropy += prob * calculate_entropy(sub_data) # 對各子集香農(nóng)熵求和 relative_entropy = entropy - new_entropy # 計(jì)算信息增益 return relative_entropy
step3.選擇最大信息增益的feature
# 選擇最大信息增益的feature def choose_max_relative_entropy(data): num_feature = len(data[0]) - 1 base_entropy = calculate_entropy(data)#香農(nóng)熵 best_infor_gain = 0 best_feature = -1 for i in range(num_feature): info_gain=calculate_relative_entropy(data, i, base_entropy) #最大信息增益 if (info_gain > best_infor_gain): best_infor_gain = info_gain best_feature = i return best_feature
step4.構(gòu)建決策樹
def create_decision_tree(data, labels): class_list=[example[-1] for example in data] # 類別相同,停止劃分 if class_list.count(class_list[-1]) == len(class_list): return class_list[-1] # 判斷是否遍歷完所有的特征時(shí)返回個(gè)數(shù)最多的類別 if len(data[0]) == 1: return most_class(class_list) # 按照信息增益最高選取分類特征屬性 best_feat = choose_max_relative_entropy(data) best_feat_lable = labels[best_feat] # 該特征的label decision_tree = {best_feat_lable: {}} # 構(gòu)建樹的字典 del(labels[best_feat]) # 從labels的list中刪除該label feat_values = [example[best_feat] for example in data] unique_values = set(feat_values) for value in unique_values: sub_lables=labels[:] # 構(gòu)建數(shù)據(jù)的子集合,并進(jìn)行遞歸 decision_tree[best_feat_lable][value] = create_decision_tree(split_data(data, best_feat, value), sub_lables) return decision_tree
在構(gòu)建決策樹的過程中會(huì)用到兩個(gè)工具方法:
# 當(dāng)遍歷完所有的特征時(shí)返回個(gè)數(shù)最多的類別 def most_class(classList): class_count={} for vote in classList: if vote not in class_count.keys():class_count[vote]=0 class_count[vote]+=1 sorted_class_count=sorted(class_count.items,key=operator.itemgetter(1),reversed=True) return sorted_class_count[0][0] # 工具函數(shù)輸入三個(gè)變量(待劃分的數(shù)據(jù)集,特征,分類值)返回不含劃分特征的子集 def split_data(data, axis, value): ret_data=[] for feat_vec in data: if feat_vec[axis]==value : reduce_feat_vec=feat_vec[:axis] reduce_feat_vec.extend(feat_vec[axis+1:]) ret_data.append(reduce_feat_vec) return ret_data
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python時(shí)間戳與日期格式之間相互轉(zhuǎn)化的詳細(xì)教程
java默認(rèn)精度是毫秒級別的,生成的時(shí)間戳是13位,而python默認(rèn)是10位的,精度是秒,下面這篇文章主要給大家介紹了關(guān)于Python時(shí)間戳與日期格式之間相互轉(zhuǎn)化的相關(guān)資料,需要的朋友可以參考下2022-08-08python與matlab一些常用函數(shù)互轉(zhuǎn)問題
這篇文章主要介紹了python與matlab一些常用函數(shù)互轉(zhuǎn),包括十六進(jìn)制字節(jié)流數(shù)據(jù)的相關(guān)知識(shí),本文通過示例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2022-12-12python實(shí)現(xiàn)的簡單窗口倒計(jì)時(shí)界面實(shí)例
這篇文章主要介紹了python實(shí)現(xiàn)的簡單窗口倒計(jì)時(shí)界面,實(shí)例分析了Python基于Tkinter操作windows窗口界面的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05Python實(shí)現(xiàn)柵欄密碼的加密解密方法詳解
這篇文章主要介紹了Python實(shí)現(xiàn)柵欄密碼的加密解密方法,所謂柵欄密碼,就是把要加密的明文分成N個(gè)一組,然后把每組的第1個(gè)字連起來,形成一段無規(guī)律的話。不過柵欄密碼本身有一個(gè)潛規(guī)則,就是組成柵欄的字母一般不會(huì)太多2023-01-01python3常用的數(shù)據(jù)清洗方法(小結(jié))
這篇文章主要介紹了python3常用的數(shù)據(jù)清洗方法(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10Python實(shí)現(xiàn)帶圖形界面的炸金花游戲(升級版)
詐金花又叫三張牌,是在全國廣泛流傳的一種民間多人紙牌游戲,它具有獨(dú)特的比牌規(guī)則。本文將通過Python語言實(shí)現(xiàn)升級版的帶圖形界面的詐金花游戲,需要的可以參考一下2022-12-12