怎么使用python實(shí)現(xiàn)決策樹(shù)ID3算法-創(chuàng)新互聯(lián)

這篇文章主要介紹怎么使用python實(shí)現(xiàn)決策樹(shù)ID3算法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

站在用戶(hù)的角度思考問(wèn)題,與客戶(hù)深入溝通,找到定興網(wǎng)站設(shè)計(jì)與定興網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶(hù)體驗(yàn)好的作品,建站類(lèi)型包括:成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名與空間、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋定興地區(qū)。

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 對(duì)應(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) # 對(duì)各子集香農(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)建決策樹(shù)

def create_decision_tree(data, labels):
  class_list=[example[-1] for example in data]
  # 類(lèi)別相同,停止劃分
  if class_list.count(class_list[-1]) == len(class_list):
    return class_list[-1]
  # 判斷是否遍歷完所有的特征時(shí)返回個(gè)數(shù)最多的類(lèi)別
  if len(data[0]) == 1:
    return most_class(class_list)
  # 按照信息增益最高選取分類(lèi)特征屬性
  best_feat = choose_max_relative_entropy(data)
  best_feat_lable = labels[best_feat] # 該特征的label
  decision_tree = {best_feat_lable: {}} # 構(gòu)建樹(shù)的字典
  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)建決策樹(shù)的過(guò)程中會(huì)用到兩個(gè)工具方法:

# 當(dāng)遍歷完所有的特征時(shí)返回個(gè)數(shù)最多的類(lèi)別
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ù)集,特征,分類(lèi)值)返回不含劃分特征的子集
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

以上是“怎么使用python實(shí)現(xiàn)決策樹(shù)ID3算法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

文章名稱(chēng):怎么使用python實(shí)現(xiàn)決策樹(shù)ID3算法-創(chuàng)新互聯(lián)
URL鏈接:http://muchs.cn/article32/dphdpc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站內(nèi)鏈、網(wǎng)站建設(shè)做網(wǎng)站、建站公司、軟件開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)