Python* API Reference for Intel® Data Analytics Acceleration Library 2018 Update 2

datastructures_rowmerged.py

1 # file: datastructures_rowmerged.py
2 #===============================================================================
3 # Copyright 2014-2018 Intel Corporation
4 # All Rights Reserved.
5 #
6 # If this software was obtained under the Intel Simplified Software License,
7 # the following terms apply:
8 #
9 # The source code, information and material ("Material") contained herein is
10 # owned by Intel Corporation or its suppliers or licensors, and title to such
11 # Material remains with Intel Corporation or its suppliers or licensors. The
12 # Material contains proprietary information of Intel or its suppliers and
13 # licensors. The Material is protected by worldwide copyright laws and treaty
14 # provisions. No part of the Material may be used, copied, reproduced,
15 # modified, published, uploaded, posted, transmitted, distributed or disclosed
16 # in any way without Intel's prior express written permission. No license under
17 # any patent, copyright or other intellectual property rights in the Material
18 # is granted to or conferred upon you, either expressly, by implication,
19 # inducement, estoppel or otherwise. Any license under such intellectual
20 # property rights must be express and approved by Intel in writing.
21 #
22 # Unless otherwise agreed by Intel in writing, you may not remove or alter this
23 # notice or any other notice embedded in Materials by Intel or Intel's
24 # suppliers or licensors in any way.
25 #
26 #
27 # If this software was obtained under the Apache License, Version 2.0 (the
28 # "License"), the following terms apply:
29 #
30 # You may not use this file except in compliance with the License. You may
31 # obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
32 #
33 #
34 # Unless required by applicable law or agreed to in writing, software
35 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
36 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 #
38 # See the License for the specific language governing permissions and
39 # limitations under the License.
40 #===============================================================================
41 
42 #
43 # ! Content:
44 # ! Python row merged data structures example.
45 # !*****************************************************************************
46 
47 #
48 
51 
52 import os
53 import sys
54 
55 import numpy as np
56 
57 from daal.data_management import (
58  DictionaryIface, HomogenNumericTable, RowMergedNumericTable, BlockDescriptor, convertToHomogen, readWrite, readOnly
59 )
60 
61 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
62 if utils_folder not in sys.path:
63  sys.path.insert(0, utils_folder)
64 from utils import printArray, printNumericTable
65 
66 if __name__ == "__main__":
67 
68  print("Row merged numeric table example\n")
69 
70  nObservations1 = 5
71  nObservations2 = 6
72  nFeatures = 5
73  firstReadRow = 3
74  nRead = 6
75  featureIdx = 2
76 
77  # Example of using homogeneous numeric table
78  data1 = np.array([(0.0, 0.1, 0.2, 0.3, 0.4,),
79  (1.0, 1.1, 1.2, 1.3, 1.4,),
80  (2.0, 2.1, 2.2, 2.3, 2.4,),
81  (3.0, 3.1, 3.2, 3.3, 3.4,),
82  (4.0, 4.1, 4.2, 4.3, 4.4,),])
83 
84  data2 = np.array([(0.5, 0.6, 0.7, 0.8, 0.9,),
85  (1.5, 1.6, 1.7, 1.8, 1.9,),
86  (2.5, 2.6, 2.7, 2.8, 2.9,),
87  (3.5, 3.6, 3.7, 3.8, 3.9,),
88  (4.5, 4.6, 4.7, 4.8, 4.9,),
89  (5.5, 5.6, 5.7, 5.8, 5.9,),])
90 
91  # Create row merged numeric table consisting of two homogen numeric tables
92  table1 = HomogenNumericTable(DictionaryIface.equal, data1)
93  table2 = HomogenNumericTable(DictionaryIface.equal, data2)
94 
95  dataTable = RowMergedNumericTable()
96  dataTable.addNumericTable(table1)
97  dataTable.addNumericTable(table2)
98 
99  block = BlockDescriptor()
100 
101  # Read one row from merged numeric table
102  dataTable.getBlockOfRows(0, nObservations1 + nObservations2, readWrite, block)
103  printArray(block.getArray(), nFeatures, block.getNumberOfRows(), block.getNumberOfColumns(),
104  "Print rows from row merged numeric table as float:")
105 
106  # Modify row of the merged numeric table
107  row = block.getArray()
108  for i in range(nObservations1 + nObservations2):
109  row[i][featureIdx] *= row[i][featureIdx]
110  dataTable.releaseBlockOfRows(block)
111 
112  dataTable.getBlockOfRows(0, nObservations1 + nObservations2, readOnly, block)
113  printArray(block.getArray(), nFeatures, block.getNumberOfRows(), block.getNumberOfColumns(),
114  "Print rows from row merged numeric table as float:")
115  dataTable.releaseBlockOfRows(block)
116 
117  finalizedTable = convertToHomogen(dataTable)
118 
119  printNumericTable(finalizedTable, "Row merged table converted to homogen numeric table")

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