Source code for naginterfaces.library.examples.mv.gaussian_mixture_ex

#!/usr/bin/env python3
"``naginterface.library.mv.gaussian_mixture`` Python Example."

# NAG Copyright 2017-2019.

# pylint: disable=invalid-name

from naginterfaces.library import mv

[docs]def main(): """ Example for :func:`naginterfaces.library.mv.gaussian_mixture`. Fits a mixture of Gaussians for a given (co)variance structure. >>> main() naginterfaces.library.mv.gaussian_mixture Python Example Results. Fits a Gaussian mixture model with pooled covariance structure to New Haven schools test data. The final membership probabilities are: [ 9.50176891e-01, 4.98231095e-02 3.32590884e-06, 9.99996674e-01 9.99613355e-01, 3.86644659e-04 9.99920087e-01, 7.99127116e-05 3.89990173e-02, 9.61000983e-01 9.32704894e-01, 6.72951064e-02 9.88809712e-01, 1.11902877e-02 4.12521422e-03, 9.95874786e-01 9.72521486e-01, 2.74785140e-02 9.99691952e-01, 3.08048285e-04 2.17221867e-01, 7.82778133e-01 7.69380852e-01, 2.30619148e-01 9.99973063e-01, 2.69370297e-05 6.11334389e-03, 9.93886656e-01 4.41893305e-02, 9.55810670e-01 3.50057883e-04, 9.99649942e-01 9.99902971e-01, 9.70286734e-05 4.02698414e-05, 9.99959730e-01 9.73798317e-01, 2.62016830e-02 3.02036785e-04, 9.99697963e-01 6.94705604e-02, 9.30529440e-01 4.16030240e-03, 9.95839698e-01 3.08391490e-02, 9.69160851e-01 9.91157909e-01, 8.84209120e-03 4.15339034e-04, 9.99584661e-01 ] The log-likehood is -29.683060270297. There were 14 function evaluations required. """ print('naginterfaces.library.mv.gaussian_mixture Python Example Results.') print('Fits a Gaussian mixture model with pooled covariance structure to') print('New Haven schools test data.') x = [ [2.7, 3.2, 4.5, 4.8], [3.9, 3.8, 5.9, 6.2], [4.8, 4.1, 6.8, 5.5], [3.1, 3.5, 4.3, 4.6], [3.4, 3.7, 5.1, 5.6], [3.1, 3.4, 4.1, 4.7], [4.6, 4.4, 6.6, 6.1], [3.1, 3.3, 4.0, 4.9], [3.8, 3.7, 4.7, 4.9], [5.2, 4.9, 8.2, 6.9], [3.9, 3.8, 5.2, 5.4], [4.1, 4.0, 5.6, 5.6], [5.7, 5.1, 7.0, 6.3], [3.0, 3.2, 4.5, 5.0], [2.9, 3.3, 4.5, 5.1], [3.4, 3.3, 4.4, 5.0], [4.0, 4.2, 5.2, 5.4], [3.0, 3.0, 4.6, 5.0], [4.0, 4.1, 5.9, 5.8], [3.0, 3.2, 4.4, 5.1], [3.6, 3.6, 5.3, 5.4], [3.1, 3.2, 4.6, 5.0], [3.2, 3.3, 5.4, 5.3], [3.0, 3.4, 4.2, 4.7], [3.8, 4.0, 6.9, 6.7], ] nvar = 4 ng = 2 sopt = 2 gm = mv.gaussian_mixture(x, nvar, ng, sopt, riter=5) print('The final membership probabilities are:') print('[') for i in range(gm.prob.shape[0]): print( ' ' + ', '.join(['{:10.8e}']*gm.prob.shape[1]).format(*gm.prob[i, :]) ) print(']') print('The log-likehood is {:.12f}.'.format(gm.loglik)) print('There were {:d} function evaluations required.'.format(gm.niter))
if __name__ == '__main__': import doctest import sys sys.exit( doctest.testmod( None, verbose=True, report=False, optionflags=doctest.REPORT_NDIFF, ).failed )