Python* API Reference for Intel® Data Analytics Acceleration Library 2019

datastructures_soa.py

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

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