Source code for naginterfaces.​library.​examples.​correg.​pls_ex

#!/usr/bin/env python
"``naginterface.library.correg.pls`` Python Example."

# NAG Copyright 2017-2019.

# pylint: disable=invalid-name,too-many-locals

from naginterfaces.library import correg

[docs]def main(): """ Example for :func:`naginterfaces.library.correg.pls`. Partial least squares (PLS) regression using singular value decomposition, parameter estimates, predictions. >>> main() naginterfaces.library.correg.pls Python Example Results. PLS regression using SVD; param. estimates; predictions. Begin regression. Begin estimation. Begin prediction. Predictions: [ 0.2133 0.5153 0.1438 0.4460 0.1716 2.4808 0.0963 1.4476 -0.1546 -0.5492 0.5393 0.2685 -1.1333 1.7974 0.4972 ] """ print('naginterfaces.library.correg.pls Python Example Results.') print('PLS regression using SVD; param. estimates; predictions.') print('Begin regression.') # The predictor and response observations: x, y = get_data() # The inclusion array: isx = [1]*len(x[0]) ip = isx.count(1) # The scaling mode: iscale = 1 # Scalings, not required: xstd = [0.]*ip ystd = [0.]*len(y[0]) # The number of latent variables to calculate: maxfac = 4 svd = correg.pls_svd( x, isx, y, iscale, xstd, ystd, maxfac, ) print('Begin estimation.') # Number of factors to include: nfact = 2 # Singular-value threshold: rcond = -1. # Calculation mode: orig = 1 # Mean values of the variables: xbar = [ -2.6137, -2.3614, -1.0449, 2.8614, 0.3156, -0.2641, -0.3146, -1.1221, 0.2401, 0.4694, -1.9619, 0.1691, 2.5664, 1.3741, -2.7821, ] ybar = [0.4520] # Variable Influence Projection mode: vipopt = 1 fit = correg.pls_fit( nfact, svd.p, svd.c, svd.w, rcond, orig, xbar, ybar, iscale, svd.xstd, svd.ystd, vipopt, svd.ycv[:nfact, :], ) print('Begin prediction.') # Estimate mode: orig = -1 yhat = correg.pls_pred( orig, xbar, ybar, iscale, svd.xstd, svd.ystd, fit.b, isx, x, ) print('Predictions:') print('[') for i in range(yhat.shape[0]): print( ' ' + ', '.join( ['{:6.4f}']*yhat.shape[1] ).format(*yhat[i, :]) ) print(']')
def get_data(): """ The predictor and response observations. """ x = [ [ -2.6931, -2.5271, -1.2871, 3.0777, 0.3891, -0.0701, 1.9607, -1.6324, 0.5746, 1.9607, -1.6324, 0.5740, 2.8369, 1.4092, -3.1398, ], [ -2.6931, -2.5271, -1.2871, 3.0777, 0.3891, -0.0701, 1.9607, -1.6324, 0.5746, 0.0744, -1.7333, 0.0902, 2.8369, 1.4092, -3.1398, ], [ -2.6931, -2.5271, -1.2871, 3.0777, 0.3891, -0.0701, 0.0744, -1.7333, 0.0902, 1.9607, -1.6324, 0.5746, 2.8369, 1.4092, -3.1398, ], [ -2.6931, -2.5271, -1.2871, 3.0777, 0.3891, -0.0701, 0.0744, -1.7333, 0.0902, 0.0744, -1.7333, 0.0902, 2.8369, 1.4092, -3.1398, ], [ -2.6931, -2.5271, -1.2871, 2.8369, 1.4092, -3.1398, 0.0744, -1.7333, 0.0902, 0.0744, -1.7333, 0.0902, 2.8369, 1.4092, -3.1398, ], [ -2.6931, -2.5271, -1.2871, 3.0777, 0.3891, -0.0701, -4.7548, 3.6521, 0.8524, 0.0744, -1.7333, 0.0902, 2.8369, 1.4092, -3.1398, ], [ -2.6931, -2.5271, -1.2871, 3.0777, 0.3891, -0.0701, 0.0744, -1.7333, 0.0902, 0.0744, -1.7333, 0.0902, -1.2201, 0.8829, 2.2253, ], [ -2.6931, -2.5271, -1.2871, 3.0777, 0.3891, -0.0701, 2.4064, 1.7438, 1.1057, 0.0744, -1.7333, 0.0902, 2.8369, 1.4092, -3.1398, ], [ -2.6931, -2.5271, -1.2871, 0.0744, -1.7333, 0.0902, 0.0744, -1.7333, 0.0902, 0.0744, -1.7333, 0.0902, 2.8369, 1.4092, -3.1398, ], [ 2.2261, -5.3648, 0.3049, 3.0777, 0.3891, -0.0701, 0.0744, -1.7333, 0.0902, 0.0744, -1.7333, 0.0902, 2.8369, 1.4092, -3.1398, ], [ -4.1921, -1.0285, -0.9801, 3.0777, 0.3891, -0.0701, 0.0744, -1.7333, 0.0902, 0.0744, -1.7333, 0.0902, 2.8369, 1.4092, -3.1398, ], [ -4.9217, 1.2977, 0.4473, 3.0777, 0.3891, -0.0701, 0.0744, -1.7333, 0.0902, 0.0744, -1.7333, 0.0902, 2.8369, 1.4092, -3.1398, ], [ -2.6931, -2.5271, -1.2871, 3.0777, 0.3891, -0.0701, 2.2261, -5.3648, 0.3049, 2.2261, -5.3648, 0.3049, 2.8369, 1.4092, -3.1398, ], [ -2.6931, -2.5271, -1.2871, 3.0777, 0.3891, -0.0701, -4.9217, 1.2977, 0.4473, 0.0744, -1.7333, 0.0902, 2.8369, 1.4092, -3.1398, ], [ -2.6931, -2.5271, -1.2871, 3.0777, 0.3891, -0.0701, -4.1921, -1.0285, -0.9801, 0.0744, -1.7333, 0.0902, 2.8369, 1.4092, -3.1398, ], ] y = [ [0.00], [0.28], [0.20], [0.51], [0.11], [2.73], [0.18], [1.53], [-0.10], [-0.52], [0.40], [0.30], [-1.00], [1.57], [0.59], ] return x, y if __name__ == '__main__': import doctest import sys sys.exit( doctest.testmod( None, verbose=True, report=False, optionflags=doctest.REPORT_NDIFF, ).failed )