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

datastructures_rowmerged.py

1 # file: datastructures_rowmerged.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 row merged data structures example.
21 # !*****************************************************************************
22 
23 #
24 
25 
26 #
27 
28 import os
29 import sys
30 
31 import numpy as np
32 
33 from daal.data_management import (
34  DictionaryIface, HomogenNumericTable, RowMergedNumericTable, BlockDescriptor, convertToHomogen, readWrite, readOnly
35 )
36 
37 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
38 if utils_folder not in sys.path:
39  sys.path.insert(0, utils_folder)
40 from utils import printArray, printNumericTable
41 
42 if __name__ == "__main__":
43 
44  print("Row merged numeric table example\n")
45 
46  nObservations1 = 5
47  nObservations2 = 6
48  nFeatures = 5
49  firstReadRow = 3
50  nRead = 6
51  featureIdx = 2
52 
53  # Example of using homogeneous numeric table
54  data1 = np.array([(0.0, 0.1, 0.2, 0.3, 0.4,),
55  (1.0, 1.1, 1.2, 1.3, 1.4,),
56  (2.0, 2.1, 2.2, 2.3, 2.4,),
57  (3.0, 3.1, 3.2, 3.3, 3.4,),
58  (4.0, 4.1, 4.2, 4.3, 4.4,),])
59 
60  data2 = np.array([(0.5, 0.6, 0.7, 0.8, 0.9,),
61  (1.5, 1.6, 1.7, 1.8, 1.9,),
62  (2.5, 2.6, 2.7, 2.8, 2.9,),
63  (3.5, 3.6, 3.7, 3.8, 3.9,),
64  (4.5, 4.6, 4.7, 4.8, 4.9,),
65  (5.5, 5.6, 5.7, 5.8, 5.9,),])
66 
67  # Create row merged numeric table consisting of two homogen numeric tables
68  table1 = HomogenNumericTable(DictionaryIface.equal, data1)
69  table2 = HomogenNumericTable(DictionaryIface.equal, data2)
70 
71  dataTable = RowMergedNumericTable()
72  dataTable.addNumericTable(table1)
73  dataTable.addNumericTable(table2)
74 
75  block = BlockDescriptor()
76 
77  # Read one row from merged numeric table
78  dataTable.getBlockOfRows(0, nObservations1 + nObservations2, readWrite, block)
79  printArray(block.getArray(), nFeatures, block.getNumberOfRows(), block.getNumberOfColumns(),
80  "Print rows from row merged numeric table as float:")
81 
82  # Modify row of the merged numeric table
83  row = block.getArray()
84  for i in range(nObservations1 + nObservations2):
85  row[i][featureIdx] *= row[i][featureIdx]
86  dataTable.releaseBlockOfRows(block)
87 
88  dataTable.getBlockOfRows(0, nObservations1 + nObservations2, readOnly, block)
89  printArray(block.getArray(), nFeatures, block.getNumberOfRows(), block.getNumberOfColumns(),
90  "Print rows from row merged numeric table as float:")
91  dataTable.releaseBlockOfRows(block)
92 
93  finalizedTable = convertToHomogen(dataTable)
94 
95  printNumericTable(finalizedTable, "Row merged table converted to homogen numeric table")

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