以下是《集体编程智慧中的代码》
defuniquecounts(rows):results={}forrowinrows:# The result is the last columnr=row[len(row)-1]ifrnotinresults: results[r]=0results[r]+=1returnresultsdefginiimpurity(rows):total=len(rows)counts=uniquecounts(rows)imp=0fork1incounts:p1=float(counts[k1])/total#imp+=p1*p1fork2incounts:ifk1==k2:continuep2=float(counts[k2])/totalimp+=p1*p2returnimp#1-imp
基尼不纯度的大概意思是 一个随机事件变成它的对立事件的概率。 例如
一个随机事件X ,P(X=0) = 0.5 ,P(X=1)=0.5那么基尼不纯度就为 P(X=0)*(1 - P(X=0)) + P(X=1)*(1
- P(X=1)) = 0.5
公式为:
而本书中并未使用这个公式(其实使用该公式会让代码更加清晰明了),接下来我们解释书中代码使用的数学内容:
假设:A+B+C=1
(A+B+C)(A+B+C)-(A*A+B*B+C*C)=2*(A*B+A*C+B*C)
1-(A*A+B*B+C*C)=2*(A*B+A*C+B*C)
等式左边就是基尼不纯度
本文介绍了基尼不纯度的概念及其在机器学习中的应用。通过具体的Python代码示例,详细解析了如何计算数据集的基尼不纯度,并探讨了一种计算方式背后的数学原理。
1539

被折叠的 条评论
为什么被折叠?



