-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNaiveBayes.py
160 lines (128 loc) · 4.94 KB
/
NaiveBayes.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from __future__ import division
from datetime import datetime
from math import exp, log, sqrt
from sklearn.feature_extraction import FeatureHasher
from sklearn.utils import murmurhash3_32
import numpy as np
num_samples = 10000000
train = 'c:/criteo/smaller_datasets/train-' + str(num_samples) # path to training file
p_d= 10e-2
D = 2 ** 25 # number of weights use for learning
# A. Bounded logloss
# INPUT:
# p: our prediction
# y: real answer
# OUTPUT
# logarithmic loss of p given y
def logloss(p, y):
p = max(min(p, 1. - 10e-12), 10e-12)
return -log(p) if y == 1. else -log(1. - p)
# B. Apply hash trick of the original csv row
# for simplicity, we treat both integer and categorical features as categorical
# INPUT:
# csv_row: a csv dictionary, ex: {'Lable': '1', 'I1': '357', 'I2': '', ...}
# D: the max index that we can hash to
# OUTPUT:
# x: a list of indices that its value is 1
def get_x(row, D):
x = [] # 0 is the index of the bias term
for i, value in enumerate(row):
#index = int(value + str(i), 16) % D # weakest hash ever ;)
index = murmurhash3_32(value + str(i), seed=0) % D
x.append(index)
continue
return x # x contains indices of features that have a value of 1
def count_feature_vals(x, counter):
for i in x:
counter[i] += 1
return counter
m = 1
def get_cond_probs(x, counter, total, print_probs = False):
probs = 1
for i in x:
num_occurrences = counter[i]
#if(num_occurrences == 0):
# print("Oh shit. Might have to dirichlet")
# exit(1)
# dirichlet
prob = (num_occurrences+m*p_d)/(total+m)
probs *= prob
if(print_probs):
print "prob: %f\toccurs: %d\ttotal: %d" % (prob, num_occurrences, total)
return probs
final_vloss = []
final_tloss = []
k_folds = 5
fold_size = num_samples / k_folds
for idx in range(k_folds):
v_start = idx*fold_size
v_end = v_start + fold_size - 1
n_yes = [0.] * D # number of times we've encountered a feature associated with yes
n_no = [0.] * D # number of times we've encountered a feature associated with no
total_yes = 0 # number of examples that are yes
total_no = 0 # number of examples that are no
for t, raw in enumerate(open(train)):
if t >= v_start and t <= v_end:
continue
row = raw.split('\t')
row[len(row)-1] = row[len(row)-1][:-1] # remove the trailing new line
y = 1. if row[0] == '1' else 0.
del row[0] # can't let the model peek the answer
x = get_x(row, D)
if(y == 1):
total_yes += 1
n_yes = count_feature_vals(x, n_yes)
else:
total_no += 1
n_no = count_feature_vals(x, n_no)
#if(t % 10000 == 0 and t > 0):
#print('%s\tencountered: %d\t' % (datetime.now(), t))
prob1_yes = total_yes/(total_no+total_yes)
prob1_no = total_no/(total_no+total_yes)
vloss = 0
tloss = 0
v_count = 0
t_count = 0
for t, raw in enumerate(open(train)):
if t >= v_start and t <= v_end:
row = raw.split('\t')
row[len(row)-1] = row[len(row)-1][:-1] # remove the trailing new line
y = 1. if row[0] == '1' else 0.
del row[0] # can't let the model peek the answer
x = get_x(row, D)
yes_chance = prob1_yes * get_cond_probs(x, n_yes, total_yes, print_probs=False)
no_chance = prob1_no * get_cond_probs(x, n_no, total_no)
p = (yes_chance/(yes_chance+no_chance))
#if(t % 1000 == 0 and t > 0):
# print (p)
# print [yes_chance, no_chance]
vloss += logloss(p, y)
#if(v_count % 10000 == 0 and v_count > 0):
#print('%s\tencountered: %d\tCurrent Logloss: %f' % (datetime.now(), v_count, vloss/v_count))
v_count += 1
else:
row = raw.split('\t')
row[len(row)-1] = row[len(row)-1][:-1] # remove the trailing new line
y = 1. if row[0] == '1' else 0.
del row[0] # can't let the model peek the answer
x = get_x(row, D)
yes_chance = prob1_yes * get_cond_probs(x, n_yes, total_yes, print_probs=False)
no_chance = prob1_no * get_cond_probs(x, n_no, total_no)
p = (yes_chance/(yes_chance+no_chance))
#if(t % 1000 == 0 and t > 0):
# print (p)
# print [yes_chance, no_chance]
tloss += logloss(p, y)
#if(t_count % 10000 == 0 and t_count > 0):
# print('%s\tencountered: %d\tCurrent Logloss: %f' % (
# datetime.now(), t_count, tloss/t_count))
t_count += 1
final_vloss.append(vloss/v_count)
final_tloss.append(tloss/t_count)
#print "vcount: %d" % v_count
#print "tcount: %d" % t_count
print "p = " + str(p_d)
print "training loss: "
print final_tloss
print "validation loss: "
print final_vloss