-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1NNDTW.py
More file actions
65 lines (53 loc) · 1.71 KB
/
1NNDTW.py
File metadata and controls
65 lines (53 loc) · 1.71 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
55
56
57
58
59
60
61
62
63
64
import numpy as np
#import math
import os
import psutil
import time
#from dtaidistance import dtw
#from tslearn.metrics import dtw_limited_warping_length
import sys
def dtw(A, B):
N = len(A)
M = len(B)
d = np.zeros((N, M))
for n in range(N):
for m in range(M):
d[n][m]=(A[n]-B[m])**2
D=np.zeros(d.shape)
D[0][0]=d[0][0]
for n in range(1,N):
D[n][0]=d[n][0]+D[n-1][0]
for m in range(1,M):
D[0][m]=d[0][m]+D[0][m-1]
for n in range(1,N):
for m in range(1,M):
D[n][m]=d[n][m]+min(D[n-1][m],D[n-1][m-1],D[n][m-1])
return D[N-1][M-1]
training = np.loadtxt(sys.argv[1], delimiter='\t')
testing = np.loadtxt(sys.argv[2], delimiter='\t')
num_rows_test, num_columns_test = testing.shape
num_rows_train, num_columns_train = training.shape
training_noclass=training[:,1:]
testing_noclass=testing[:,1:]
predicted_label = None
correct = 0
start_time = time.time()
for i in range(num_rows_test):
#print(i)
least_distance = float('inf')
for j in range(num_rows_train):
dist = dtw(testing_noclass[i], training_noclass[j])
if dist < least_distance:
predicted_label = training[j][0]
least_distance = dist
if predicted_label == testing[i][0]:
correct = correct + 1
accuracy = (correct/num_rows_test) * 100
print("Datasetname:",sys.argv[1])
print("Total Accuracy of oneNNDTW is:", accuracy)
print("Total Execution time oneNNDTW is:", time.time() - start_time)
process = psutil.Process(os.getpid())
memory = process.memory_full_info().uss
memory_in_KB = memory /(1024)
print("Total Memory of oneNNDTW inKB",memory_in_KB) # in bytes
#dist = dtw.distance(testing[i], training[j]) # math.sqrt(squaring)