24 from daal
import step1Local, step2Master
25 from daal.algorithms.linear_regression
import training, prediction
26 from daal.data_management
import (
27 DataSourceIface, FileDataSource, HomogenNumericTable, MergedNumericTable, NumericTableIface
30 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
31 if utils_folder
not in sys.path:
32 sys.path.insert(0, utils_folder)
33 from utils
import printNumericTable
35 DAAL_PREFIX = os.path.join(
'..',
'data')
38 trainDatasetFileNames = [
39 os.path.join(DAAL_PREFIX,
'distributed',
'linear_regression_train_1.csv'),
40 os.path.join(DAAL_PREFIX,
'distributed',
'linear_regression_train_2.csv'),
41 os.path.join(DAAL_PREFIX,
'distributed',
'linear_regression_train_3.csv'),
42 os.path.join(DAAL_PREFIX,
'distributed',
'linear_regression_train_4.csv')
45 testDatasetFileName = os.path.join(DAAL_PREFIX,
'distributed',
'linear_regression_test.csv')
50 nDependentVariables = 2
53 predictionResult =
None
60 masterAlgorithm = training.Distributed(step2Master, method=training.qrDense)
62 for i
in range(nBlocks):
64 trainDataSource = FileDataSource(
65 trainDatasetFileNames[i], DataSourceIface.notAllocateNumericTable,
66 DataSourceIface.doDictionaryFromContext
70 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
71 trainDependentVariables = HomogenNumericTable(
72 nDependentVariables, 0, NumericTableIface.doNotAllocate
74 mergedData = MergedNumericTable(trainData, trainDependentVariables)
77 trainDataSource.loadDataBlock(mergedData)
80 localAlgorithm = training.Distributed(step1Local, method=training.qrDense)
83 localAlgorithm.input.set(training.data, trainData)
84 localAlgorithm.input.set(training.dependentVariables, trainDependentVariables)
88 masterAlgorithm.input.add(training.partialModels, localAlgorithm.compute())
91 masterAlgorithm.compute()
94 trainingResult = masterAlgorithm.finalizeCompute()
95 printNumericTable(trainingResult.get(training.model).getBeta(),
"Linear Regression coefficients:")
101 testDataSource = FileDataSource(
102 testDatasetFileName, DataSourceIface.doAllocateNumericTable,
103 DataSourceIface.doDictionaryFromContext
107 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
108 testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
109 mergedData = MergedNumericTable(testData, testGroundTruth)
112 testDataSource.loadDataBlock(mergedData)
115 algorithm = prediction.Batch()
118 algorithm.input.setTable(prediction.data, testData)
119 algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
122 predictionResult = algorithm.compute()
123 printNumericTable(predictionResult.get(prediction.prediction),
"Linear Regression prediction results: (first 10 rows):", 10)
124 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):", 10)
126 if __name__ ==
"__main__":