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

datastructures_csr.py

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

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