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

pca_transform_dense_batch.py

1 # file: pca_transform_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 # ! Content:
20 # ! Python example of PCA transformation algorithm.
21 # !*****************************************************************************
22 
23 #
24 
25 
26 #
27 
28 import os
29 import sys
30 import numpy as np
31 
32 import daal.algorithms.pca as pca
33 import daal.algorithms.pca.transform as pca_transform
34 from daal.data_management import DataSourceIface, FileDataSource
35 
36 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))))
37 if utils_folder not in sys.path:
38  sys.path.insert(0, utils_folder)
39 from utils import printNumericTable
40 from daal.data_management import NumericTable
41 # Input data set parameters
42 datasetName = os.path.join('..', 'data', 'batch', 'pca_transform.csv')
43 
44 if __name__ == "__main__":
45 
46  # Retrieve the input data
47  dataSource = FileDataSource(datasetName,
48  DataSourceIface.doAllocateNumericTable,
49  DataSourceIface.doDictionaryFromContext)
50  dataSource.loadDataBlock()
51  data = dataSource.getNumericTable()
52 
53  # Create an algorithm
54  algorithm = pca.Batch(fptype=np.float64,method=pca.svdDense)
55 
56  # Set the algorithm input data
57  algorithm.input.setDataset(pca.data, data)
58 
59  # Set the algorithm normalization parameters (mean and variance)
60  # to be exported for transform and whitening parameter (eigenvalue)
61  # If whitening is not required eigenvalues should be removed
62  # The eigenvalues would be calculated in pca.eigenvalues table of result
63  # but would not be passed to dataForTranform collection
64  # algorithm.paramter.resultsToCompute = (pca.mean | pca.variance | pca.eigenvalue)
65 
66  algorithm.parameter.resultsToCompute = pca.mean | pca.variance | pca.eigenvalue;
67 
68  # Compute PCA
69  res = algorithm.compute()
70  # Output basis, eigenvalues and mean values
71  printNumericTable(res.get(pca.eigenvalues), "Eigenvalues:")
72  printNumericTable(res.get(pca.eigenvectors), "Eigenvectors:")
73 
74  eigenvaluesT = res.get(pca.eigenvalues)
75  printNumericTable(eigenvaluesT, "Eigenvalues kv:")
76 
77  meansT = res.get(pca.means)
78  printNumericTable(meansT, "Means kv:")
79 
80  #eigenvaluesT = res.getCollection(pca.eigenvalue)
81  variancesT = res.get(pca.variances)
82  printNumericTable(variancesT, "Variances kv:")
83 
84  # Create an algorithm
85  tralgorithm = pca_transform.Batch(fptype=np.float64)
86 
87  # Set lower and upper bounds for the algorithm
88  tralgorithm.parameter.nComponents = 2
89 
90  # Set an input object for the algorithm
91  tralgorithm.input.setTable(pca_transform.data, data)
92 
93  # Set an input object for the eigenvectors
94  tralgorithm.input.setTable(pca_transform.eigenvectors, res.get(pca.eigenvectors))
95 
96  # Set an input object for the eigenvectors
97  tralgorithm.input.setCollection(pca_transform.dataForTransform, res.getCollection(pca.dataForTransform))
98 
99  # Compute PCA transformation function
100  trres = tralgorithm.compute()
101 
102  printNumericTable(trres.get(pca.transform.transformedData), "Transformed data:");
103  #printNumericTable(data, "First rows of the input data:", 4)
104  #printNumericTable(trres.get(pca_transform.transformedData), "First rows of the min-max normalization result:", 4)

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