22 from daal
import step1Local, step2Master
23 from daal.algorithms.linear_regression
import training, prediction
24 from daal.data_management
import (
25 DataSourceIface, FileDataSource, HomogenNumericTable, MergedNumericTable,NumericTableIface
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
33 DAAL_PREFIX = os.path.join(
'..',
'data')
35 trainDatasetFileNames = [
36 os.path.join(DAAL_PREFIX,
'distributed',
'linear_regression_train_1.csv'),
37 os.path.join(DAAL_PREFIX,
'distributed',
'linear_regression_train_2.csv'),
38 os.path.join(DAAL_PREFIX,
'distributed',
'linear_regression_train_3.csv'),
39 os.path.join(DAAL_PREFIX,
'distributed',
'linear_regression_train_4.csv')
42 testDatasetFileName = os.path.join(DAAL_PREFIX,
'distributed',
'linear_regression_test.csv')
47 nDependentVariables = 2
50 predictionResult =
None
57 masterAlgorithm = training.Distributed(step2Master)
59 for i
in range(nBlocks):
61 trainDataSource = FileDataSource(
62 trainDatasetFileNames[i], DataSourceIface.notAllocateNumericTable,
63 DataSourceIface.doDictionaryFromContext
67 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
68 trainDependentVariables = HomogenNumericTable(
69 nDependentVariables, 0, NumericTableIface.doNotAllocate
71 mergedData = MergedNumericTable(trainData, trainDependentVariables)
74 trainDataSource.loadDataBlock(mergedData)
77 localAlgorithm = training.Distributed(step1Local)
80 localAlgorithm.input.set(training.data, trainData)
81 localAlgorithm.input.set(training.dependentVariables, trainDependentVariables)
85 masterAlgorithm.input.add(training.partialModels, localAlgorithm.compute())
88 masterAlgorithm.compute()
91 trainingResult = masterAlgorithm.finalizeCompute()
92 printNumericTable(trainingResult.get(training.model).getBeta(),
"Linear Regression coefficients:")
96 global trainingResult, predictionResult
99 testDataSource = FileDataSource(
100 testDatasetFileName, DataSourceIface.doAllocateNumericTable,
101 DataSourceIface.doDictionaryFromContext
105 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
106 testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
107 mergedData = MergedNumericTable(testData, testGroundTruth)
110 testDataSource.loadDataBlock(mergedData)
113 algorithm = prediction.Batch()
116 algorithm.input.setTable(prediction.data, testData)
117 algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
120 predictionResult = algorithm.compute()
121 printNumericTable(predictionResult.get(prediction.prediction),
"Linear Regression prediction results: (first 10 rows):", 10)
122 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):", 10)
124 if __name__ ==
"__main__":