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

kmeans_csr_distr.py

1 # file: kmeans_csr_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 
28 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
29 if utils_folder not in sys.path:
30  sys.path.insert(0, utils_folder)
31 from utils import printNumericTable, createSparseTable
32 
33 DAAL_PREFIX = os.path.join('..', 'data')
34 
35 # K-Means algorithm parameters
36 nClusters = 20
37 nIterations = 5
38 nBlocks = 4
39 nVectorsInBlock = 8000
40 
41 dataFileNames = [
42  os.path.join(DAAL_PREFIX, 'batch', 'kmeans_csr.csv'),
43  os.path.join(DAAL_PREFIX, 'batch', 'kmeans_csr.csv'),
44  os.path.join(DAAL_PREFIX, 'batch', 'kmeans_csr.csv'),
45  os.path.join(DAAL_PREFIX, 'batch', 'kmeans_csr.csv')
46 ]
47 
48 dataTable = [0] * nBlocks
49 
50 if __name__ == "__main__":
51 
52  masterAlgorithm = kmeans.Distributed(step2Master, nClusters, method=kmeans.lloydCSR, )
53 
54  centroids = None
55  assignments = [0] * nBlocks
56 
57  masterInitAlgorithm = init.Distributed(step2Master, nClusters, method=init.randomDense)
58 
59  for i in range(nBlocks):
60 
61  # Read dataFileNames and create a numeric table to store the input data
62  dataTable[i] = createSparseTable(dataFileNames[i])
63 
64  # Create an algorithm object for the K-Means algorithm
65  localInit = init.Distributed(step1Local, nClusters, nBlocks * nVectorsInBlock, i * nVectorsInBlock, method=init.randomDense)
66 
67  localInit.input.set(init.data, dataTable[i])
68  # compute and add input for next
69  masterInitAlgorithm.input.add(init.partialResults, localInit.compute())
70 
71  masterInitAlgorithm.compute()
72  res = masterInitAlgorithm.finalizeCompute()
73  centroids = res.get(init.centroids)
74 
75  for it in range(nIterations):
76  for i in range(nBlocks):
77  # Create an algorithm object for the K-Means algorithm
78  localAlgorithm = kmeans.Distributed(step1Local, nClusters, it == nIterations, method=kmeans.lloydCSR)
79 
80  # Set the input data to the algorithm
81  localAlgorithm.input.set(kmeans.data, dataTable[i])
82  localAlgorithm.input.set(kmeans.inputCentroids, centroids)
83 
84  pres = localAlgorithm.compute()
85 
86  masterAlgorithm.input.add(kmeans.partialResults, pres)
87 
88  masterAlgorithm.compute()
89  result = masterAlgorithm.finalizeCompute()
90 
91  centroids = result.get(kmeans.centroids)
92  objectiveFunction = result.get(kmeans.objectiveFunction)
93 
94  for i in range(nBlocks):
95  # Create an algorithm object for the K-Means algorithm
96  localAlgorithm = kmeans.Batch(nClusters, 0, method=kmeans.lloydCSR)
97 
98  # Set the input data to the algorithm
99  localAlgorithm.input.set(kmeans.data, dataTable[i])
100  localAlgorithm.input.set(kmeans.inputCentroids, centroids)
101 
102  res = localAlgorithm.compute()
103 
104  assignments[i] = res.get(kmeans.assignments)
105 
106  # Print the clusterization results
107  printNumericTable(assignments[0], "First 10 cluster assignments from 1st node:", 10)
108  printNumericTable(centroids, "First 10 dimensions of centroids:", 20, 10)
109  printNumericTable(objectiveFunction, "Objective function value:")

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