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

datastructures_aos.py

1 # file: datastructures_aos.py
2 #===============================================================================
3 # Copyright 2014-2018 Intel Corporation.
4 #
5 # This software and the related documents are Intel copyrighted materials, and
6 # your use of them is governed by the express license under which they were
7 # provided to you (License). Unless the License provides otherwise, you may not
8 # use, modify, copy, publish, distribute, disclose or transmit this software or
9 # the related documents without Intel's prior written permission.
10 #
11 # This software and the related documents are provided as is, with no express
12 # or implied warranties, other than those that are expressly stated in the
13 # License.
14 #===============================================================================
15 
16 
17 
18 
19 import os
20 import sys
21 
22 import numpy as np
23 
24 from daal.data_management import data_feature_utils, AOSNumericTable, BlockDescriptor, readOnly, readWrite
25 
26 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
27 if utils_folder not in sys.path:
28  sys.path.insert(0, utils_folder)
29 from utils import printArray
30 
31 
32 if __name__ == "__main__":
33 
34  print("Array of structures (AOS) numeric table example\n")
35 
36  points = np.array([(0.5, -1.3, 1, 100.1),
37  (2.5, -3.3, 2, 200.2),
38  (4.5, -5.3, 2, 350.3),
39  (6.5, -7.3, 0, 470.4),
40  (8.5, -9.3, 1, 270.5)],
41  dtype=[('x','f4'), ('y','f4'), ('categ','i4'), ('value','f8')])
42 
43  nObservations = len(points)
44  nFeatures = len(points[0])
45 
46  # Construct AOS numericTable for a data array with nFeatures fields and nObservations elements
47  # Dictionary will be initialized with type information from ndarray
48  dataTable = AOSNumericTable(points)
49 
50  # Get the dictionary and update it with additional information about data
51  dict = dataTable.getDictionary()
52 
53  # Add a feature type to the dictionary
54  dict[0].featureType = data_feature_utils.DAAL_CONTINUOUS
55  dict[1].featureType = data_feature_utils.DAAL_CONTINUOUS
56  dict[2].featureType = data_feature_utils.DAAL_CATEGORICAL
57  dict[3].featureType = data_feature_utils.DAAL_CONTINUOUS
58 
59  # Set the number of categories for a categorical feature
60  dict[2].categoryNumber = 3
61 
62  # Read a block of rows
63  firstReadRow = 0
64  doubleBlock = BlockDescriptor()
65  dataTable.getBlockOfRows(firstReadRow, nObservations, readWrite, doubleBlock)
66  printArray(
67  doubleBlock.getArray(), nFeatures, doubleBlock.getNumberOfRows(),
68  doubleBlock.getNumberOfColumns(),"Print AOS data structures as double:"
69  )
70  dataTable.releaseBlockOfRows(doubleBlock)
71 
72  # Read a feature (column)
73  readFeatureIdx = 2
74 
75  intBlock = BlockDescriptor(ntype=np.intc)
76  dataTable.getBlockOfColumnValues(readFeatureIdx, firstReadRow, nObservations, readOnly, intBlock)
77  printArray(
78  intBlock.getArray(), 1, intBlock.getNumberOfRows(), intBlock.getNumberOfColumns(),
79  "Print the third feature of AOS:", flt64=False
80  )
81  dataTable.releaseBlockOfColumnValues(intBlock)

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