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

datastructures_soa.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 ## <a name="DAAL-EXAMPLE-PY-DATASTRUCTURES_SOA"></a>
42 ## \example datastructures_soa.py
43 
44 import os
45 import sys
46 
47 import numpy as np
48 
49 from daal.data_management import BlockDescriptor, SOANumericTable, data_feature_utils, readOnly
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 def toString(v):
58  if v == data_feature_utils.DAAL_CATEGORICAL:
59  return "DAAL_CATEGORICAL"
60  elif v == data_feature_utils.DAAL_ORDINAL:
61  return "DAAL_ORDINAL"
62  elif v == data_feature_utils.DAAL_CONTINUOUS:
63  return "DAAL_CONTINUOUS"
64  else:
65  return "[Unknown FeatureType]"
66 
67 
68 if __name__ == "__main__":
69  print("Structure of array (SOA) numeric table example\n")
70 
71  firstReadRow = 0
72  nRead = 3
73  readFeatureIdx = None
74  nObservations = 10
75  nFeatures = 4
76  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)
77  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)
78  iDataSOA = np.array([-10, -20, -30, -40, -50, -60, -70, -80, -90, -100], dtype=np.int32)
79  cDataSOA = np.array([1, 2, 3, 4, 5, 1, 2, 3, 4, 5], dtype=np.uint8)
80 
81  dataTable = SOANumericTable(nFeatures, nObservations)
82  dataTable.setArray(cDataSOA, 0)
83  dataTable.setArray(fDataSOA, 1)
84  dataTable.setArray(dDataSOA, 2)
85  dataTable.setArray(iDataSOA, 3)
86 
87  doubleBlock = BlockDescriptor()
88  dataTable.getBlockOfRows(firstReadRow, nRead, readOnly, doubleBlock)
89  printArray(
90  doubleBlock.getArray(), nFeatures, doubleBlock.getNumberOfRows(), doubleBlock.getNumberOfColumns(),
91  "Print SOA data structures as double:"
92  )
93  dataTable.releaseBlockOfRows(doubleBlock)
94 
95  readFeatureIdx = 0
96  intBlock = BlockDescriptor(ntype=np.intc)
97  dataTable.getBlockOfColumnValues(readFeatureIdx, firstReadRow, nObservations, readOnly, intBlock)
98  printArray(
99  intBlock.getArray(), 1, intBlock.getNumberOfRows(), intBlock.getNumberOfColumns(),
100  "Print the first feature of SOA:", flt64=False
101  )
102  dataTable.releaseBlockOfColumnValues(intBlock)
103 
104  pDictionary = dataTable.getDictionary()
105  print("Number of features in table: " + str(pDictionary.getNumberOfFeatures()))
106  print("")
107 
108  print("Default type in autogenerated dictionary:")
109  for i in range(0, nFeatures):
110  featureType = pDictionary[i].featureType
111  print("Type of " + str(i) + " feature: " + toString(featureType))
112  print("")
113 
114  categoricalFeature = pDictionary[0]
115  categoricalFeature.featureType = data_feature_utils.DAAL_CATEGORICAL
116 
117  print("Modified type in the dictionary:")
118  for i in range(0, nFeatures):
119  featureType = pDictionary[i].featureType
120  print("Type of " + str(i) + " feature: " + toString(featureType))
121  print("")

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