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

datastructures_soa.py

1 # file: datastructures_soa.py
2 #===============================================================================
3 # Copyright 2014-2018 Intel Corporation
4 # All Rights Reserved.
5 #
6 # If this software was obtained under the Intel Simplified Software License,
7 # the following terms apply:
8 #
9 # The source code, information and material ("Material") contained herein is
10 # owned by Intel Corporation or its suppliers or licensors, and title to such
11 # Material remains with Intel Corporation or its suppliers or licensors. The
12 # Material contains proprietary information of Intel or its suppliers and
13 # licensors. The Material is protected by worldwide copyright laws and treaty
14 # provisions. No part of the Material may be used, copied, reproduced,
15 # modified, published, uploaded, posted, transmitted, distributed or disclosed
16 # in any way without Intel's prior express written permission. No license under
17 # any patent, copyright or other intellectual property rights in the Material
18 # is granted to or conferred upon you, either expressly, by implication,
19 # inducement, estoppel or otherwise. Any license under such intellectual
20 # property rights must be express and approved by Intel in writing.
21 #
22 # Unless otherwise agreed by Intel in writing, you may not remove or alter this
23 # notice or any other notice embedded in Materials by Intel or Intel's
24 # suppliers or licensors in any way.
25 #
26 #
27 # If this software was obtained under the Apache License, Version 2.0 (the
28 # "License"), the following terms apply:
29 #
30 # You may not use this file except in compliance with the License. You may
31 # obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
32 #
33 #
34 # Unless required by applicable law or agreed to in writing, software
35 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
36 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 #
38 # See the License for the specific language governing permissions and
39 # limitations under the License.
40 #===============================================================================
41 
42 
43 
44 
45 import os
46 import sys
47 
48 import numpy as np
49 
50 from daal.data_management import BlockDescriptor, SOANumericTable, data_feature_utils, readOnly
51 
52 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
53 if utils_folder not in sys.path:
54  sys.path.insert(0, utils_folder)
55 from utils import printArray
56 
57 
58 def toString(v):
59  if v == data_feature_utils.DAAL_CATEGORICAL:
60  return "DAAL_CATEGORICAL"
61  elif v == data_feature_utils.DAAL_ORDINAL:
62  return "DAAL_ORDINAL"
63  elif v == data_feature_utils.DAAL_CONTINUOUS:
64  return "DAAL_CONTINUOUS"
65  else:
66  return "[Unknown FeatureType]"
67 
68 
69 if __name__ == "__main__":
70  print("Structure of array (SOA) numeric table example\n")
71 
72  firstReadRow = 0
73  nRead = 3
74  readFeatureIdx = None
75  nObservations = 10
76  nFeatures = 4
77  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)
78  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)
79  iDataSOA = np.array([-10, -20, -30, -40, -50, -60, -70, -80, -90, -100], dtype=np.int32)
80  cDataSOA = np.array([1, 2, 3, 4, 5, 1, 2, 3, 4, 5], dtype=np.uint8)
81 
82  dataTable = SOANumericTable(nFeatures, nObservations)
83  dataTable.setArray(cDataSOA, 0)
84  dataTable.setArray(fDataSOA, 1)
85  dataTable.setArray(dDataSOA, 2)
86  dataTable.setArray(iDataSOA, 3)
87 
88  doubleBlock = BlockDescriptor()
89  dataTable.getBlockOfRows(firstReadRow, nRead, readOnly, doubleBlock)
90  printArray(
91  doubleBlock.getArray(), nFeatures, doubleBlock.getNumberOfRows(), doubleBlock.getNumberOfColumns(),
92  "Print SOA data structures as double:"
93  )
94  dataTable.releaseBlockOfRows(doubleBlock)
95 
96  readFeatureIdx = 0
97  intBlock = BlockDescriptor(ntype=np.intc)
98  dataTable.getBlockOfColumnValues(readFeatureIdx, firstReadRow, nObservations, readOnly, intBlock)
99  printArray(
100  intBlock.getArray(), 1, intBlock.getNumberOfRows(), intBlock.getNumberOfColumns(),
101  "Print the first feature of SOA:", flt64=False
102  )
103  dataTable.releaseBlockOfColumnValues(intBlock)
104 
105  pDictionary = dataTable.getDictionary()
106  print("Number of features in table: " + str(pDictionary.getNumberOfFeatures()))
107  print("")
108 
109  print("Default type in autogenerated dictionary:")
110  for i in range(0, nFeatures):
111  featureType = pDictionary[i].featureType
112  print("Type of " + str(i) + " feature: " + toString(featureType))
113  print("")
114 
115  categoricalFeature = pDictionary[0]
116  categoricalFeature.featureType = data_feature_utils.DAAL_CATEGORICAL
117 
118  print("Modified type in the dictionary:")
119  for i in range(0, nFeatures):
120  featureType = pDictionary[i].featureType
121  print("Type of " + str(i) + " feature: " + toString(featureType))
122  print("")

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