21 import daal.algorithms.kmeans
as kmeans
22 import daal.algorithms.kmeans.init
as init
23 from daal.data_management
import FileDataSource, DataSourceIface
24 from daal.services
import Environment
27 datasetFileName = os.path.join(
'..',
'data',
'batch',
'kmeans_dense.csv')
36 if __name__ ==
"__main__":
39 nThreadsInit = Environment.getInstance().getNumberOfThreads()
42 Environment.getInstance().setNumberOfThreads(nThreads)
45 nThreadsNew = Environment.getInstance().getNumberOfThreads()
48 dataSource = FileDataSource(
49 datasetFileName, DataSourceIface.doAllocateNumericTable,
50 DataSourceIface.doDictionaryFromContext
54 dataSource.loadDataBlock()
57 initAlg = init.Batch(nClusters)
59 initAlg.input.set(init.data, dataSource.getNumericTable())
60 res = initAlg.compute()
61 centroids = res.get(init.centroids)
64 algorithm = kmeans.Batch(nClusters, nIterations)
66 algorithm.input.set(kmeans.data, dataSource.getNumericTable())
67 algorithm.input.set(kmeans.inputCentroids, centroids)
70 unused_result = algorithm.compute()
72 print(
"Initial number of threads: {}".format(nThreadsInit))
73 print(
"Number of threads to set: {}".format(nThreads))
74 print(
"Number of threads after setting: {}".format(nThreadsNew))