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

pca_cor_dense_distr.py

1 # file: pca_cor_dense_distr.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 import numpy as np
25 
26 from daal import step1Local, step2Master
27 from daal.algorithms import pca
28 from daal.data_management import FileDataSource, DataSourceIface
29 
30 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
31 if utils_folder not in sys.path:
32  sys.path.insert(0, utils_folder)
33 from utils import printNumericTable
34 
35 DAAL_PREFIX = os.path.join('..', 'data')
36 
37 # Input data set parameters
38 nBlocks = 4
39 nVectorsInBlock = 250
40 nFeatures = None
41 
42 dataFileNames = [
43  os.path.join(DAAL_PREFIX, 'distributed', 'pca_normalized_1.csv'),
44  os.path.join(DAAL_PREFIX, 'distributed', 'pca_normalized_2.csv'),
45  os.path.join(DAAL_PREFIX, 'distributed', 'pca_normalized_3.csv'),
46  os.path.join(DAAL_PREFIX, 'distributed', 'pca_normalized_4.csv')
47 ]
48 
49 if __name__ == "__main__":
50 
51  # Create an algorithm for principal component analysis using the correlation method on the master node
52  masterAlgorithm = pca.Distributed(step2Master,fptype=np.float64)
53 
54  for i in range(nBlocks):
55  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
56  dataSource = FileDataSource(
57  dataFileNames[i], DataSourceIface.doAllocateNumericTable,
58  DataSourceIface.doDictionaryFromContext
59  )
60 
61  # Retrieve the input data
62  dataSource.loadDataBlock(nVectorsInBlock)
63 
64  # Create an algorithm for principal component analysis using the correlation method on the local node
65  localAlgorithm = pca.Distributed(step1Local,fptype=np.float64)
66 
67  # Set the input data to the algorithm
68  localAlgorithm.input.setDataset(pca.data, dataSource.getNumericTable())
69 
70  # Compute PCA decomposition
71  # Set local partial results as input for the master-node algorithm
72  masterAlgorithm.input.add(pca.partialResults, localAlgorithm.compute())
73 
74  # Merge and finalize PCA decomposition on the master node
75  masterAlgorithm.compute()
76  result = masterAlgorithm.finalizeCompute()
77 
78  # Print the results
79  printNumericTable(result.get(pca.eigenvalues), "Eigenvalues:")
80  printNumericTable(result.get(pca.eigenvectors), "Eigenvectors:")

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