48 from daal
import step1Local, step2Master
49 from daal.algorithms.linear_regression
import training, prediction
50 from daal.data_management
import (
51 DataSourceIface, FileDataSource, HomogenNumericTable, MergedNumericTable, NumericTableIface
54 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
55 if utils_folder
not in sys.path:
56 sys.path.insert(0, utils_folder)
57 from utils
import printNumericTable
59 DAAL_PREFIX = os.path.join(
'..',
'data')
62 trainDatasetFileNames = [
63 os.path.join(DAAL_PREFIX,
'distributed',
'linear_regression_train_1.csv'),
64 os.path.join(DAAL_PREFIX,
'distributed',
'linear_regression_train_2.csv'),
65 os.path.join(DAAL_PREFIX,
'distributed',
'linear_regression_train_3.csv'),
66 os.path.join(DAAL_PREFIX,
'distributed',
'linear_regression_train_4.csv')
69 testDatasetFileName = os.path.join(DAAL_PREFIX,
'distributed',
'linear_regression_test.csv')
74 nDependentVariables = 2
77 predictionResult =
None
84 masterAlgorithm = training.Distributed(step2Master, method=training.qrDense)
86 for i
in range(nBlocks):
88 trainDataSource = FileDataSource(
89 trainDatasetFileNames[i], DataSourceIface.notAllocateNumericTable,
90 DataSourceIface.doDictionaryFromContext
94 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
95 trainDependentVariables = HomogenNumericTable(
96 nDependentVariables, 0, NumericTableIface.doNotAllocate
98 mergedData = MergedNumericTable(trainData, trainDependentVariables)
101 trainDataSource.loadDataBlock(mergedData)
104 localAlgorithm = training.Distributed(step1Local, method=training.qrDense)
107 localAlgorithm.input.set(training.data, trainData)
108 localAlgorithm.input.set(training.dependentVariables, trainDependentVariables)
112 masterAlgorithm.input.add(training.partialModels, localAlgorithm.compute())
115 masterAlgorithm.compute()
118 trainingResult = masterAlgorithm.finalizeCompute()
119 printNumericTable(trainingResult.get(training.model).getBeta(),
"Linear Regression coefficients:")
125 testDataSource = FileDataSource(
126 testDatasetFileName, DataSourceIface.doAllocateNumericTable,
127 DataSourceIface.doDictionaryFromContext
131 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
132 testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
133 mergedData = MergedNumericTable(testData, testGroundTruth)
136 testDataSource.loadDataBlock(mergedData)
139 algorithm = prediction.Batch()
142 algorithm.input.setTable(prediction.data, testData)
143 algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
146 predictionResult = algorithm.compute()
147 printNumericTable(predictionResult.get(prediction.prediction),
"Linear Regression prediction results: (first 10 rows):", 10)
148 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):", 10)
150 if __name__ ==
"__main__":