Python* API Reference for Intel® Data Analytics Acceleration Library 2020 Update 1

em_gmm_dense_batch.py

1 # file: em_gmm_dense_batch.py
2 #===============================================================================
3 # Copyright 2014-2020 Intel Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #===============================================================================
17 
18 
19 
20 
21 import os
22 import sys
23 
24 from daal.algorithms import em_gmm
25 from daal.data_management import FileDataSource, DataSourceIface
26 
27 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
28 if utils_folder not in sys.path:
29  sys.path.insert(0, utils_folder)
30 from utils import printNumericTable
31 
32 DAAL_PREFIX = os.path.join('..', 'data')
33 
34 # Input data set parameters
35 datasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'em_gmm.csv')
36 nComponents = 2
37 
38 if __name__ == "__main__":
39 
40  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
41  dataSource = FileDataSource(
42  datasetFileName,
43  DataSourceIface.doAllocateNumericTable,
44  DataSourceIface.doDictionaryFromContext
45  )
46  nFeatures = dataSource.getNumberOfColumns()
47 
48  # Retrieve the data from the input file
49  dataSource.loadDataBlock()
50 
51  # Create algorithm objects to initialize the EM algorithm for the GMM
52  # computing the number of components using the default method
53  initAlgorithm = em_gmm.init.Batch(nComponents)
54 
55  # Set an input data table for the initialization algorithm
56  initAlgorithm.input.set(em_gmm.init.data, dataSource.getNumericTable())
57 
58  # Compute initial values for the EM algorithm for the GMM with the default parameters
59  resultInit = initAlgorithm.compute()
60 
61  # Create algorithm objects for the EM algorithm for the GMM computing the number of components using the default method
62  algorithm = em_gmm.Batch(nComponents)
63 
64  # Set an input data table for the algorithm
65  algorithm.input.setTable(em_gmm.data, dataSource.getNumericTable())
66  algorithm.input.setValues(em_gmm.inputValues, resultInit)
67 
68  # Compute the results of the EM algorithm for the GMM with the default parameters
69  result = algorithm.compute()
70 
71  # Print the results
72  printNumericTable(result.getResult(em_gmm.weights), "Weights")
73  printNumericTable(result.getResult(em_gmm.means), "Means")
74  for i in range(nComponents):
75  printNumericTable(result.getCovariances(em_gmm.covariances, i), "Covariance")

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