Python* API Reference for Intel® Data Analytics Acceleration Library 2019 Update 5

assoc_rules_apriori_batch.py

1 # file: assoc_rules_apriori_batch.py
2 #===============================================================================
3 # Copyright 2014-2019 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 
17 
18 
19 import os
20 import sys
21 
22 from daal.algorithms import association_rules
23 from daal.data_management import FileDataSource, DataSourceIface
24 
25 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
26 if utils_folder not in sys.path:
27  sys.path.insert(0, utils_folder)
28 from utils import printAprioriItemsets, printAprioriRules
29 
30 # Input data set parameters
31 datasetFileName = os.path.join('..','data','batch','apriori.csv')
32 
33 # Apriori algorithm parameters
34 minSupport = 0.001
35 minConfidence = 0.7
36 
37 # Initialize FileDataSource_CSVFeatureManager to retrieve the input data from a .csv file
38 dataSource = FileDataSource(
39  datasetFileName, DataSourceIface.doAllocateNumericTable, DataSourceIface.doDictionaryFromContext
40 )
41 
42 # Retrieve the data from the input file
43 dataSource.loadDataBlock()
44 
45 # Create an algorithm to mine association rules using the Apriori method
46 alg = association_rules.Batch()
47 alg.input.set(association_rules.data, dataSource.getNumericTable())
48 alg.parameter.minSupport = minSupport
49 alg.parameter.minConfidence = minConfidence
50 
51 # Find large item sets and construct association rules
52 res = alg.compute()
53 
54 # Get computed results of the Apriori algorithm
55 nt1 = res.get(association_rules.largeItemsets)
56 nt2 = res.get(association_rules.largeItemsetsSupport)
57 
58 nt3 = res.get(association_rules.antecedentItemsets)
59 nt4 = res.get(association_rules.consequentItemsets)
60 nt5 = res.get(association_rules.confidence)
61 
62 printAprioriItemsets(nt1, nt2)
63 printAprioriRules(nt3, nt4, nt5)

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