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

datastructures_aos.py

1 # file: datastructures_aos.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.data_management import features, AOSNumericTable, BlockDescriptor, readOnly, readWrite
27 
28 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
29 if utils_folder not in sys.path:
30  sys.path.insert(0, utils_folder)
31 from utils import printArray
32 
33 
34 if __name__ == "__main__":
35 
36  print("Array of structures (AOS) numeric table example\n")
37 
38  points = np.array([(0.5, -1.3, 1, 100.1),
39  (2.5, -3.3, 2, 200.2),
40  (4.5, -5.3, 2, 350.3),
41  (6.5, -7.3, 0, 470.4),
42  (8.5, -9.3, 1, 270.5)],
43  dtype=[('x','f4'), ('y','f4'), ('categ','i4'), ('value','f8')])
44 
45  nObservations = len(points)
46  nFeatures = len(points[0])
47 
48  # Construct AOS numericTable for a data array with nFeatures fields and nObservations elements
49  # Dictionary will be initialized with type information from ndarray
50  dataTable = AOSNumericTable(points)
51 
52  # Get the dictionary and update it with additional information about data
53  dict = dataTable.getDictionary()
54 
55  # Add a feature type to the dictionary
56  dict[0].featureType = features.DAAL_CONTINUOUS
57  dict[1].featureType = features.DAAL_CONTINUOUS
58  dict[2].featureType = features.DAAL_CATEGORICAL
59  dict[3].featureType = features.DAAL_CONTINUOUS
60 
61  # Set the number of categories for a categorical feature
62  dict[2].categoryNumber = 3
63 
64  # Read a block of rows
65  firstReadRow = 0
66  doubleBlock = BlockDescriptor()
67  dataTable.getBlockOfRows(firstReadRow, nObservations, readWrite, doubleBlock)
68  printArray(
69  doubleBlock.getArray(), nFeatures, doubleBlock.getNumberOfRows(),
70  doubleBlock.getNumberOfColumns(),"Print AOS data structures as double:"
71  )
72  dataTable.releaseBlockOfRows(doubleBlock)
73 
74  # Read a feature (column)
75  readFeatureIdx = 2
76 
77  intBlock = BlockDescriptor(ntype=np.intc)
78  dataTable.getBlockOfColumnValues(readFeatureIdx, firstReadRow, nObservations, readOnly, intBlock)
79  printArray(
80  intBlock.getArray(), 1, intBlock.getNumberOfRows(), intBlock.getNumberOfColumns(),
81  "Print the third feature of AOS:", flt64=False
82  )
83  dataTable.releaseBlockOfColumnValues(intBlock)

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