23 import daal.algorithms.kmeans
as kmeans
24 import daal.algorithms.kmeans.init
as init
25 from daal.data_management
import FileDataSource, DataSourceIface
26 from daal.services
import Environment
29 datasetFileName = os.path.join(
'..',
'data',
'batch',
'kmeans_dense.csv')
38 if __name__ ==
"__main__":
41 nThreadsInit = Environment.getInstance().getNumberOfThreads()
44 Environment.getInstance().setNumberOfThreads(nThreads)
47 nThreadsNew = Environment.getInstance().getNumberOfThreads()
50 dataSource = FileDataSource(
51 datasetFileName, DataSourceIface.doAllocateNumericTable,
52 DataSourceIface.doDictionaryFromContext
56 dataSource.loadDataBlock()
59 initAlg = init.Batch(nClusters)
61 initAlg.input.set(init.data, dataSource.getNumericTable())
62 res = initAlg.compute()
63 centroids = res.get(init.centroids)
66 algorithm = kmeans.Batch(nClusters, nIterations)
68 algorithm.input.set(kmeans.data, dataSource.getNumericTable())
69 algorithm.input.set(kmeans.inputCentroids, centroids)
72 unused_result = algorithm.compute()
74 print(
"Initial number of threads: {}".format(nThreadsInit))
75 print(
"Number of threads to set: {}".format(nThreads))
76 print(
"Number of threads after setting: {}".format(nThreadsNew))