-
Notifications
You must be signed in to change notification settings - Fork 0
/
lpnorm.py
executable file
·70 lines (53 loc) · 1.83 KB
/
lpnorm.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
'''
Written by Naveen Venkat
Final Year Undergraduate Student, Dept. of CSIS
Birla Institute of Technology and Science, Pilani
www.naveenvenkat.com | [email protected]
'''
import data
from time import time
from tqdm import tqdm
import numpy as np
def runLpNormComparison(n, d, plist, plot=True):
DS = data.getRandomData(n, d)
dists = []
times = []
P1 = DS[0]
rest = DS[1:]
for p in tqdm(plist):
t1 = time()
dist = data.getLpNorm(rest - P1, p, axis=1)
t2 = time()
times.append(t2 - t1)
dists.append( np.std(dist) )
if(plot):
# Display standard deviation results
data.plt.figure('Standard deviation of distance')
data.plt.xlabel('order of norm')
data.plt.ylabel('standard deviation of distance')
data.plt.plot(plist, dists)
# Display running time results
data.plt.figure('Running time for distance')
data.plt.xlabel('order of norm')
data.plt.ylabel('running time for distance calculation (seconds)')
data.plt.plot(plist, times)
data.plt.show()
return (plist, dists, times)
if __name__=='__main__':
plotList = []
dims = [10, 20, 30, 40]
plist = [i/10 for i in range(10, 101, 1)]
plothandles = []
for d in dims:
plotList.append( runLpNormComparison(1000, d, plist, plot=False) )
for i in range(len(plotList)):
(plist, dists, times) = plotList[i]
d = dims[i]
# Display standard deviation results
data.plt.figure('Standard deviation of distance')
data.plt.xlabel('order of norm')
data.plt.ylabel('standard deviation of distance')
x, = data.plt.plot(plist, dists, label= str(d) + ' dimensions')
plothandles.append(x)
data.plt.legend(handles=plothandles)
data.plt.show()