48 from daal.algorithms.linear_regression
import training, prediction
49 from daal.data_management
import (
50 DataSourceIface, FileDataSource, HomogenNumericTable, MergedNumericTable, NumericTableIface
53 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
54 if utils_folder
not in sys.path:
55 sys.path.insert(0, utils_folder)
56 from utils
import printNumericTable
58 DAAL_PREFIX = os.path.join(
'..',
'data')
61 trainDatasetFileName = os.path.join(DAAL_PREFIX,
'online',
'linear_regression_train.csv')
62 testDatasetFileName = os.path.join(DAAL_PREFIX,
'online',
'linear_regression_test.csv')
64 nTrainVectorsInBlock = 250
67 nDependentVariables = 2
70 predictionResult =
None
77 trainDataSource = FileDataSource(
78 trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
79 DataSourceIface.doDictionaryFromContext
83 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
84 trainDependentVariables = HomogenNumericTable(
85 nDependentVariables, 0, NumericTableIface.doNotAllocate
87 mergedData = MergedNumericTable(trainData, trainDependentVariables)
90 algorithm = training.Online()
92 while(trainDataSource.loadDataBlock(nTrainVectorsInBlock, mergedData) == nTrainVectorsInBlock):
94 algorithm.input.set(training.data, trainData)
95 algorithm.input.set(training.dependentVariables, trainDependentVariables)
101 trainingResult = algorithm.finalizeCompute()
103 printNumericTable(trainingResult.get(training.model).getBeta(),
"Linear Regression coefficients:")
107 global trainingResult, predictionResult
110 testDataSource = FileDataSource(
111 testDatasetFileName, DataSourceIface.doAllocateNumericTable,
112 DataSourceIface.doDictionaryFromContext
116 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
117 testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
118 mergedData = MergedNumericTable(testData, testGroundTruth)
121 testDataSource.loadDataBlock(mergedData)
124 algorithm = prediction.Batch()
127 algorithm.input.setTable(prediction.data, testData)
128 algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
131 predictionResult = algorithm.compute()
132 printNumericTable(predictionResult.get(prediction.prediction),
"Linear Regression prediction results: (first 10 rows):", 10)
133 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):", 10)
135 if __name__ ==
"__main__":