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

datastructures_aos.py

1 #===============================================================================
2 # Copyright 2014-2017 Intel Corporation
3 # All Rights Reserved.
4 #
5 # If this software was obtained under the Intel Simplified Software License,
6 # the following terms apply:
7 #
8 # The source code, information and material ("Material") contained herein is
9 # owned by Intel Corporation or its suppliers or licensors, and title to such
10 # Material remains with Intel Corporation or its suppliers or licensors. The
11 # Material contains proprietary information of Intel or its suppliers and
12 # licensors. The Material is protected by worldwide copyright laws and treaty
13 # provisions. No part of the Material may be used, copied, reproduced,
14 # modified, published, uploaded, posted, transmitted, distributed or disclosed
15 # in any way without Intel's prior express written permission. No license under
16 # any patent, copyright or other intellectual property rights in the Material
17 # is granted to or conferred upon you, either expressly, by implication,
18 # inducement, estoppel or otherwise. Any license under such intellectual
19 # property rights must be express and approved by Intel in writing.
20 #
21 # Unless otherwise agreed by Intel in writing, you may not remove or alter this
22 # notice or any other notice embedded in Materials by Intel or Intel's
23 # suppliers or licensors in any way.
24 #
25 #
26 # If this software was obtained under the Apache License, Version 2.0 (the
27 # "License"), the following terms apply:
28 #
29 # You may not use this file except in compliance with the License. You may
30 # obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
31 #
32 #
33 # Unless required by applicable law or agreed to in writing, software
34 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
35 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 #
37 # See the License for the specific language governing permissions and
38 # limitations under the License.
39 #===============================================================================
40 
41 
42 
43 
44 import os
45 import sys
46 
47 import numpy as np
48 
49 from daal.data_management import data_feature_utils, AOSNumericTable, BlockDescriptor, readOnly, readWrite
50 
51 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
52 if utils_folder not in sys.path:
53  sys.path.insert(0, utils_folder)
54 from utils import printArray
55 
56 
57 if __name__ == "__main__":
58 
59  print("Array of structures (AOS) numeric table example\n")
60 
61  points = np.array([(0.5, -1.3, 1, 100.1),
62  (2.5, -3.3, 2, 200.2),
63  (4.5, -5.3, 2, 350.3),
64  (6.5, -7.3, 0, 470.4),
65  (8.5, -9.3, 1, 270.5)],
66  dtype=[('x','f4'), ('y','f4'), ('categ','i4'), ('value','f8')])
67 
68  nObservations = len(points)
69  nFeatures = len(points[0])
70 
71  # Construct AOS numericTable for a data array with nFeatures fields and nObservations elements
72  # Dictionary will be initialized with type information from ndarray
73  dataTable = AOSNumericTable(points)
74 
75  # Get the dictionary and update it with additional information about data
76  dict = dataTable.getDictionary()
77 
78  # Add a feature type to the dictionary
79  dict[0].featureType = data_feature_utils.DAAL_CONTINUOUS
80  dict[1].featureType = data_feature_utils.DAAL_CONTINUOUS
81  dict[2].featureType = data_feature_utils.DAAL_CATEGORICAL
82  dict[3].featureType = data_feature_utils.DAAL_CONTINUOUS
83 
84  # Set the number of categories for a categorical feature
85  dict[2].categoryNumber = 3
86 
87  # Read a block of rows
88  firstReadRow = 0
89  doubleBlock = BlockDescriptor()
90  dataTable.getBlockOfRows(firstReadRow, nObservations, readWrite, doubleBlock)
91  printArray(
92  doubleBlock.getArray(), nFeatures, doubleBlock.getNumberOfRows(),
93  doubleBlock.getNumberOfColumns(),"Print AOS data structures as double:"
94  )
95  dataTable.releaseBlockOfRows(doubleBlock)
96 
97  # Read a feature (column)
98  readFeatureIdx = 2
99 
100  intBlock = BlockDescriptor(ntype=np.intc)
101  dataTable.getBlockOfColumnValues(readFeatureIdx, firstReadRow, nObservations, readOnly, intBlock)
102  printArray(
103  intBlock.getArray(), 1, intBlock.getNumberOfRows(), intBlock.getNumberOfColumns(),
104  "Print the third feature of AOS:", flt64=False
105  )
106  dataTable.releaseBlockOfColumnValues(intBlock)

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