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

impl_als_csr_batch.py

1 # file: impl_als_csr_batch.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-IMPLICIT_ALS_CSR_BATCH"></a>
17 ## \example impl_als_csr_batch.py
18 
19 import os
20 import sys
21 
22 import daal.algorithms.implicit_als.prediction.ratings as ratings
23 import daal.algorithms.implicit_als.training as training
24 import daal.algorithms.implicit_als.training.init as init
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 printNumericTable, createSparseTable
30 
31 DAAL_PREFIX = os.path.join('..', 'data')
32 
33 # Input data set parameters
34 trainDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'implicit_als_csr.csv')
35 
36 # Algorithm parameters
37 nFactors = 2
38 
39 dataTable = None
40 initialModel = None
41 trainingResult = None
42 
43 
44 def initializeModel():
45  global initialModel, dataTable
46 
47  # Read trainDatasetFileName from a file and create a numeric table to store the input data
48  dataTable = createSparseTable(trainDatasetFileName)
49 
50  # Create an algorithm object to initialize the implicit ALS model with the default method
51  initAlgorithm = init.Batch(method=init.fastCSR)
52  initAlgorithm.parameter.nFactors = nFactors
53 
54  # Pass a training data set and dependent values to the algorithm
55  initAlgorithm.input.set(init.data, dataTable)
56 
57  # Initialize the implicit ALS model
58  res = initAlgorithm.compute()
59  # (Result class from implicit_als.training.init)
60  initialModel = res.get(init.model)
61 
62 
63 def trainModel():
64  global trainingResult
65 
66  # Create an algorithm object to train the implicit ALS model with the default method
67  algorithm = training.Batch(method=training.fastCSR)
68 
69  # Pass a training data set and dependent values to the algorithm
70  algorithm.input.setTable(training.data, dataTable)
71  algorithm.input.setModel(training.inputModel, initialModel)
72 
73  algorithm.parameter.nFactors = nFactors
74 
75  # Build the implicit ALS model
76  # Retrieve the algorithm results
77  trainingResult = algorithm.compute()
78 
79 
80 def testModel():
81 
82  # Create an algorithm object to predict recommendations of the implicit ALS model
83  algorithm = ratings.Batch()
84  algorithm.parameter.nFactors = nFactors
85 
86  algorithm.input.set(ratings.model, trainingResult.get(training.model))
87 
88  res = algorithm.compute()
89 
90  predictedRatings = res.get(ratings.prediction)
91 
92  printNumericTable(predictedRatings, "Predicted ratings:")
93 
94 if __name__ == "__main__":
95 
96  initializeModel()
97  trainModel()
98  testModel()

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