Python* API Reference for Intel® Data Analytics Acceleration Library 2019

cov_dense_distr.py

1 # file: cov_dense_distr.py
2 #===============================================================================
3 # Copyright 2014-2018 Intel Corporation.
4 #
5 # This software and the related documents are Intel copyrighted materials, and
6 # your use of them is governed by the express license under which they were
7 # provided to you (License). Unless the License provides otherwise, you may not
8 # use, modify, copy, publish, distribute, disclose or transmit this software or
9 # the related documents without Intel's prior written permission.
10 #
11 # This software and the related documents are provided as is, with no express
12 # or implied warranties, other than those that are expressly stated in the
13 # License.
14 #===============================================================================
15 
16 
17 
18 
19 import os
20 import sys
21 
22 from daal import step1Local, step2Master
23 from daal.algorithms import covariance
24 from daal.data_management import FileDataSource, DataSourceIface
25 
26 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
27 if utils_folder not in sys.path:
28  sys.path.insert(0, utils_folder)
29 from utils import printNumericTable
30 
31 DAAL_PREFIX = os.path.join('..', 'data')
32 
33 # Input data set parameters
34 nBlocks = 4
35 
36 datasetFileNames = [
37  os.path.join(DAAL_PREFIX, 'distributed', 'covcormoments_dense_1.csv'),
38  os.path.join(DAAL_PREFIX, 'distributed', 'covcormoments_dense_2.csv'),
39  os.path.join(DAAL_PREFIX, 'distributed', 'covcormoments_dense_3.csv'),
40  os.path.join(DAAL_PREFIX, 'distributed', 'covcormoments_dense_4.csv')
41 ]
42 
43 partialResult = [0] * nBlocks
44 result = None
45 
46 
47 def computestep1Local(block):
48  global partialResult
49 
50  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
51  dataSource = FileDataSource(
52  datasetFileNames[block], DataSourceIface.doAllocateNumericTable,
53  DataSourceIface.doDictionaryFromContext
54  )
55 
56  # Retrieve the data from the input file
57  dataSource.loadDataBlock()
58 
59  # Create algorithm objects to compute a dense variance-covariance matrix in the distributed processing mode using the default method
60  algorithm = covariance.Distributed(step1Local)
61 
62  # Set input objects for the algorithm
63  algorithm.input.set(covariance.data, dataSource.getNumericTable())
64 
65  # Compute partial estimates on local nodes
66  partialResult[block] = algorithm.compute() # Get the computed partial estimates
67 
68 
69 def computeOnMasterNode():
70  global result
71 
72  # Create algorithm objects to compute a dense variance-covariance matrix in the distributed processing mode using the default method
73  algorithm = covariance.Distributed(step2Master)
74 
75  # Set input objects for the algorithm
76  for i in range(nBlocks):
77  algorithm.input.add(covariance.partialResults, partialResult[i])
78 
79  # Compute a partial estimate on the master node from the partial estimates on local nodes
80  algorithm.compute()
81 
82  # Finalize the result in the distributed processing mode
83  result = algorithm.finalizeCompute() # Get the computed dense variance-covariance matrix
84 
85 if __name__ == "__main__":
86 
87  for i in range(nBlocks):
88  computestep1Local(i)
89 
90  computeOnMasterNode()
91 
92  printNumericTable(result.get(covariance.covariance), "Covariance matrix:")
93  printNumericTable(result.get(covariance.mean), "Mean vector:")

For more complete information about compiler optimizations, see our Optimization Notice.