22 from daal.algorithms.linear_regression
import training, prediction
23 from daal.data_management
import (
24 DataSourceIface, FileDataSource, HomogenNumericTable, MergedNumericTable, NumericTableIface
27 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
28 if utils_folder
not in sys.path:
29 sys.path.insert(0, utils_folder)
30 from utils
import printNumericTable
32 DAAL_PREFIX = os.path.join(
'..',
'data')
35 trainDatasetFileName = os.path.join(DAAL_PREFIX,
'online',
'linear_regression_train.csv')
36 testDatasetFileName = os.path.join(DAAL_PREFIX,
'online',
'linear_regression_test.csv')
38 nTrainVectorsInBlock = 250
41 nDependentVariables = 2
44 predictionResult =
None
51 trainDataSource = FileDataSource(
52 trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
53 DataSourceIface.doDictionaryFromContext
57 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
58 trainDependentVariables = HomogenNumericTable(
59 nDependentVariables, 0, NumericTableIface.doNotAllocate
61 mergedData = MergedNumericTable(trainData, trainDependentVariables)
64 algorithm = training.Online(method=training.qrDense)
66 while(trainDataSource.loadDataBlock(nTrainVectorsInBlock, mergedData) == nTrainVectorsInBlock):
68 algorithm.input.set(training.data, trainData)
69 algorithm.input.set(training.dependentVariables, trainDependentVariables)
75 trainingResult = algorithm.finalizeCompute()
76 printNumericTable(trainingResult.get(training.model).getBeta(),
"Linear Regression coefficients:")
80 global trainingResult, predictionResult
83 testDataSource = FileDataSource(
84 testDatasetFileName, DataSourceIface.doAllocateNumericTable,
85 DataSourceIface.doDictionaryFromContext
89 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
90 testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
91 mergedData = MergedNumericTable(testData, testGroundTruth)
94 testDataSource.loadDataBlock(mergedData)
97 algorithm = prediction.Batch()
100 algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
101 algorithm.input.setTable(prediction.data, testData)
104 predictionResult = algorithm.compute()
105 printNumericTable(predictionResult.get(prediction.prediction),
"Linear Regression prediction results: (first 10 rows):", 10)
106 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):", 10)
108 if __name__ ==
"__main__":