-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest1.py
More file actions
55 lines (41 loc) · 1.06 KB
/
Copy pathtest1.py
File metadata and controls
55 lines (41 loc) · 1.06 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
51
52
53
54
import csv
import numpy as np
from sslMarkovRandomWalks import sslMarkovRandomWalks
np.random.seed(seed=1234)
# read data , inputs are in X, labels are in Y
f=open('voting.data')
X=[]
Y=[]
reader = csv.reader(f, delimiter=',')
for row in reader:
tmp=[]
for i in range(1,len(row)-1):
if row[i]=='+':
tmp.append(1)
elif row[i]=='-':
tmp.append(-1)
else:
tmp.append(0)
X.append(tmp)
if row[-1]=='0':
Y.append(-1)
else:
Y.append(1)
X=np.array(X)
Y=np.array(Y)
# to divide the dataset into two part as random one is labelled another part is unlabelled
# creaet a index from 0 to number of data and randomly reorder it
n=X.shape[0]
no2=int(n/5)
indices = np.arange(n)
np.random.shuffle(indices)
# divide it into the midlle train and test
Xtrain=X[indices[0:no2],:]
Ytrain=Y[indices[0:no2]]
Xtest=X[indices[no2:],:]
Ytest=Y[indices[no2:]]
# run method
yhat,prob=sslMarkovRandomWalks(Xtrain,Ytrain,Xtest)
# calculate accuracy
acc=(yhat==Ytest).mean()
print('Accuracy :'+str(acc))