-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
131 lines (106 loc) · 3.76 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import numpy as np
import pandas as pd
from sklearn.metrics import mutual_info_score
import sys
def logloss(preds, targets):
res = 0
for pred, target in zip(preds, targets):
pred = np.min(pred, 1 - 10**(-3))
pred = np.max(pred, 10**(-3))
if pred == 1.0:
pred = 0.999
if pred == 0.0:
pred = 0.001
res += target * np.log(pred) + (1 - target) * np.log(1 - pred)
res /= len(targets)
res = -res
return res
def get_params():
params = {}
params["objective"] = "binary:logistic"
params["eval_metric"] = "logloss"
params["eta"] = 0.1
params["min_child_weight"] = 1
params["subsample"] = 1
params["colsample_bytree"] = 0.3
params["silent"] = 1
params["max_depth"] = 7
return params
def log(f, line):
f.write(line + '\n')
# calculate mutual information
def calc_MI(x, y, bins):
c_xy = np.histogram2d(x, y, bins)[0]
mi = mutual_info_score(None, None, contingency=c_xy)
return mi
def discret(val, bins):
for idx in xrange(len(bins)):
try:
if val >= bins[idx] and val <= bins[idx+1] :
return idx
except:
print bins
print val
sys.exit(-1)
# for category feature
def calc_MI_cate_feat_target(column, target, num_bins):
vals, tmp_indexer = pd.factorize(column, na_sentinel=-1)
p_neg = 0.238801
p_pos = 0.761199
max_cate = np.max(vals)
densitys, bin_edges = np.histogram(vals, density=True)
#print densitys
#print 'start'
final_mi = 0
for level in xrange(-1, max_cate+1):
p_cate_pos = np.sum((vals == level) & (target == 1)) / float(column.shape[0])
p_cate_neg = np.sum((vals == level) & (target == 0)) / float(column.shape[0])
p_cate = np.sum((vals == level)) / float(column.shape[0])
if p_cate_pos == 0 or p_cate_neg == 0:
continue
final_mi += p_cate_pos * np.log2(p_cate_pos / (p_cate * p_pos))
final_mi += p_cate_neg * np.log2(p_cate_neg / (p_cate * p_neg))
#print '%d, %f' %(level, final_mi)
return final_mi
def calc_MI_feat_target(column, target, num_bins):
p_neg = 0.238801
p_pos = 0.761199
column = column.round(5)
min_val = np.min(column)
max_val = np.max(column)
column.fillna(-999, inplace=True)
values = column.values
try:
bins = [-999] + np.arange(min_val, max_val, (max_val-min_val)/float(num_bins)).tolist()
print '%f - %f' % (min_val, max_val)
except:
print min_val
print max_val
print (max_val-min_val)/num_bins
sys.exit(-1)
bins[-1] = max_val
densitys, bin_edges = np.histogram(values, bins=bins, density=True)
dist_vals = []
for val in column.values:
dist_val = discret(val, bins)
dist_vals.append(dist_val)
final_mi = 0
dist_vals = np.array(dist_vals)
for level in xrange(len(bins)):
p_cate_pos = np.sum((dist_vals == level) & (target == 1)) / float(column.shape[0])
p_cate_neg = np.sum((dist_vals == level) & (target == 0)) / float(column.shape[0])
p_cate = np.sum((dist_vals == level)) / float(column.shape[0])
if p_cate_pos == 0 or p_cate_neg == 0:
continue
final_mi += p_cate_pos * np.log2(p_cate_pos / (p_cate * p_pos))
final_mi += p_cate_neg * np.log2(p_cate_neg / (p_cate * p_neg))
return final_mi
# compute statistics of numeric feature - one column
def num_feat_stat(vals):
num_nan = np.sum([np.isnan(val) for val in vals])
num_inf = np.sum([np.isinf(val) for val in vals])
num_zero = np.sum(vals == 0)
max_val = np.max(vals)
min_val = np.min(vals)
#print '%d %d %f %f' % (num_nan, num_zero, max_val, min_val)
return num_nan, num_inf, num_zero