-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkfoldcv.py
More file actions
53 lines (44 loc) · 1.48 KB
/
kfoldcv.py
File metadata and controls
53 lines (44 loc) · 1.48 KB
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
# Input: number of folds k
# numpy matrix X of features, with n rows (samples), d columns (features)
# numpy vector y of scalar values, with n rows (samples), 1 column
# Output: numpy vector z of k rows, 1 column
class Kfoldcv():
def __init__(self, k, X, y):
self.k = k
self.X = X
self.y = y
def kfoldcv(self):
from probcpredict import Probcpredict
from probclearn import Probclearn
import math
import numpy as np
n = len(self.y)
d = len(self.X[0])
z = np.zeros((self.k, 1))
for i in range(self.k):
T = set(range(int(math.floor(float(n) * i / float(self.k))), int(math.floor((float(n) * (i + 1) / float(self.k)) - 1) + 1)))
S = set(range(0, n)) - T
X_train = np.zeros((len(S), d))
y_train = np.zeros((len(S), 1))
index = []
for x in S:
index.append(x)
for t in range(len(S)):
X_train[t] = self.X[index[t]]
y_train[t] = self.y[index[t]]
pc=Probclearn(X_train, y_train)
q, mu_plus, mu_minus, sigma_plus, sigma_minus = pc.probclearn()
for t in T:
x = self.X[t].reshape(self.X[t].shape[0], 1)
pp = Probcpredict(q, mu_plus, mu_minus, sigma_plus, sigma_minus, x)
# x=np.zeros((d,1))
# for i in range(d):
# x[i]=X[t][i]
if self.y[t] != pp.probcpredict():
z[i] = z[i] + 1
z[i] = z[i] / float(len(T))
sum=0
for i in range(self.k):
sum=z[i]+sum
accuracy=sum/float(self.k)
return accuracy[0]