-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutili.py
135 lines (113 loc) · 3.65 KB
/
utili.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
import numpy as np
import matplotlib.pyplot as plt
def predict(X, theta):
return np.dot(X, theta)
#Y = [m * 1] = h
def predictLog(theta, x, algo):
z = np.matmul(x, theta)
y = 1 / (1 + np.exp(-z))
if algo == 1:
#print(y)
#Se il risultato è maggiore di 0.5 appartiene alla classe 1, altrimenti appartiene alla classe 0
if y > 0.5:
print("La tupla appartiene alla classe con probabilita': ",format(y*100,'.2f'),"%")
else:
print("La tupla NON appartiene alla classe con probabilita': ",format((1-y)*100,'.2f'),"%")
#return y
# else:
# return y
def Cost(X, y, theta):
m = len(y)
h = predict(X, theta)
diff = h - y
J = (np.sum(diff ** 2)) / (2 * m)
return J
def CostLog(h, y):
m = len(y)
J = (sum((-y * np.log(h)) - ((1 - y) * np.log(1 - h)))) / m
return J
def normalEquations(X, y):
a = np.dot(X.T, X)
b = np.dot(X.T, y.T)
theta = np.dot((np.linalg.inv(a)), b)
return theta
def gradientDescent(X, y, theta, alpha, num_iters):
m = len(y)
history = []
for i in range(0, num_iters):
h = predict(X, theta)
diff = h - y
theta = theta - ((alpha/m) * np.dot(X.T, diff))
history.append(Cost(X, y, theta))
return (theta, history)
def gradientDescent_logistic(X, y, theta, alpha, num_iters):
m = len(y)
history = []
for i in range(0, num_iters):
z = predict(X, theta)
h = 1 / (1 + np.exp(-z))
diff = h - y
theta = theta - ((alpha/m) * np.dot(X.T, diff))
history.append(CostLog(h, y))
return (theta, history)
def gradientDescent_logistic_multival(X, y, theta, alpha, num_iters):
history = []
for i in range(0, y.shape[1]):
theta[:, i], history2 = gradientDescent_logistic(X, y[:, i], theta[:, i], alpha, num_iters)
history.append([history2])
return (theta, history)
def stochastic_grad_des(X, y, theta, alpha, num_iters):
print("inizio stochastic")
history = []
J = []
for a in range(0, num_iters):
# Derivo per il numero di samples
for i in range(0, X.shape[0]):
# Derivo per il numero di features
for j in range(0, X.shape[1]):
# Calcolo l'ipotesi
e = predict(X[i], theta)
e = e - y[i]
# Calcolo la derivata (non sono sicuro che sia x[i][j]
deriv = e * X[i][j]
# Salvo temporaneamente theta qui, perchè l'update va fatto dopo aver calcolato tutti i theta
theta[j] = (theta[j] - (alpha * deriv) / X.shape[1])
history.append(Cost(X, y, theta))
return (theta, history)
def mini_batch(X, y, theta, alpha, num_iters, b):
print("Inizio minibatch")
m = len(y)
history = []
z = 0
for i in range(0, num_iters):
#Iteriamo fino a qunado non consideriamo tutti i samples
j=0
while j < m:
k = j + b
theta, history2 = gradientDescent(X[j:k], y[j:k], theta, alpha, 1)
history.append(history2)
j += b
z += 1
return (theta, history)
def plotLearning(history):
fig = plt.figure()
ax = plt.subplot(111)
plt.plot(np.arange(len(history)), history, '-b')
plt.xlabel('iteration')
plt.ylabel('J')
plt.show(block = False)
plt.subplot(ax)
plt.pause(2)
plt.close()
return fig
def plotLearningK(history):
fig = plt.figure()
ax = plt.subplot(111)
plt.plot(np.arange(1,(len(history)+1)), history, '-b')
plt.xlabel('k')
plt.ylabel('J')
plt.show(block = False)
plt.subplot(ax)
plt.pause(2)
plt.close()
return fig