24 from daal.algorithms.linear_regression
import training, prediction
25 from daal.data_management
import (
26 DataSourceIface, FileDataSource, HomogenNumericTable, MergedNumericTable, NumericTableIface
29 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
30 if utils_folder
not in sys.path:
31 sys.path.insert(0, utils_folder)
32 from utils
import printNumericTable
34 DAAL_PREFIX = os.path.join(
'..',
'data')
37 trainDatasetFileName = os.path.join(DAAL_PREFIX,
'online',
'linear_regression_train.csv')
38 testDatasetFileName = os.path.join(DAAL_PREFIX,
'online',
'linear_regression_test.csv')
40 nTrainVectorsInBlock = 250
43 nDependentVariables = 2
46 predictionResult =
None
53 trainDataSource = FileDataSource(
54 trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
55 DataSourceIface.doDictionaryFromContext
59 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
60 trainDependentVariables = HomogenNumericTable(
61 nDependentVariables, 0, NumericTableIface.doNotAllocate
63 mergedData = MergedNumericTable(trainData, trainDependentVariables)
66 algorithm = training.Online()
68 while(trainDataSource.loadDataBlock(nTrainVectorsInBlock, mergedData) == nTrainVectorsInBlock):
70 algorithm.input.set(training.data, trainData)
71 algorithm.input.set(training.dependentVariables, trainDependentVariables)
77 trainingResult = algorithm.finalizeCompute()
79 printNumericTable(trainingResult.get(training.model).getBeta(),
"Linear Regression coefficients:")
83 global trainingResult, predictionResult
86 testDataSource = FileDataSource(
87 testDatasetFileName, DataSourceIface.doAllocateNumericTable,
88 DataSourceIface.doDictionaryFromContext
92 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
93 testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
94 mergedData = MergedNumericTable(testData, testGroundTruth)
97 testDataSource.loadDataBlock(mergedData)
100 algorithm = prediction.Batch()
103 algorithm.input.setTable(prediction.data, testData)
104 algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
107 predictionResult = algorithm.compute()
108 printNumericTable(predictionResult.get(prediction.prediction),
"Linear Regression prediction results: (first 10 rows):", 10)
109 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):", 10)
111 if __name__ ==
"__main__":