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,
'batch',
'linear_regression_train.csv')
36 testDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'linear_regression_test.csv')
39 nDependentVariables = 2
42 predictionResult =
None
49 trainDataSource = FileDataSource(
50 trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
51 DataSourceIface.doDictionaryFromContext
55 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
56 trainDependentVariables = HomogenNumericTable(
57 nDependentVariables, 0, NumericTableIface.doNotAllocate
59 mergedData = MergedNumericTable(trainData, trainDependentVariables)
62 trainDataSource.loadDataBlock(mergedData)
65 algorithm = training.Batch(method=training.qrDense)
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 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)
90 testDataSource.loadDataBlock(mergedData)
93 algorithm = prediction.Batch()
96 algorithm.input.setTable(prediction.data, testData)
97 algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
100 predictionResult = algorithm.compute()
101 printNumericTable(predictionResult.get(prediction.prediction),
"Linear Regression prediction results: (first 10 rows):", 10)
102 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):", 10)
104 if __name__ ==
"__main__":