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

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

# NAG Copyright 2017-2019.

# pylint: disable=invalid-name

from naginterfaces.library import mv

[docs]def main(): """ Example for :func:`naginterfaces.library.mv.cluster_kmeans`. K-means cluster analysis. >>> main() naginterfaces.library.mv.cluster_kmeans Python Example Results. K-means cluster analysis. The cluster to which each point belongs: [1, 1, 3, 2, 3, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 3] The number of points in each cluster: [6, 3, 11] The within-cluster sum of weights of each cluster: [6.00, 3.00, 11.00] The within-cluster sum of squares of each cluster: [46.57, 20.38, 468.90] The final cluster centres: [ 8.1183e+01, 1.1667e+01, 7.1500e+00, 2.0500e+00, 6.6000e+00 4.7867e+01, 3.5800e+01, 1.6333e+01, 2.4000e+00, 6.7333e+00 6.4045e+01, 2.5209e+01, 1.0745e+01, 2.8364e+00, 6.6545e+00 ] """ print('naginterfaces.library.mv.cluster_kmeans Python Example Results.') print('K-means cluster analysis.') x = [ [77.3, 13., 9.7, 1.5, 6.4], [82.5, 10., 7.5, 1.5, 6.5], [66.9, 20.6, 12.5, 2.3, 7.], [47.2, 33.8, 19., 2.8, 5.8], [65.3, 20.5, 14.2, 1.9, 6.9], [83.3, 10., 6.7, 2.2, 7.], [81.6, 12.7, 5.7, 2.9, 6.7], [47.8, 36.5, 15.7, 2.3, 7.2], [48.6, 37.1, 14.3, 2.1, 7.2], [61.6, 25.5, 12.9, 1.9, 7.3], [58.6, 26.5, 14.9, 2.4, 6.7], [69.3, 22.3, 8.4, 4., 7.], [61.8, 30.8, 7.4, 2.7, 6.4], [67.7, 25.3, 7., 4.8, 7.3], [57.2, 31.2, 11.6, 2.4, 6.5], [67.2, 22.7, 10.1, 3.3, 6.2], [59.2, 31.2, 9.6, 2.4, 6.], [80.2, 13.2, 6.6, 2., 5.8], [82.2, 11.1, 6.7, 2.2, 7.2], [69.7, 20.7, 9.6, 3.1, 5.9], ] # The inclusion flags: isx = [1]*len(x[0]) # The initial cluster centres: cmeans = [ [82.5, 10., 7.5, 1.5, 6.5], [47.8, 36.5, 15.7, 2.3, 7.2], [67.2, 22.7, 10.1, 3.3, 6.2], ] kmc = mv.cluster_kmeans(x, isx, cmeans) print('The cluster to which each point belongs:') print('[' + ', '.join(['{:d}']*len(kmc.inc)).format(*kmc.inc) + ']') print('The number of points in each cluster:') print('[' + ', '.join(['{:d}']*len(kmc.nic)).format(*kmc.nic) + ']') print('The within-cluster sum of weights of each cluster:') print('[' + ', '.join(['{:4.2f}']*len(kmc.csw)).format(*kmc.csw) + ']') print('The within-cluster sum of squares of each cluster:') print('[' + ', '.join(['{:4.2f}']*len(kmc.css)).format(*kmc.css) + ']') print('The final cluster centres:') print('[') for i in range(kmc.cmeans.shape[0]): print( ' ' + ', '.join( ['{:6.4e}']*kmc.cmeans.shape[1] ).format(*kmc.cmeans[i, :]) ) print(']')
if __name__ == '__main__': import doctest import sys sys.exit( doctest.testmod( None, verbose=True, report=False, optionflags=doctest.REPORT_NDIFF, ).failed )