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

basic_statistics.py

1 # file: basic_statistics.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 for using of basic statistics
21 # !*****************************************************************************
22 
23 #
24 
25 
26 #
27 
28 import os
29 import sys
30 import numpy as np
31 
32 from daal.data_management import HomogenNumericTable, NumericTableIface, FileDataSource, DataSourceIface
33 
34 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
35 if utils_folder not in sys.path:
36  sys.path.insert(0, utils_folder)
37 from utils import printNumericTable
38 
39 
40 if __name__ == "__main__":
41 
42  print("Basic statistics example\n")
43 
44  # Input data set parameters
45  datasetFileName = "../data/batch/basic_statistics.csv"
46  data = np.array([(7.0, 3.0, 6.0, 2.0),
47  (1.0, 3.0, 0.0, 2.0),
48  (9.0, 2.0, 6.0, 2.0),
49  (3.0, 4.0, 7.0, 2.0),])
50 
51  # Initialize FileDataSource to retrieve the input data from a .csv file
52  dataSource = FileDataSource(datasetFileName, DataSourceIface.doAllocateNumericTable)
53 
54  dataSource.createDictionaryFromContext()
55  dataSource.loadDataBlock()
56  table = dataSource.getNumericTable()
57 
58  # Get basic statistics from the table. They were calculated inside DataSource for each column.
59  min = table.basicStatistics.get(NumericTableIface.minimum)
60  max = table.basicStatistics.get(NumericTableIface.maximum)
61  sum = table.basicStatistics.get(NumericTableIface.sum)
62  sumSquares = table.basicStatistics.get(NumericTableIface.sumSquares)
63 
64  # Print calculated basic statistics
65  printNumericTable(table, "Basic statistics of table:")
66  printNumericTable(min, "Minimum:")
67  printNumericTable(max, "Maximum:")
68  printNumericTable(sum, "Sum:")
69  printNumericTable(sumSquares, "SumSquares:")
70 
71  # Create NumericTable with the same data. But in this case basic statistics are not calculated.
72  dataTable = HomogenNumericTable(data)
73 
74  # Set basic statistics in the new NumericTable
75  dataTable.basicStatistics.set(NumericTableIface.minimum, min);
76  dataTable.basicStatistics.set(NumericTableIface.maximum, max);
77  dataTable.basicStatistics.set(NumericTableIface.sum, sum);
78  dataTable.basicStatistics.set(NumericTableIface.sumSquares, sumSquares);
79 
80  # Print basic statistics those were set
81  printNumericTable(dataTable, "New table:")
82  printNumericTable(dataTable.basicStatistics.get(NumericTableIface.minimum), "Minimum:")
83  printNumericTable(dataTable.basicStatistics.get(NumericTableIface.maximum), "Maximum:")
84  printNumericTable(dataTable.basicStatistics.get(NumericTableIface.sum), "Sum:")
85  printNumericTable(dataTable.basicStatistics.get(NumericTableIface.sumSquares), "SumSquares:")

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