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

datastructures_csr.py

1 # file: datastructures_csr.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 
20 
21 import os
22 import sys
23 
24 import numpy as np
25 
26 from daal.data_management import BlockDescriptor, CSRBlockDescriptor, CSRNumericTable, readOnly, readWrite
27 
28 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
29 if utils_folder not in sys.path:
30  sys.path.insert(0, utils_folder)
31 from utils import printArray
32 
33 
34 if __name__ == "__main__":
35 
36  print("Compressed spares rows (CSR) numeric table example\n")
37 
38  nObservations = 5
39  nFeatures = 5
40  firstReadRow = 1
41  nRead = 3
42 
43  # Example of using CSR numeric table
44  values = np.array([1, -1, -3, -2, 5, 4, 6, 4, -4, 2, 7, 8, -5], dtype=np.float64)
45  colIndices = np.array([1, 2, 4, 1, 2, 3, 4, 5, 1, 3, 4, 2, 5], dtype=np.uint64)
46  rowOffsets = np.array([1, 4, 6, 9, 12, 14], dtype=np.uint64)
47 
48  dataTable = CSRNumericTable(values, colIndices, rowOffsets, nFeatures, nObservations)
49 
50  # Read block of rows in dense format
51  block = BlockDescriptor(ntype=np.float64)
52  dataTable.getBlockOfRows(firstReadRow, nRead, readOnly, block)
53  print(str(block.getNumberOfRows()) + " rows are read\n")
54  printArray(
55  block.getArray(), nFeatures, block.getNumberOfRows(), block.getNumberOfColumns(),
56  "Print 3 rows from CSR data array as dense double array:"
57  )
58  dataTable.releaseBlockOfRows(block)
59 
60  # Read block of rows in CSR format and write into it
61  csrBlock = CSRBlockDescriptor(ntpye=np.float32)
62  num_cols = csrBlock.getNumberOfColumns()
63  dataTable.getSparseBlock(firstReadRow, nRead, readWrite, csrBlock)
64  valuesBlock = csrBlock.getBlockValues()
65  nValuesInBlock = csrBlock.getDataSize()
66  printArray(valuesBlock, nValuesInBlock, 1, num_cols, "Values in 3 rows from CSR data array:")
67  printArray(
68  csrBlock.getBlockColumnIndices(), nValuesInBlock, 1, num_cols,
69  "Columns indices in 3 rows from CSR data array:", flt64=False
70  )
71  printArray(
72  csrBlock.getBlockRowIndices(), nRead + 1, 1, num_cols,
73  "Rows offsets in 3 rows from CSR data array:", flt64=False
74  )
75 
76  for i in range(nValuesInBlock):
77  valuesBlock[i] = -(1.0 + i)
78 
79  dataTable.releaseSparseBlock(csrBlock)
80 
81  # Read block of rows in dense format
82  dataTable.getBlockOfRows(firstReadRow, nRead, readOnly, block)
83  print(str(block.getNumberOfRows()) + " rows are read\n")
84  printArray(
85  block.getArray(), nFeatures, block.getNumberOfRows(), block.getNumberOfColumns(),
86  "Print 3 rows from CSR data array as dense double array:"
87  )
88  dataTable.releaseBlockOfRows(block)

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