-
Notifications
You must be signed in to change notification settings - Fork 0
/
svm.py
34 lines (32 loc) · 804 Bytes
/
svm.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
import arff
import numpy as np
from sklearn import svm
import os
from sklearn.model_selection import train_test_split
files = []
data_path = "MDP\\D''\\"
for f in os.listdir(data_path):
f_path = os.path.join(data_path, f)
files.append(f_path)
for f in files:
print(f)
a = np.array(list(arff.load(f)))
size = a.shape
t = int(0.8 * size[0])
x = a[:t, :-1]
y = a[:t, -1]
print(' Training......')
clf = svm.SVC(kernel='linear')
clf.fit(x, y)
print(' Done......')
correct = 0
total = 0
for r in range(t, size[0]):
x = a[r][:-1]
y = a[r][-1]
res = clf.predict([x])
if res[0]==y:
correct += 1
total += 1
ans = (correct/total)*100
print(' '+str(ans)+'% accurate prediction')