46 import daal.algorithms.kmeans
as kmeans
47 import daal.algorithms.kmeans.init
as init
48 from daal.data_management
import FileDataSource, DataSourceIface
49 from daal.services
import Environment
52 datasetFileName = os.path.join(
'..',
'data',
'batch',
'kmeans_dense.csv')
61 if __name__ ==
"__main__":
64 nThreadsInit = Environment.getInstance().getNumberOfThreads()
67 Environment.getInstance().setNumberOfThreads(nThreads)
70 nThreadsNew = Environment.getInstance().getNumberOfThreads()
73 dataSource = FileDataSource(
74 datasetFileName, DataSourceIface.doAllocateNumericTable,
75 DataSourceIface.doDictionaryFromContext
79 dataSource.loadDataBlock()
82 initAlg = init.Batch(nClusters)
84 initAlg.input.set(init.data, dataSource.getNumericTable())
85 res = initAlg.compute()
86 centroids = res.get(init.centroids)
89 algorithm = kmeans.Batch(nClusters, nIterations)
91 algorithm.input.set(kmeans.data, dataSource.getNumericTable())
92 algorithm.input.set(kmeans.inputCentroids, centroids)
95 unused_result = algorithm.compute()
97 print(
"Initial number of threads: {}".format(nThreadsInit))
98 print(
"Number of threads to set: {}".format(nThreads))
99 print(
"Number of threads after setting: {}".format(nThreadsNew))