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

kmeans_dense_distr.py

1 # file: kmeans_dense_distr.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 
20 
21 import os
22 import sys
23 
24 import daal.algorithms.kmeans as kmeans
25 import daal.algorithms.kmeans.init as init
26 from daal import step1Local, step2Master
27 from daal.data_management import FileDataSource, DataSourceIface
28 
29 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
30 if utils_folder not in sys.path:
31  sys.path.insert(0, utils_folder)
32 from utils import printNumericTable
33 
34 DAAL_PREFIX = os.path.join('..', 'data')
35 
36 dataFileNames = [
37  os.path.join(DAAL_PREFIX, 'distributed', 'kmeans_dense_1.csv'),
38  os.path.join(DAAL_PREFIX, 'distributed', 'kmeans_dense_2.csv'),
39  os.path.join(DAAL_PREFIX, 'distributed', 'kmeans_dense_3.csv'),
40  os.path.join(DAAL_PREFIX, 'distributed', 'kmeans_dense_4.csv')
41 ]
42 
43 nClusters = 20
44 nIterations = 5
45 nBlocks = 4
46 nVectorsInBlock = 2500
47 
48 dataTable = [0] * nBlocks
49 
50 if __name__ == "__main__":
51 
52  masterAlgorithm = kmeans.Distributed(step2Master, nClusters, method=kmeans.lloydDense)
53 
54  centroids = None
55  assignments = [0] * nBlocks
56 
57  masterInitAlgorithm = init.Distributed(step2Master, nClusters, method=init.randomDense)
58  for i in range(nBlocks):
59  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
60  dataSource = FileDataSource(
61  dataFileNames[i], DataSourceIface.doAllocateNumericTable,
62  DataSourceIface.doDictionaryFromContext
63  )
64 
65  # Retrieve the data from the input file
66  dataSource.loadDataBlock()
67 
68  dataTable[i] = dataSource.getNumericTable()
69 
70  # Create an algorithm object for the K-Means algorithm
71  localInit = init.Distributed(step1Local, nClusters, nBlocks * nVectorsInBlock, i * nVectorsInBlock, method=init.randomDense)
72 
73  localInit.input.set(init.data, dataTable[i])
74  res = localInit.compute()
75  masterInitAlgorithm.input.add(init.partialResults, res)
76 
77  masterInitAlgorithm.compute()
78  res = masterInitAlgorithm.finalizeCompute()
79  centroids = res.get(init.centroids)
80 
81  for it in range(nIterations):
82  for i in range(nBlocks):
83  # Create an algorithm object for the K-Means algorithm
84  localAlgorithm = kmeans.Distributed(step1Local, nClusters, it == nIterations, method=kmeans.lloydDense)
85 
86  # Set the input data to the algorithm
87  localAlgorithm.input.set(kmeans.data, dataTable[i])
88  localAlgorithm.input.set(kmeans.inputCentroids, centroids)
89 
90  pres = localAlgorithm.compute()
91 
92  masterAlgorithm.input.add(kmeans.partialResults, pres)
93 
94  masterAlgorithm.compute()
95  result = masterAlgorithm.finalizeCompute()
96 
97  centroids = result.get(kmeans.centroids)
98  goalFunction = result.get(kmeans.goalFunction)
99 
100  for i in range(nBlocks):
101  # Create an algorithm object for the K-Means algorithm
102  localAlgorithm = kmeans.Batch(nClusters, 0, method=kmeans.lloydDense)
103 
104  # Set the input data to the algorithm
105  localAlgorithm.input.set(kmeans.data, dataTable[i])
106  localAlgorithm.input.set(kmeans.inputCentroids, centroids)
107 
108  res = localAlgorithm.compute()
109 
110  assignments[i] = res.get(kmeans.assignments)
111 
112  # Print the clusterization results
113  printNumericTable(assignments[0], "First 10 cluster assignments from 1st node:", 10)
114  printNumericTable(centroids, "First 10 dimensions of centroids:", 20, 10)
115  printNumericTable(goalFunction, "Goal function value:")

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