-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogistic_functions.py
61 lines (60 loc) · 1.93 KB
/
logistic_functions.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
import math
import numpy as np
import random
#sigmoidhs
def sigmoid(w,x):
multi = 0
for i in range(len(w)):
if(x[i]>0):
multi = multi + w[i]
return 1/(1 + math.exp(-multi))
#stoxastiki anavasi klisis, me oro kanonikopoihshs
def logistic_reg(email,typ,eta,maxStep,lamda):
wlen = len(email[0])+1
ws = [random.uniform(-1, 1)for iter in range(wlen)]
for step in range(maxStep):
s = 0
for i in range(len(typ)):#gia ola ta paradeigmata ekpaideusis
x = list(email[i])
x.append(1)
if typ[i]=='ham':
y=1
lw = math.log(sigmoid(ws,x))
else:
y=0
lw = math.log(1 - sigmoid(ws,x))
sum_w=0
for i in range(len(ws)):
sum_w = sum_w + ws[i]**2
s = s + lw - lamda * sum_w
tmp = y - sigmoid(ws,x)
for l in range(wlen):#enhmerose ta varh w
ws[l] = (1-2*lamda*eta)*ws[l] + eta * tmp * x[l]
if(s == 0):
break
return ws
#trexei ta paradeigmata pou pernei ws eisodo me vash t dosmena w
#epistrefei pinaka me apotelesmata me skopo ton upologismo twn Acc,Pre,Recall,F1
def logistic_run(w,x,typ):
res = [[0, 0], [0, 0]]
for i in range(len(typ)):
xi = list(x[i])
xi.append(1)
probplus = sigmoid(w,xi)
if(probplus>=0.5):
#theoro oti anhkei sth thetiki kathgoria
ans='ham'
else:
#theoro oti anhkei sthn arnhtikh kathgoria
ans='spam'
if (ans=='ham'):
if(ans == typ[i]):
res[0][0] = res[0][0] + 1#truenegative
else:
res[1][0] = res[1][0] + 1#falsenegative
else:
if(ans == typ[i] ):
res[1][1] = res[1][1] + 1#truepositive
else:
res[0][1] = res[0][1] + 1#falsepositive
return res