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

datastructures_packedtriangular.py

1 # file: datastructures_packedtriangular.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 # ! Content:
18 # ! Python example of using packed data structures
19 # !*****************************************************************************
20 
21 #
22 ## <a name = "DAAL-EXAMPLE-PY-DATASTRUCTURES_PACKEDTRIANGULAR"></a>
23 ## \example datastructures_packedtriangular.py
24 #
25 from __future__ import print_function
26 
27 import os
28 import sys
29 
30 import numpy as np
31 
32 from daal.data_management import PackedTriangularMatrix, NumericTableIface, BlockDescriptor, readOnly, readWrite
33 
34 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
35 if utils_folder not in sys.path:
36  sys.path.insert(0, utils_folder)
37 from utils import printArray
38 
39 
40 if __name__ == "__main__":
41 
42  print("Packed triangular matrix example")
43  print()
44 
45  nDim = 5
46  firstReadRow = 0
47  nRead = 5
48 
49  # Example of using a packed triangular matrix
50  data = np.array([0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4], dtype=np.float64)
51 
52  dataTable = PackedTriangularMatrix(NumericTableIface.lowerPackedTriangularMatrix, data)
53 
54  block = BlockDescriptor()
55 
56  # Read a block of rows
57  dataTable.getBlockOfRows(firstReadRow, nRead, readOnly, block)
58  print("{} rows are read".format(block.getNumberOfRows()))
59 
60  printArray(block.getArray(), nDim, block.getNumberOfRows(), block.getNumberOfColumns(),
61  "Print 3 rows from packed triangular matrix as float:")
62 
63  # Read a feature(column) and write into it
64  readFeatureIdx = 2
65  dataTable.getBlockOfColumnValues(readFeatureIdx, firstReadRow, nDim, readWrite, block)
66  printArray(block.getArray(), 1, block.getNumberOfRows(), block.getNumberOfColumns(),
67  "Print the third feature of packed triangular matrix:")
68 
69  # Set new value to a buffer and release it
70  dataBlock = block.getArray()
71  dataBlock[readFeatureIdx - 1] = -1
72  dataBlock[readFeatureIdx + 1] = -2
73  dataTable.releaseBlockOfColumnValues(block)
74 
75  # Read a block of rows. Ensure that data has changed
76  dataTable.getBlockOfRows(firstReadRow, nRead, readOnly, block)
77  print("{} rows are read".format(block.getNumberOfRows()))
78  printArray(block.getArray(), nDim, block.getNumberOfRows(), block.getNumberOfColumns(),
79  "Print 3 rows from packed triangular matrix as float:")

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