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

datastructures_csr.py

1 #===============================================================================
2 # Copyright 2014-2017 Intel Corporation
3 # All Rights Reserved.
4 #
5 # If this software was obtained under the Intel Simplified Software License,
6 # the following terms apply:
7 #
8 # The source code, information and material ("Material") contained herein is
9 # owned by Intel Corporation or its suppliers or licensors, and title to such
10 # Material remains with Intel Corporation or its suppliers or licensors. The
11 # Material contains proprietary information of Intel or its suppliers and
12 # licensors. The Material is protected by worldwide copyright laws and treaty
13 # provisions. No part of the Material may be used, copied, reproduced,
14 # modified, published, uploaded, posted, transmitted, distributed or disclosed
15 # in any way without Intel's prior express written permission. No license under
16 # any patent, copyright or other intellectual property rights in the Material
17 # is granted to or conferred upon you, either expressly, by implication,
18 # inducement, estoppel or otherwise. Any license under such intellectual
19 # property rights must be express and approved by Intel in writing.
20 #
21 # Unless otherwise agreed by Intel in writing, you may not remove or alter this
22 # notice or any other notice embedded in Materials by Intel or Intel's
23 # suppliers or licensors in any way.
24 #
25 #
26 # If this software was obtained under the Apache License, Version 2.0 (the
27 # "License"), the following terms apply:
28 #
29 # You may not use this file except in compliance with the License. You may
30 # obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
31 #
32 #
33 # Unless required by applicable law or agreed to in writing, software
34 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
35 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 #
37 # See the License for the specific language governing permissions and
38 # limitations under the License.
39 #===============================================================================
40 
41 ## <a name="DAAL-EXAMPLE-PY-DATASTRUCTURES_CSR">
42 ## \example datastructures_csr.py
43 
44 import os
45 import sys
46 
47 import numpy as np
48 
49 from daal.data_management import BlockDescriptor, CSRBlockDescriptor, CSRNumericTable, readOnly, readWrite
50 
51 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
52 if utils_folder not in sys.path:
53  sys.path.insert(0, utils_folder)
54 from utils import printArray
55 
56 
57 if __name__ == "__main__":
58 
59  print("Compressed spares rows (CSR) numeric table example\n")
60 
61  nObservations = 5
62  nFeatures = 5
63  firstReadRow = 1
64  nRead = 3
65 
66  # Example of using CSR numeric table
67  values = np.array([1, -1, -3, -2, 5, 4, 6, 4, -4, 2, 7, 8, -5], dtype=np.float64)
68  colIndices = np.array([1, 2, 4, 1, 2, 3, 4, 5, 1, 3, 4, 2, 5], dtype=np.uint64)
69  rowOffsets = np.array([1, 4, 6, 9, 12, 14], dtype=np.uint64)
70 
71  dataTable = CSRNumericTable(values, colIndices, rowOffsets, nFeatures, nObservations)
72 
73  # Read block of rows in dense format
74  block = BlockDescriptor(ntype=np.float64)
75  dataTable.getBlockOfRows(firstReadRow, nRead, readOnly, block)
76  print(str(block.getNumberOfRows()) + " rows are read\n")
77  printArray(
78  block.getArray(), nFeatures, block.getNumberOfRows(), block.getNumberOfColumns(),
79  "Print 3 rows from CSR data array as dense double array:"
80  )
81  dataTable.releaseBlockOfRows(block)
82 
83  # Read block of rows in CSR format and write into it
84  csrBlock = CSRBlockDescriptor(ntpye=np.float32)
85  num_cols = csrBlock.getNumberOfColumns()
86  dataTable.getSparseBlock(firstReadRow, nRead, readWrite, csrBlock)
87  valuesBlock = csrBlock.getBlockValues()
88  nValuesInBlock = csrBlock.getDataSize()
89  printArray(valuesBlock, nValuesInBlock, 1, num_cols, "Values in 3 rows from CSR data array:")
90  printArray(
91  csrBlock.getBlockColumnIndices(), nValuesInBlock, 1, num_cols,
92  "Columns indices in 3 rows from CSR data array:", flt64=False
93  )
94  printArray(
95  csrBlock.getBlockRowIndices(), nRead + 1, 1, num_cols,
96  "Rows offsets in 3 rows from CSR data array:", flt64=False
97  )
98 
99  for i in range(nValuesInBlock):
100  valuesBlock[i] = -(1.0 + i)
101 
102  dataTable.releaseSparseBlock(csrBlock)
103 
104  # Read block of rows in dense format
105  dataTable.getBlockOfRows(firstReadRow, nRead, readOnly, block)
106  print(str(block.getNumberOfRows()) + " rows are read\n")
107  printArray(
108  block.getArray(), nFeatures, block.getNumberOfRows(), block.getNumberOfColumns(),
109  "Print 3 rows from CSR data array as dense double array:"
110  )
111  dataTable.releaseBlockOfRows(block)

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