国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現求特征選擇的信息增益

2020-02-16 00:11:55
字體:
來源:轉載
供稿:網友

使用python語言,實現求特征選擇的信息增益,可以同時滿足特征中有連續型和二值離散型屬性的情況。

師兄讓我做一個特征選擇的代碼,我在網上找了一下,大部分都是用來求離散型屬性的信息益益,但是我的數據是同時包含二值離散型和連續型屬性的,所以這里實現了一下。

代碼塊

import numpy as npimport mathclass IG():  def __init__(self,X,y):    X = np.array(X)    n_feature = np.shape(X)[1]    n_y = len(y)    orig_H = 0    for i in set(y):      orig_H += -(y.count(i)/n_y)*math.log(y.count(i)/n_y)    condi_H_list = []    for i in range(n_feature):      feature = X[:,i]      sourted_feature = sorted(feature)      threshold = [(sourted_feature[inde-1]+sourted_feature[inde])/2 for inde in range(len(feature)) if inde != 0 ]      thre_set = set(threshold)      if float(max(feature)) in thre_set:        thre_set.remove(float(max(feature)))      if min(feature) in thre_set:        thre_set.remove(min(feature))      pre_H = 0      for thre in thre_set:        lower = [y[s] for s in range(len(feature)) if feature[s] < thre]        highter = [y[s] for s in range(len(feature)) if feature[s] > thre]        H_l = 0        for l in set(lower):          H_l += -(lower.count(l) / len(lower))*math.log(lower.count(l) / len(lower))        H_h = 0        for h in set(highter):          H_h += -(highter.count(h) / len(highter))*math.log(highter.count(h) / len(highter))        temp_condi_H = len(lower)/n_y *H_l+ len(highter)/n_y * H_h        condi_H = orig_H - temp_condi_H        pre_H = max(pre_H,condi_H)      condi_H_list.append(pre_H)    self.IG = condi_H_list  def getIG(self):    return self.IGif __name__ == "__main__":  X = [[1, 0, 0, 1],     [0, 1, 1, 1],     [0, 0, 1, 0]]  y = [0, 0, 1]  print(IG(X,y).getIG())

輸出結果為:

[0.17441604792151594, 0.17441604792151594, 0.17441604792151594, 0.6365141682948128]

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乐都县| 通州市| 抚顺县| 资源县| 北碚区| 永春县| 苏尼特左旗| 睢宁县| 寿宁县| 子长县| 昭通市| 屏东县| 江孜县| 威海市| 赤城县| 卢湾区| 伽师县| 怀宁县| 楚雄市| 马公市| 包头市| 婺源县| 阳东县| 汉中市| 耒阳市| 平南县| 莒南县| 垣曲县| 论坛| 札达县| 郑州市| 深州市| 潮安县| 奎屯市| 徐州市| 兴国县| 济阳县| 徐州市| 伽师县| 武冈市| 巴彦淖尔市|