-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain2.py
More file actions
78 lines (62 loc) · 2.18 KB
/
main2.py
File metadata and controls
78 lines (62 loc) · 2.18 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import random
import numpy as np
import math
import matplotlib.pyplot as plt
from functions import *
PRIMES_TO_READ = ['1','2','3','4','5'] # define the file to read to load the list with prime numbers
DATA_PATH = './data/primes%s.txt'
STORAGE_PATH = './results/E2-%s.eps'
START_NUMBER_IND = 0
END_NUMBER_IND = 1000
CARDINALITY = 500 #-> Define how many concecutive primes to consider for the group
#
# 1. read ALL the primes
#
numbsALL = []
for i in range(0,len(PRIMES_TO_READ)):
p = DATA_PATH % (PRIMES_TO_READ[i])
n = np.genfromtxt(fname=p,skip_header=4)
numbsALL = np.concatenate((numbsALL, np.ndarray.flatten(n)))
#
# 2. Knee thresholding
# Repeat multiple times - Extract number of significant eigenvectors
M = END_NUMBER_IND - START_NUMBER_IND
number_of_principal_axis = np.zeros((M,1))
preserved_variance = np.zeros((M,1))
for n in range(START_NUMBER_IND,END_NUMBER_IND):
# keep fraction of numbers
numbs = numbsALL[n:n + CARDINALITY]
print('ITERATION %5d/%5d - %4d Numbers selected...' % (n+1, M, CARDINALITY))
# compute correlation
K = len(numbs)
s = np.zeros((K,K))
for i in range(0,K):
for j in range(i+1,K):
d = numbs[i]-numbs[j]
s[i,j] = d * d
s[j,i] = s[i,j]
if n == 0 or n%100 == 0:
#show the distance matrix every 100 iterations + the first (-> make sure at least one image is produced)
plt.imshow(s)
plt.savefig(STORAGE_PATH%('-cov-' + str(n+1)))
plt.close()
# eigenvalues
[v,e] = np.linalg.eig(s)
vv = np.abs(v)
# knee thresolding
number_of_principal_axis[n] = kneeThresholding(vv)
preserved_variance[n] = math.floor(100*np.sum(vv[:int(number_of_principal_axis[n])])/np.sum(vv))
#
# 3. plot result
#
xind = list(range(1,M+1))
fig, axs = plt.subplots(2)
fig.suptitle(('Summary of Experiments [Start:%d, End:%d, Cardinality:%d]')%(numbsALL[START_NUMBER_IND],numbsALL[END_NUMBER_IND], CARDINALITY))
axs[0].plot(xind, number_of_principal_axis,'-')
axs[0].set_ylabel('# of clusters')
axs[0].set_yticks([3, 4, 5])
axs[1].plot(xind,preserved_variance,'-')
axs[1].set_ylabel('% of var')
axs[1].set_xlabel('Group')
plt.savefig(STORAGE_PATH%(M))
plt.close()