import numpy as np import sys def calc_ent(x): x_value_list = set([x[i] for i in range(x.shape[0])]) ent = 0.0 for x_value in x_value_list: p = float(x[x == x_value].shape[0]) / x.shape[0] logp = np.log2(p) ent -= p * logp return ent def calc_condition_ent(x, y): x_value_list = set([x[i] for i in range(x.shape[0])]) ent = 0.0 for x_value in x_value_list: sub_y = y[x == x_value] temp_ent = calc_ent(sub_y) ent += (float(sub_y.shape[0]) / y.shape[0]) * temp_ent return ent def calc_ent_grap(x,y): base_ent = calc_ent(y) condition_ent = calc_condition_ent(x, y) ent_grap = base_ent - condition_ent return ent_grap x=[1,1,2,0,3] y=[1,1,0,0,0] x=np.array(x) y=np.array(y) print('%.2f'%calc_ent_grap(x,y))
信息增益
最新推荐文章于 2024-07-20 21:00:29 发布