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

datastructures_packedsymmetric.py

1 # file: datastructures_packedsymmetric.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 # ! Content:
20 # ! Python example of using packed data structures
21 # !*****************************************************************************
22 
23 #
24 
25 
26 #
27 
28 import os
29 import sys
30 
31 import numpy as np
32 
33 from daal.data_management import PackedSymmetricMatrix, NumericTableIface, BlockDescriptor, readOnly, readWrite
34 
35 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
36 if utils_folder not in sys.path:
37  sys.path.insert(0, utils_folder)
38 from utils import printArray
39 
40 
41 if __name__ == "__main__":
42 
43  print("Packed symmetric matrix example\n")
44 
45  nDim = 5
46  firstReadRow = 0
47  nRead = 5
48 
49  # Example of using a packed symmetric 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 = PackedSymmetricMatrix(NumericTableIface.lowerPackedSymmetricMatrix, 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  printArray(block.getArray(), nDim, block.getNumberOfRows(), block.getNumberOfColumns(),
60  "Print 3 rows from packed symmetric matrix as float:")
61 
62  # Read a feature(column) and write into it
63  readFeatureIdx = 2
64  dataTable.getBlockOfColumnValues(readFeatureIdx, firstReadRow, nDim, readWrite, block)
65  printArray(block.getArray(), 1, block.getNumberOfRows(), block.getNumberOfColumns(),
66  "Print the third feature of packed symmetric matrix:")
67 
68  # Set new value to a buffer and release it
69  dataBlock = block.getArray()
70  dataBlock[readFeatureIdx - 1] = -1
71  dataBlock[readFeatureIdx + 1] = -2
72  dataTable.releaseBlockOfColumnValues(block)
73 
74  # Read a block of rows. Ensure that data has changed
75  dataTable.getBlockOfRows(firstReadRow, nRead, readOnly, block)
76  print("{} rows are read".format(block.getNumberOfRows()))
77  printArray(block.getArray(), nDim, block.getNumberOfRows(), block.getNumberOfColumns(),
78  "Print 3 rows from packed symmetric matrix as float:")

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