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

datastructures_matrix.py

1 # file: datastructures_matrix.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 matrix data structures
21 # !*****************************************************************************
22 
23 #
24 
25 
26 #
27 
28 import sys, os
29 import numpy as np
30 from daal.data_management import Matrix, BlockDescriptor, readOnly
31 
32 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
33 if utils_folder not in sys.path:
34  sys.path.insert(0, utils_folder)
35 from utils import printArray
36 
37 if __name__ == "__main__":
38 
39  print("Matrix numeric table example\n")
40 
41  nObservations = 10
42  nFeatures = 11
43  firstReadRow = 0
44  nRead = 5
45 
46  #Example of using a matrix
47  data = np.array([(0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1),
48  (1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2),
49  (2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3),
50  (3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4),
51  (4.0, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5),
52  (5.0, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 1),
53  (6.0, 6.1, 6.2, 6.3, 6.4, 6.5, 6.6, 6.7, 6.8, 6.9, 2),
54  (7.0, 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7, 7.8, 7.9, 3),
55  (8.0, 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7, 8.8, 8.9, 4),
56  (9.0, 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9, 5)], dtype=np.float32)
57 
58  dataTable = Matrix(data, ntype=np.float32)
59 
60  block = BlockDescriptor()
61 
62  # Read a block of rows
63  dataTable.getBlockOfRows(firstReadRow, nRead, readOnly, block)
64  print(str(block.getNumberOfRows()) + " rows are read")
65  printArray(block.getArray(), nFeatures, nRead, block.getNumberOfColumns(), "Print 5 rows from matrix data array as float:")
66  dataTable.releaseBlockOfRows(block)
67 
68  readFeatureIdx = 2
69 
70  # Set new values in Matrix
71  npdt = dataTable.getArray()
72  npdt[firstReadRow, readFeatureIdx] = -1
73  npdt[firstReadRow + 1, readFeatureIdx] = -2
74  npdt[firstReadRow + 2, readFeatureIdx] = -3
75 
76  # Read a feature(column) and print it
77  dataTable.getBlockOfColumnValues(readFeatureIdx, firstReadRow, nObservations, readOnly, block)
78  printArray(block.getArray(), 1, block.getNumberOfRows(), block.getNumberOfColumns(), "Print the third feature of matrix data:")
79  dataTable.releaseBlockOfColumnValues(block)

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