47 from daal.algorithms.linear_regression
import training, prediction
48 from daal.data_management
import (
49 DataSourceIface, FileDataSource, HomogenNumericTable, MergedNumericTable, NumericTableIface
52 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
53 if utils_folder
not in sys.path:
54 sys.path.insert(0, utils_folder)
55 from utils
import printNumericTable
57 DAAL_PREFIX = os.path.join(
'..',
'data')
60 trainDatasetFileName = os.path.join(DAAL_PREFIX,
'online',
'linear_regression_train.csv')
61 testDatasetFileName = os.path.join(DAAL_PREFIX,
'online',
'linear_regression_test.csv')
63 nTrainVectorsInBlock = 250
66 nDependentVariables = 2
69 predictionResult =
None
76 trainDataSource = FileDataSource(
77 trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
78 DataSourceIface.doDictionaryFromContext
82 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
83 trainDependentVariables = HomogenNumericTable(
84 nDependentVariables, 0, NumericTableIface.doNotAllocate
86 mergedData = MergedNumericTable(trainData, trainDependentVariables)
89 algorithm = training.Online()
91 while(trainDataSource.loadDataBlock(nTrainVectorsInBlock, mergedData) == nTrainVectorsInBlock):
93 algorithm.input.set(training.data, trainData)
94 algorithm.input.set(training.dependentVariables, trainDependentVariables)
100 trainingResult = algorithm.finalizeCompute()
102 printNumericTable(trainingResult.get(training.model).getBeta(),
"Linear Regression coefficients:")
106 global trainingResult, predictionResult
109 testDataSource = FileDataSource(
110 testDatasetFileName, DataSourceIface.doAllocateNumericTable,
111 DataSourceIface.doDictionaryFromContext
115 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
116 testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
117 mergedData = MergedNumericTable(testData, testGroundTruth)
120 testDataSource.loadDataBlock(mergedData)
123 algorithm = prediction.Batch()
126 algorithm.input.setTable(prediction.data, testData)
127 algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
130 predictionResult = algorithm.compute()
131 printNumericTable(predictionResult.get(prediction.prediction),
"Linear Regression prediction results: (first 10 rows):", 10)
132 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):", 10)
134 if __name__ ==
"__main__":