-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpca_class.py
1402 lines (1113 loc) · 58.9 KB
/
pca_class.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -----------------------------------------------------------------------------
#
# This script a class that can be used for principal component analysis
#
# Author: Max Melching
# Source: https://github.com/MaxMelching/bachelor_project
#
# -----------------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
# This last import is from another script that was used for the thesis.
# If this is not available, removing jsd from this script is easy and
# does not change the functionality of it severely.
from criteria import jsd
def create_datamatrix(data: any, names: any = None) -> np.array:
"""
Creates a matrix where each column stores repeated measurements in
`data` of every parameter in `names`.
Remark: this function was created to work with data from
Gravitational-Wave posterior probability distributions published
by the LIGO-Virgo-Kagra collaboration. However, it should also work
with other data fulfilling the conditions described below.
Parameters:
- data (2D array-like): dataset where values of measurements are
stored. Has to be of a type which allows access to subsets
labeled with parameters par via data[par].
- names (array-like, optional, default = None): set of
parameters, order determines order of columns in output.
If None, every parameter from data is taken.
Returns:
- numpy-array with columns equal to data[name] for each name in
names.
"""
if names is None:
names = np.array([data.dtype.names]) # take every parameter
else:
names = np.array(names)
return np.array([data[name] for name in names]).T # maybe = np.array(data[names]).T
class pca:
def __init__(self, X: any, basis: any = None, names: any = None,
normed: bool = False, centered: bool = False):
"""
Computes the Principal Components (PCs) of input data and
provides additional functions to return (co-)variances,
eigenvectors and more.
Parameters:
----------
- X (array-like): matrix containing dataset to work with.
Columns represent different parameters, rows measurements.
- basis (array-like, optional, default = None): data which
is used to compute the transformation matrix (eigenvectors
of the corresponding covariance matrix). Must have the
same number of columns as X.
If basis = None, it is set to X and the transformed X
contains the Principal Components.
- names (array-like, default = None): contains strings with
labels of the parameters for the columns of X. Is only
used in plots, so it can be None when no plots are used
(otherwise, there will be an error).
- normed (boolean, optional, default = False): if True, the
data in X is normalized by dividing the values for
each parameter by the respective standard deviation.
- centered (boolean, optional, default = False): if True,
the data in X is normalized by dividing the values for
each parameter by the respective standard deviation.
Attributes:
----------
- X (numpy-array): copy of input X, which might be changed
depending on the values of normed, centered.
- basis (numpy-array): copy of input basis, which might be
changed depending on the values of normed, centered. If
the basis is None (default), it is set to X.
- m (integer): number of rows in X.
- n (integer): number of columns in X.
- mb (integer): number of rows in basis.
- nb (integer): number of columns in basis.
- evals (numpy-array): eigenvalues of covariance matrix of
basis (not necessarily X).
- A (numpy-array): eigenvectors of covariance matrix of
basis, arranged in a matrix.
- Z (numpy-array): matrix product of X and A (transformed
data). Equal to the PCs of X if basis = X.
- Zbasis (numpy-array): matrix product of basis and A. Equal
to Z if basis = X.
- names (numpy-array): copy of input names.
"""
# Create local variables
self.X = np.array(X)
if basis is None:
self.basis = np.array(X)
else:
self.basis = np.array(basis)
# Store dimensions
self.m, self.n = self.X.shape
self.mb, self.nb = self.basis.shape
if centered:
# Subtract row vector mean from every row of X. That does
# not change variances of data/PCs, only shifts mean to zero
self.X -= np.mean(self.X, axis=0)
self.basis -= np.mean(self.basis, axis=0)
if normed:
# Divide each row by its standard deviation
self.X /= np.std(self.basis, axis=0)
self.basis /= np.std(self.basis, axis=0)
self.S = np.corrcoef(self.basis, rowvar=False)
# np.cov would do the same because basis is standardized,
# but this way we make sure and it is more obvious
else:
self.S = np.cov(self.basis, rowvar=False)
# = 1 / (self.m - 1) * np.dot(self.basis.T, self.basis)
# Diagonalise covariance matrix
self.evals, self.A = np.linalg.eigh(self.S)
# Sort eigenvalues and -vectors in descending order
perm = np.flip(self.evals.argsort()) # argsort gives ascending
self.evals = self.evals[perm]
self.A = self.A[:, perm]
# Compute transformed data
self.Z = np.dot(self.X, self.A)
self.Zbasis = np.dot(self.basis, self.A)
self.names = np.array(names)
# Permuting them like evals, A is not necessary as only
# order of eigenvectors is (potentially) switched, but not the
# components in them (which names correspond to).
def get_X(self, q: int = None) -> np.array:
"""
Returns the data matrix stored as a local variable. May differ
from input X due to normalization and/or centering.
Parameters:
----------
- q (int, optional, default = None): number of columns to
return. If None, all of them are taken.
Returns:
-------
- first q columns of local variable X.
"""
if q is None:
q = self.n
return self.X[:, :q]
def get_basis(self, q: int = None) -> np.array:
"""
Returns a certain number of columns of the basis matrix stored
as a local variable. These may differ from input basis due to
normalization and/ or centering.
Parameters:
----------
- q (int, optional, default = None): number of columns to
return. If None, all of them are taken.
Returns:
-------
- first q columns of local variable basis.
"""
if q is None:
q = self.n
return self.basis[:, :q]
def get_PCs(self, q: int = None) -> np.array:
"""
Returns a certain number of columns of the transformed data
matrix stored as a local variable, which contains the PCs of X
if basis = X.
Parameters:
----------
- q (int, optional, default = None): number of columns to
return. If None, all of them are taken.
Returns:
-------
- first q columns of local variable Z.
"""
if q is None:
q = self.n
return self.Z[:, :q] # = np.dot(X, A[:, :q])
def get_basisPCs(self, q: int = None) -> np.array:
"""
Returns a certain number of columns of the transformed basis
matrix used in the function, which contains the PCs of basis.
Parameters:
----------
- q (int, optional, default = None): number of columns to
return. If None, all of them are taken.
Returns:
-------
- first q columns of local variable Zbasis.
"""
if q is None:
q = self.n
return self.Zbasis[:, :q]
def get_cov(self, q: int = None, display: bool = False,
savepath: str = None) -> np.array | None:
"""
Returns the covariance matrix of the first q columns of basis
either as a 2D numpy-array or as a heatmap.
Parameters:
----------
- q (int, optional, default = None): number of eigenvalues
to return. If None, all of them are taken.
- display (boolean, optional, default = False): determines
if the function returns a numpy-array (which can be used
for storing the matrix) or if it is displayed.
- savepath (string, optional, default = None): if display is
True and savepath is not None, the plot generated will be
saved. The filename is either given in saveplot (when a
fileformat is contained in savepath) or an automatic one
is created and saved to the path given by savepath (to
save it into the same directory, set savepath = '').
Returns:
-------
- first q columns of local variable S if display = True,
else None.
"""
if q is None:
q = self.n
if not display:
return self.S[:q, :q]
else:
cov_df = pd.DataFrame(
np.round(self.S[:q, :q], 2),
columns=self.names[:q],
index = self.names[:q]
)
sns.heatmap(cov_df.abs(), annot=cov_df, fmt='g')
if savepath:
# If path already contains a name + data format, this
# name is taken. Otherwise, an automatic one is created.
if '.' in savepath[-4:]:
plt.savefig(savepath, bbox_inches='tight')
else:
name = f'covmatrix_{self.names}'
# if limits:
# name += f'_limits{limits}'
plt.savefig(savepath + name + '.pdf', bbox_inches='tight')
plt.show()
def get_transformedcov(self, q: int = None, display: bool = False,
savepath: str = None) -> np.array | None:
"""
Computes the diagonalized covariance matrix of the first q
columns of basis, i.e. the matrix product (A^T)SA. Returned
either as a 2D numpy-array or as a heatmap.
Parameters:
----------
- q (int, optional, default = None): number of eigenvalues
to return. If None, all of them are taken.
- display (boolean, optional, default = False): determines
if the function returns a numpy-array (which can be used
for storing the matrix) or if it is displayed.
- savepath (string, optional, default = None): if display is
True and savepath is not None, the plot generated will be
saved. The filename is either given in saveplot (when a
fileformat is contained in savepath) or an automatic one
is created and saved to the path given by savepath (to
save it into the same directory, set savepath = '').
Returns:
-------
- first q columns of first q columns of diagonalized
covariance matrix of basis if display = True, else None.
"""
if q is None:
q = self.n
Sa = np.dot(self.A.T, np.dot(self.S, self.A))
if not display:
return Sa
else:
cov_df = pd.DataFrame(
np.round(Sa[:q, :q], 2),
columns=self.names[:q],
index=self.names[:q]
)
sns.heatmap(cov_df.abs(), annot=cov_df, fmt='g')
# Limits 0 and 1 could be chosen for normed = True
if savepath:
# If path already contains a name + data format, this
# name is taken. Otherwise, an automatic one is created.
if '.' in savepath[-4:]:
plt.savefig(savepath, bbox_inches='tight')
else:
name = f'transformed_covariancematrix{self.names}'
# if limits:
# name += f'_limits{limits}'
plt.savefig(savepath + name + '.pdf', bbox_inches='tight')
plt.show()
def get_eigenvalues(self, q: int = None) -> np.array | None:
"""
Returns a certain number of eigenvalues of the covariance matrix
of basis in descending order.
Parameters:
----------
- q (int, optional, default = None): number of eigenvalues
to return. If None, all of them are taken.
Returns:
-------
- first q columns of local variable evals.
"""
if q is None:
q = self.n
return self.evals[:q] # = np.var(self.Zbasis[:, :q], axis=0)
def get_transformedvariances(self, q: int = None, display: bool = False,
savepath: str = None) -> np.array | None:
"""
Returns a certain number of variances of parameters in Z, Zbasis.
Parameters:
----------
- q (int, optional, default = None): number of eigenvalues
to return. If None, all of them are taken.
- display (boolean, optional, default = False): determines
if the function returns a numpy-array (which can be used
for storing the matrix) or if it is displayed.
- savepath (string, optional, default = None): if display is
True and savepath is not None, the plot generated will be
saved. The filename is either given in saveplot (when a
fileformat is contained in savepath) or an automatic one
is created and saved to the path given by savepath (to
save it into the same directory, set savepath = '').
Returns:
-------
- matrix of first q variances of Z, Zbasis in each row if
display = True, else None.
"""
if q is None:
q = self.n
zvar = np.var(self.Z[:, :q], axis=0)
if not display:
return [zvar, self.evals]
else:
pcconvert1 = 1 / np.sum(self.evals) * 100 # conversion factor; will be used multiple times, thus define here
pcconvert2 = 1 / np.sum(zvar) * 100 # not necessarily equal to pcconvert1 since X, basis may have different total variance
percents = [f'{i + 1} ({self.evals[i] * pcconvert1: .2f}% vs. {zvar[i] * pcconvert2: .2f}% of variance)' for i in range(q)]
df = pd.DataFrame(np.array([np.round(zvar, 2), np.round(self.evals[:q], 2)]).T, columns = ['Variances of Z', 'Variances of Zbasis'], index = percents)
sns.heatmap(df, vmin = 0, annot = True, fmt = 'g')
#plt.xlabel('number') # kind of unnecessary? Should be clear
plt.ylabel('Components')
if savepath:
# If path already contains a name + data format, this
# name is taken. Otherwise, an automatic one is created.
if '.' in savepath[-4:]:
plt.savefig(savepath, bbox_inches='tight')
else:
name = f'transformed_variances_{self.names}'
# if limits:
# name += f'_limits{limits}'
plt.savefig(savepath + name + '.pdf', bbox_inches='tight')
plt.show()
def get_retainedvariance(self, q: int = None) -> float:
"""
Calculates the percentage of variance retained by a certain
number of eigenvalues.
Parameters:
----------
- q (int, optional, default = None): number of eigenvalues
to take. If None, all of them are taken.
Returns:
-------
- sum of the first q eigenvalues divided by 100 to give a
percentage.
"""
if q is None:
q = self.n
return np.sum(self.evals[:q]) / np.sum(self.evals) * 100
def get_eigenvectors(self, q: int = None, display: bool = False,
savepath: str = None) -> np.array | None:
"""
Returns a certain number of eigenvectors of S.
Parameters:
----------
- q (int, optional, default = None): number of eigenvectors
to return. If None, all of them are taken.
- display (boolean, optional, default = False): determines
if the function returns a numpy-array (which can be used
for storing the matrix) or if it is displayed.
- savepath (string, optional, default = None): if display is
True and savepath is not None, the plot generated will be
saved. The filename is either given in saveplot (when a
fileformat is contained in savepath) or an automatic one
is created and saved to the path given by savepath (to
save it into the same directory, set savepath = '').
Returns:
-------
- matrix of first q eigenvectors of basis if display = True,
else None.
"""
if q is None:
q = self.n
# shows components with respect to basis of parameters, so the first component
# of the first eigenvector is the 'contribution' of parameter 1
if not display:
return self.A[:, :q]
else:
pcconvert = 1 / np.sum(self.evals) * 100 # conversion factor; will be used multiple times, thus defined here
percents = [f'{i + 1} ({self.evals[i] * pcconvert: .2f}% of variance)' for i in range(q)]
eigenvec_df = pd.DataFrame(np.round(self.A[:, :q].T, 2), columns = self.names, index = percents)
# no .T and change columns, index? -> nah, looks weird with components written downwards
sns.heatmap(eigenvec_df.abs(), vmin = 0, vmax = 1, annot = eigenvec_df, fmt = 'g', linewidths = 0.5)
# cmap is based on color palette rocket from seaborn (default)
# like this, color is based on abs and annot writes real values including minus signs into the cells
plt.title(f'The first {q} eigenvalues account for {np.sum(self.evals[0:q]) / np.sum(self.evals) * 100: .2f}% of the variance')
plt.xlabel('Parameters')
plt.ylabel('Eigenvectors')
# maybe divide each vector (= row) by its maximum element? Then, relative influence can be seen better, right?
if savepath:
# If path already contains a name + data format, this
# name is taken. Otherwise, an automatic one is created.
if '.' in savepath[-4:]:
plt.savefig(savepath, bbox_inches='tight')
else:
name = f'eigenvectors_{self.names}'
# if limits:
# name += f'_limits{limits}'
plt.savefig(savepath + name + '.pdf', bbox_inches='tight')
plt.show()
def get_weightedeigenvectors(self, q: int = None, display: bool = False,
savepath: str = None) -> np.array | None:
"""
Computes the weighted versions of eigenvectors of S where
weights are the square root of the respective eigenvalue. This
visualizes the significance of the eigenvector in a convenient
manner and is also called correlation.
Parameters:
----------
- q (int, optional, default = None): number of eigenvectors
to return. If None, all of them are taken.
- display (boolean, optional, default = False): determines
if the function returns a numpy-array (which can be used
for storing the matrix) or if it is displayed.
- savepath (string, optional, default = None): if display is
True and savepath is not None, the plot generated will be
saved. The filename is either given in saveplot (when a
fileformat is contained in savepath) or an automatic one
is created and saved to the path given by savepath (to
save it into the same directory, set savepath = '').
Returns:
-------
- matrix of first q weighted eigenvectors of basis if
display = True, else None.
"""
#corr = self.A * np.sqrt(self.evals) / np.std(self.X, axis = 0) # std is 1 for normed = True
#corr = self.A * np.sqrt(np.var(self.Z, axis = 0)) / np.std(self.X, axis = 0)
# although not strict from definition -> divide by std(Z)??? Will be 1 for our case, so doesn't matter for now
corr = self.A[:, :q] * np.sqrt(self.evals[:q])
if not display:
return corr
else:
PCs = [f'PC {i}' for i in range(1, self.n + 1)]
corr_df = pd.DataFrame(np.round(corr.T, 2), columns = self.names, index = PCs)
sns.heatmap(corr_df.abs(), vmin = 0, annot = corr_df, fmt = 'g', linewidths=0.5)
plt.title('Correlation between parameters and PCs')
plt.xlabel('Parameters')
plt.ylabel('PCs')
if savepath:
# If path already contains a name + data format, this
# name is taken. Otherwise, an automatic one is created.
if '.' in savepath[-4:]:
plt.savefig(savepath, bbox_inches='tight')
else:
name = f'weighted_eigenvectors_{self.names}'
# if limits:
# name += f'_limits{limits}'
plt.savefig(savepath + name + '.pdf', bbox_inches='tight')
plt.show()
def get_eigenvectors_standarderror(self, q: int = None, display = False,
savepath: str = None) -> np.array | None:
"""
Calculates the error for each component of a certain amount of
eigenvectors of S based on a formula taken from "A User's Guide
to Principal Component Analysis" by J.E. Jackson.
Parameters:
----------
- q (int, optional, default = None): number of eigenvectors
to return. If None, all of them are taken.
- display (boolean, optional, default = False): determines
if the function returns a numpy-array (which can be used
for storing the matrix) or if it is displayed.
- savepath (string, optional, default = None): if display is
True and savepath is not None, the plot generated will be
saved. The filename is either given in saveplot (when a
fileformat is contained in savepath) or an automatic one
is created and saved to the path given by savepath (to
save it into the same directory, set savepath = '').
Returns:
-------
- matrix of the error of the first q eigenvectors of basis
if display = True, else None.
"""
if not q:
q = self.n
m = self.basis.shape[0]
# it is correct to take the eigenvalues here as they are obtained from self.basis (which is what we want)
errors = np.sqrt([[self.evals[i] / m * np.sum([self.evals[h] / (self.evals[h] - self.evals[i])**2 * self.A[g, h]**2 for h in range(self.n) if h != i]) for i in range(self.n)] for g in range(self.n)])
#np.sqrt(self.evals / self.m * np.sum(self.evals * self.A, axis = 0))
if not display:
return errors[:, :q]
else:
pcconvert = 1 / np.sum(self.evals) * 100 # conversion factor; will be used multiple times, thus define here
percents = [f'{i + 1} ({self.evals[i] * pcconvert: .2f}% of variance)' for i in range(q)]
eigenvec_df = pd.DataFrame(np.round(errors[:, :q].T, 2), columns = self.names, index = percents)
# no .T and change columns, index? -> nah, looks weird with components written downwards
# maybe divide each vector (= row) by its maximum element? Then, relative influence can be seen better, right?
sns.heatmap(eigenvec_df.abs(), vmin = 0, vmax = 1, annot = eigenvec_df, fmt = 'g', linewidths = 0.5)
# cmap is based on color palette rocket from seaborn (default)
# like this, color is based on abs and annot writes real values including minus signs into the cells
#plt.title(f'The first {q} eigenvalues account for {np.sum(self.evals[0:q]) / np.sum(self.evals) * 100: .2f}% of the variance')
plt.title(f'Standard error of the first {q} eigenvectors')
plt.xlabel('Parameters')
plt.ylabel('Eigenvectors')
if savepath:
# If path already contains a name + data format, this
# name is taken. Otherwise, an automatic one is created.
if '.' in savepath[-4:]:
plt.savefig(savepath, bbox_inches='tight')
else:
name = f'eigenvectors_standarderror_{self.names}'
# if limits:
# name += f'_limits{limits}'
plt.savefig(savepath + name + '.pdf', bbox_inches='tight')
plt.show()
def get_transformedmeans(self, q: int = None, display: bool = False,
savepath: str = None) -> np.array | None:
"""
Returns a certain number of means of parameters in Z, Zbasis.
Parameters:
----------
- q (int, optional, default = None): number of eigenvalues
to return. If None, all of them are taken.
- display (boolean, optional, default = False): determines
if the function returns a numpy-array (which can be used
for storing the matrix) or if it is displayed.
- savepath (string, optional, default = None): if display is
True and savepath is not None, the plot generated will be
saved. The filename is either given in saveplot (when a
fileformat is contained in savepath) or an automatic one
is created and saved to the path given by savepath (to
save it into the same directory, set savepath = '').
Returns:
-------
- matrix of first q variances of Z, Zbasis in each row if
display = True, else None.
"""
if q is None:
q = self.n
means = [np.mean(self.Z[:, :q], axis=0), np.mean(self.Zbasis[:, :q], axis=0)]
if not display:
return means
else:
df = pd.DataFrame(np.round(means, 2).T, columns = ['Means of Z', 'Means of Zbasis'], index = [f'{i + 1}' for i in range(q)])
sns.heatmap(df.abs(), annot = df, fmt = 'g')
plt.ylabel('Components')
if savepath:
# If path already contains a name + data format, this
# name is taken. Otherwise, an automatic one is created.
if '.' in savepath[-4:]:
plt.savefig(savepath, bbox_inches='tight')
else:
name = f'transformed_means_{self.names}'
# if limits:
# name += f'_limits{limits}'
plt.savefig(savepath + name + '.pdf', bbox_inches='tight')
plt.show()
def get_originalmeans(self, q: int = None, display: bool = False,
savepath: str = None) -> np.array | None:
"""
Returns a certain number of means of parameters in X, basis.
Parameters:
----------
- q (int, optional, default = None): number of eigenvalues
to return. If None, all of them are taken.
- display (boolean, optional, default = False): determines
if the function returns a numpy-array (which can be used
for storing the matrix) or if it is displayed.
- savepath (string, optional, default = None): if display is
True and savepath is not None, the plot generated will be
saved. The filename is either given in saveplot (when a
fileformat is contained in savepath) or an automatic one
is created and saved to the path given by savepath (to
save it into the same directory, set savepath = '').
Returns:
-------
- matrix of first q variances of X, basis in each row if
display = True, else None.
"""
if q is None:
q = self.n
means = [np.mean(self.X[:, :q], axis=0), np.mean(self.basis[:, :q], axis=0)]
if not display:
return means
else:
df = pd.DataFrame(np.round(means, 2).T, columns = ['Means of X', 'Means of basis'], index = self.names)
sns.heatmap(df.abs(), annot = df, fmt = 'g')
plt.ylabel('Parameters')
if savepath:
# If path already contains a name + data format, this
# name is taken. Otherwise, an automatic one is created.
if '.' in savepath[-4:]:
plt.savefig(savepath, bbox_inches='tight')
else:
name = f'original_variances_{self.names}'
# if limits:
# name += f'_limits{limits}'
plt.savefig(savepath + name + '.pdf', bbox_inches='tight')
plt.show()
def get_originalvariances(self, q: int = None, display: bool = False,
savepath: str = None) -> np.array | None:
"""
Returns a certain number of variances of parameters in X, basis.
Parameters:
----------
- q (int, optional, default = None): number of eigenvalues
to return. If None, all of them are taken.
- display (boolean, optional, default = False): determines
if the function returns a numpy-array (which can be used
for storing the matrix) or if it is displayed.
- savepath (string, optional, default = None): if display is
True and savepath is not None, the plot generated will be
saved. The filename is either given in saveplot (when a
fileformat is contained in savepath) or an automatic one
is created and saved to the path given by savepath (to
save it into the same directory, set savepath = '').
Returns:
-------
- matrix of first q variances of X, basis in each row if
display = True, else None.
"""
if q is None:
q = self.n
xvar = np.var(self.X[:, :q], axis = 0)
basisvar = np.var(self.basis[:, :q], axis = 0)
if not display:
return [xvar, basisvar]
else:
pcconvert1 = 1 / np.sum(basisvar) * 100 # conversion factor; will be used multiple times, thus define here
pcconvert2 = 1 / np.sum(xvar) * 100 # not necessarily equal to pcconvert1 since X, basis may have different total variance
percents = [f'{self.names[i]} ({basisvar[i] * pcconvert1: .2f}% vs. {xvar[i] * pcconvert2: .2f}% of variance)' for i in range(q)]
df = pd.DataFrame(np.array([np.round(xvar, 2), np.round(basisvar[:q], 2)]).T, columns = ['Variances of X', 'Variances of basis'], index = percents)
sns.heatmap(df, vmin = 0, annot = True, fmt = 'g')
#plt.xlabel('number') # kind of unnecessary? Should be clear
plt.ylabel('Parameters')
if savepath:
# If path already contains a name + data format, this
# name is taken. Otherwise, an automatic one is created.
if '.' in savepath[-4:]:
plt.savefig(savepath, bbox_inches='tight')
else:
name = f'original_variances_{self.names}'
# if limits:
# name += f'_limits{limits}'
plt.savefig(savepath + name + '.pdf', bbox_inches='tight')
plt.show()
# next: some functions which are specifically for PCA, mainly for selecting relevant PCs
def total_variance(self, t: float) -> int:
"""
Computes the number of PCs necessary to keep a certain
percentage of the total variance for the transformed basis.
Parameters:
----------
- t (float): threshold of percentage to be kept. Has to be
0 <= t <= 1.
Returns:
-------
- q (integer): minimal number of variables to keep the
percentage t of the total variance.
"""
retained_var = 0
q = 1
total_var = np.sum(self.evals)
for i in range(self.n):
retained_var += self.evals[i] / total_var
if retained_var >= t:
break
else:
q += 1
return q
def scree_plot(self, q: int = None, savepath: str = None) -> None:
"""
Creates a scree plot for the variances of a certain amount of
the transformed variables.
Parameters:
----------
- q (int, optional, default = None): number of variables
to return. If None, all of them are taken.
- savepath (string, optional, default = None): if it is not
None, the plot generated will be saved. The filename is
either given in saveplot (when a fileformat is contained
in savepath) or an automatic one is created and saved to
the path given by savepath (to save it into the same
directory, set savepath = '').
Returns:
-------
- None
"""
if q is None:
q = self.n
x = np.arange(1, self.n + 1, 1)
plt.plot(x, self.evals, marker='x', markersize=10, markeredgecolor='r')
#plt.bar(x, height = self.evals, width = 0.5)
plt.ylabel('Eigenvalue')
plt.xlabel('Component')
plt.ylim(0, 1.2 * self.evals[0])
plt.xticks(x)
plt.grid(True)
for i, eval in enumerate(self.evals): # indices and values are needed
plt.text(i + 1, 1.05 * eval + 0.05, np.round(eval, 2), ha='center') # for bar: second to '1.05 * eval'
# +1 because i starts at 0 instead of 1; ha = horizontalalignment
if savepath:
# If path already contains a name + data format, this name
# is taken. Otherwise, an automatic one is created.
if '.' in savepath[-4:]:
plt.savefig(savepath, bbox_inches='tight')
else:
name = f'screeplot_{q}PCs_{self.names}'
# if limits:
# name += f'_limits{limits}'
plt.savefig(savepath + name + '.pdf', bbox_inches='tight')
plt.show()
# the following ones are showing both variances, but is that really what we want?
# Criteria are specifically about PCs...
# x = np.arange(1, q + 1, 1)
# zvar = np.array(np.var(self.Z[:, :q], axis = 0))
# plt.plot(x, self.evals[:q], marker = 'x', markersize = 10, markeredgecolor = 'r', label = 'eigenvalue')
# plt.plot(x, zvar, marker = 'x', markersize = 10, markeredgecolor = 'g', label = 'variance')
# plt.ylabel('value')
# plt.xlabel('variable number')
# plt.ylim(0, 1.2 * self.evals[0])
# plt.xticks(x)
# plt.grid(True)
# plt.legend(loc = 'upper right')
# for i, eval in enumerate(self.evals[:q]): # indices and values are needed
# plt.text(i + 1, eval + 0.1, np.round(eval, 2), ha = 'center') # for bar: second to '1.05 * eval'
# # +1 because i starts at 0 instead of 1; ha = horizontalalignment
# plt.show()
# x = np.arange(1, q + 1, 1)
# zvar = np.array(np.var(self.Z[:, :q], axis = 0))
# fig, axs = plt.subplots(ncols = 2, sharex = True, sharey = True, figsize = (16, 5))
# axs[0].plot(x, zvar, marker = 'x', markersize = 10, markeredgecolor = 'g')
# axs[1].plot(x, self.evals[:q], marker = 'x', markersize = 10, markeredgecolor = 'r')
# axs[0].set_ylabel('Variance')
# axs[1].set_ylabel('Variance')
# axs[0].set_xlabel('Variable number')
# axs[1].set_xlabel('Variable number')
# axs[0].set_ylim(0, 1.2 * max(zvar[0], self.evals[0]))
# axs[0].set_xticks(x)
# # axs[0].grid(True)
# # axs[1].grid(True)
# plt.grid(True)
# for i, var in enumerate(zvar[:q]): # indices and values are needed
# axs[0].text(i + 1, var + 0.1, np.round(var, 2), ha = 'center') # for bar: second to '1.05 * eval'
# # +1 because i starts at 0 instead of 1; ha = horizontalalignment
# for i, eval in enumerate(self.evals[:q]): # indices and values are needed
# axs[1].text(i + 1, eval + 0.1, np.round(eval, 2), ha = 'center') # for bar: second to '1.05 * eval'
# # +1 because i starts at 0 instead of 1; ha = horizontalalignment
# plt.show()
def kaiser(self) -> int:
"""
Computes the number of relevant PCs based on Kaiser's rule.
Parameters:
----------
- None
Returns:
-------
- q (integer): number of PCs with variance greater than 0.7.
This is in accordance with what I.T. Jolliffe argues in
his book "Principal Component Analysis".
"""
eigenvecs = self.A.T[np.where(self.evals > 0.7)]
q = eigenvecs.shape[0] # [1] would also be ok, is quadratic
return q
def broken_stick(self) -> np.array:
"""
Computes which PCs should be kept based on the broken stick
model approach.
Parameters:
----------
- None
Returns:
-------
- indices (numpy-array): mask for array-likes showing which
PCs should be kept. Can be used as A[:, indices] to get
the "important" eigenvectors.
"""
#eigenvecs = np.array([self.A[:, i] for i in range(self.n) if (self.evals[i] > np.sum([1 / j for j in range(i, self.n + 1)]) / self.n)]).T
indices = np.array([i - 1 for i in range(1, self.n + 1) if (self.evals[i - 1] >= np.sum([1 / j for j in range(i, self.n + 1)]) / self.n)])
return indices
# does not work yet, therefore commented
# def t2_check(self, t): # t = threshold, given as percentile
# Znorm = self.Z # / self.evals
# t2vals = np.sum(Znorm**2, axis = 1) # sums through rows, gives T^2-value for every measurement
# t2threshold = self.n * (self.m - 1) / (self.m - self.n) * f.ppf(t, self.n, self.m - self.n)
# # formula from Jackson, equation 1.7.5; ppf directly gives the point, when 95% of pdf are reached
# t2filtered = t2vals[np.where(t2vals <= t2threshold)]
# return [t2threshold, t2filtered.size, t2vals.size] # needs adjustment for normed case, way too manyy are discarded
# # -> not dividing by self.evals is better, but does that make sense?
# # -> I don't think so, because although X has normalised variance, Z doesn't (eigenvalues != 1)
# lastly: functions to plot parts of the data
def compare_plot(self, k: int, savepath: str = None) -> None:
"""
Compares the histograms of the k-th variable from X and basis.
Parameters:
----------
- k (integer): index of variable (column) to take.
- savepath (string, optional, default = None): if it is not