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

datastructures_soa.py

1 # file: datastructures_soa.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 BlockDescriptor, SOANumericTable, features, readOnly
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 def toString(v):
35  if v == features.DAAL_CATEGORICAL:
36  return "DAAL_CATEGORICAL"
37  elif v == features.DAAL_ORDINAL:
38  return "DAAL_ORDINAL"
39  elif v == features.DAAL_CONTINUOUS:
40  return "DAAL_CONTINUOUS"
41  else:
42  return "[Unknown FeatureType]"
43 
44 
45 if __name__ == "__main__":
46  print("Structure of array (SOA) numeric table example\n")
47 
48  firstReadRow = 0
49  nRead = 3
50  readFeatureIdx = None
51  nObservations = 10
52  nFeatures = 4
53  dDataSOA = np.array([1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4, 2.6, 2.8], dtype=np.float64)
54  fDataSOA = np.array([3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0], dtype=np.float32)
55  iDataSOA = np.array([-10, -20, -30, -40, -50, -60, -70, -80, -90, -100], dtype=np.int32)
56  cDataSOA = np.array([1, 2, 3, 4, 5, 1, 2, 3, 4, 5], dtype=np.uint8)
57 
58  dataTable = SOANumericTable(nFeatures, nObservations)
59  dataTable.setArray(cDataSOA, 0)
60  dataTable.setArray(fDataSOA, 1)
61  dataTable.setArray(dDataSOA, 2)
62  dataTable.setArray(iDataSOA, 3)
63 
64  doubleBlock = BlockDescriptor()
65  dataTable.getBlockOfRows(firstReadRow, nRead, readOnly, doubleBlock)
66  printArray(
67  doubleBlock.getArray(), nFeatures, doubleBlock.getNumberOfRows(), doubleBlock.getNumberOfColumns(),
68  "Print SOA data structures as double:"
69  )
70  dataTable.releaseBlockOfRows(doubleBlock)
71 
72  readFeatureIdx = 0
73  intBlock = BlockDescriptor(ntype=np.intc)
74  dataTable.getBlockOfColumnValues(readFeatureIdx, firstReadRow, nObservations, readOnly, intBlock)
75  printArray(
76  intBlock.getArray(), 1, intBlock.getNumberOfRows(), intBlock.getNumberOfColumns(),
77  "Print the first feature of SOA:", flt64=False
78  )
79  dataTable.releaseBlockOfColumnValues(intBlock)
80 
81  pDictionary = dataTable.getDictionary()
82  print("Number of features in table: " + str(pDictionary.getNumberOfFeatures()))
83  print("")
84 
85  print("Default type in autogenerated dictionary:")
86  for i in range(0, nFeatures):
87  featureType = pDictionary[i].featureType
88  print("Type of " + str(i) + " feature: " + toString(featureType))
89  print("")
90 
91  categoricalFeature = pDictionary[0]
92  categoricalFeature.featureType = features.DAAL_CATEGORICAL
93 
94  print("Modified type in the dictionary:")
95  for i in range(0, nFeatures):
96  featureType = pDictionary[i].featureType
97  print("Type of " + str(i) + " feature: " + toString(featureType))
98  print("")

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