47 from daal
import step1Local, step2Master
48 from daal.algorithms.linear_regression
import training, prediction
49 from daal.data_management
import (
50 DataSourceIface, FileDataSource, HomogenNumericTable, MergedNumericTable,NumericTableIface
53 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
54 if utils_folder
not in sys.path:
55 sys.path.insert(0, utils_folder)
56 from utils
import printNumericTable
58 DAAL_PREFIX = os.path.join(
'..',
'data')
60 trainDatasetFileNames = [
61 os.path.join(DAAL_PREFIX,
'distributed',
'linear_regression_train_1.csv'),
62 os.path.join(DAAL_PREFIX,
'distributed',
'linear_regression_train_2.csv'),
63 os.path.join(DAAL_PREFIX,
'distributed',
'linear_regression_train_3.csv'),
64 os.path.join(DAAL_PREFIX,
'distributed',
'linear_regression_train_4.csv')
67 testDatasetFileName = os.path.join(DAAL_PREFIX,
'distributed',
'linear_regression_test.csv')
72 nDependentVariables = 2
75 predictionResult =
None
82 masterAlgorithm = training.Distributed(step2Master)
84 for i
in range(nBlocks):
86 trainDataSource = FileDataSource(
87 trainDatasetFileNames[i], DataSourceIface.notAllocateNumericTable,
88 DataSourceIface.doDictionaryFromContext
92 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
93 trainDependentVariables = HomogenNumericTable(
94 nDependentVariables, 0, NumericTableIface.doNotAllocate
96 mergedData = MergedNumericTable(trainData, trainDependentVariables)
99 trainDataSource.loadDataBlock(mergedData)
102 localAlgorithm = training.Distributed(step1Local)
105 localAlgorithm.input.set(training.data, trainData)
106 localAlgorithm.input.set(training.dependentVariables, trainDependentVariables)
110 masterAlgorithm.input.add(training.partialModels, localAlgorithm.compute())
113 masterAlgorithm.compute()
116 trainingResult = masterAlgorithm.finalizeCompute()
117 printNumericTable(trainingResult.get(training.model).getBeta(),
"Linear Regression coefficients:")
121 global trainingResult, predictionResult
124 testDataSource = FileDataSource(
125 testDatasetFileName, DataSourceIface.doAllocateNumericTable,
126 DataSourceIface.doDictionaryFromContext
130 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
131 testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
132 mergedData = MergedNumericTable(testData, testGroundTruth)
135 testDataSource.loadDataBlock(mergedData)
138 algorithm = prediction.Batch()
141 algorithm.input.setTable(prediction.data, testData)
142 algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
145 predictionResult = algorithm.compute()
146 printNumericTable(predictionResult.get(prediction.prediction),
"Linear Regression prediction results: (first 10 rows):", 10)
147 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):", 10)
149 if __name__ ==
"__main__":