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,
'batch',
'linear_regression_train.csv')
38 testDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'linear_regression_test.csv')
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(nDependentVariables, 0, NumericTableIface.doNotAllocate)
59 mergedData = MergedNumericTable(trainData, trainDependentVariables)
62 trainDataSource.loadDataBlock(mergedData)
65 algorithm = training.Batch()
68 algorithm.input.set(training.data, trainData)
69 algorithm.input.set(training.dependentVariables, trainDependentVariables)
72 trainingResult = algorithm.compute()
73 printNumericTable(trainingResult.get(training.model).getBeta(),
"Linear Regression coefficients:")
77 global trainingResult, predictionResult
80 testDataSource = FileDataSource(
81 testDatasetFileName, DataSourceIface.doAllocateNumericTable,
82 DataSourceIface.doDictionaryFromContext
86 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
87 testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
88 mergedData = MergedNumericTable(testData, testGroundTruth)
91 testDataSource.loadDataBlock(mergedData)
94 algorithm = prediction.Batch()
97 algorithm.input.setTable(prediction.data, testData)
98 algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
101 predictionResult = algorithm.compute()
102 printNumericTable(predictionResult.get(prediction.prediction),
"Linear Regression prediction results: (first 10 rows):", 10)
103 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):", 10)
105 if __name__ ==
"__main__":