亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python 中的基本決策樹

Python 中的基本決策樹

元芳怎么了 2023-02-07 11:01:29
我正在學習機器學習書中決策樹的基本知識。書中出現這個例子是為了理解一些東西(我知道這不是決策樹,但有些東西我不明白)def gini (p):    return (p)*(1-(p))-(1-p)*(1-(1-p))def entropy(p):    return -p*np.log2(p)-(1-p)*np.log2((1-p))def error(p):    return 1- np.max([p, 1-p])x=np.arange(0.0, 1.0, 0.01)ent=[entropy(p) if p != 0 else None for p in x]sc_ent = [e*0.5 if e else None for e in ent]err= [error(i) for i in x]fig = plt.figure()ax=plt.subplot(111)for i, lab, ls, c, in zip ([ent, sc_ent, gini(x), err], ['Entropy', 'Entropy(scaled)', 'Gini Impurity', 'Misclassification Error'], ['-', '-', '--', '-.'], ['black', 'lightgray','red','green', 'cyan']):    line= ax.plot(x,i,label=lab, linestyle=ls, lw=2, color =c)ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.15), ncol=5, fancybox=True, shadow=False)ax.axhline(y=0.5, linewidth= 1, color='k', linestyle='--')ax.axhline(y=1, linewidth= 1, color='k', linestyle='--')plt.ylim([0, 1.1])plt.xlabel('p(i=1)')plt.ylabel('Impurity index')plt.show()有人可以解釋這里發生了什么嗎?ent=[entropy(p) if p != 0 else None for p in x]sc_ent = [e*0.5 if e else None for e in ent]err= [error(i) for i in x]
查看完整描述

1 回答

?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

x是一個數字列表。的熵0顯然是未定義的(所以我們返回None),但對于其他數字,它返回一個我們返回的數字。這是第一行。

第二行采用熵列表,并將每個數值除以 2(它對 a 不執行任何操作None)。第三行給出了一個錯誤列表,每個錯誤對應列表中的一個元素x。


查看完整回答
反對 回復 2023-02-07
  • 1 回答
  • 0 關注
  • 95 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號