#!/usr/bin/env python3
"``naginterfaces.library.mv.prin_comp`` Python Example."
# NAG Copyright 2018-2019.
# pylint: disable=invalid-name
from naginterfaces.library import mv
[docs]def main():
"""
Example for :func:`naginterfaces.library.mv.prin_comp`.
Perform a principal component analysis on a data matrix; both the principal
component loadings and the principal component scores are returned.
>>> main()
naginterfaces.library.mv.prin_comp Python Example Results.
Perform an unweighted principal component analysis on a dataset
from Cooley and Lohnes (1971). The statistics of the principal
component analysis are:
Eigenvalues Percentage Cumulative Chisq DF Sig
variation variation
[
8.2739, 0.6515, 0.6515, 8.6127, 5.0000, 0.1255
3.6761, 0.2895, 0.9410, 4.1183, 2.0000, 0.1276
0.7499, 0.0590, 1.0000, 0.0000, 0.0000, 0.0000
]
"""
print('naginterfaces.library.mv.prin_comp Python Example Results.')
print('Perform an unweighted principal component analysis on a dataset')
print('from Cooley and Lohnes (1971). The statistics of the principal')
print('component analysis are:')
matrix = 'Var-Covar'
std = 'Eigenvalue'
x = [
[7.0, 4.0, 3.0],
[4.0, 1.0, 8.0],
[6.0, 3.0, 5.0],
[8.0, 6.0, 1.0],
[8.0, 5.0, 7.0],
[7.0, 2.0, 9.0],
[5.0, 3.0, 3.0],
[9.0, 5.0, 8.0],
[7.0, 4.0, 5.0],
[8.0, 2.0, 2.0],
]
isx = [1, 1, 1]
# Initialize s to 0.0, as matrix /= 'S'
s = [0.0, 0.0, 0.0]
# Calculate NVAR
nvar = isx.count(1)
e = mv.prin_comp(matrix, std, x, isx, s, nvar).e
print(' Eigenvalues Percentage Cumulative Chisq DF'
' Sig'
)
print(' variation variation')
print('[')
for i in range(e.shape[0]):
print(
' ' +
', '.join(['{:10.4f}']*e.shape[1]).format(*e[i, :])
)
print(']')
if __name__ == '__main__':
import doctest
import sys
sys.exit(
doctest.testmod(
None, verbose=True, report=False,
optionflags=doctest.REPORT_NDIFF,
).failed
)