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(
59 nDependentVariables, 0, NumericTableIface.doNotAllocate
61 mergedData = MergedNumericTable(trainData, trainDependentVariables)
64 trainDataSource.loadDataBlock(mergedData)
67 algorithm = training.Batch(method=training.qrDense)
70 algorithm.input.set(training.data, trainData)
71 algorithm.input.set(training.dependentVariables, trainDependentVariables)
74 trainingResult = algorithm.compute()
75 printNumericTable(trainingResult.get(training.model).getBeta(),
"Linear Regression coefficients:")
79 global predictionResult
82 testDataSource = FileDataSource(
83 testDatasetFileName, DataSourceIface.doAllocateNumericTable,
84 DataSourceIface.doDictionaryFromContext
88 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
89 testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
90 mergedData = MergedNumericTable(testData, testGroundTruth)
92 testDataSource.loadDataBlock(mergedData)
95 algorithm = prediction.Batch()
98 algorithm.input.setTable(prediction.data, testData)
99 algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
102 predictionResult = algorithm.compute()
103 printNumericTable(predictionResult.get(prediction.prediction),
"Linear Regression prediction results: (first 10 rows):", 10)
104 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):", 10)
106 if __name__ ==
"__main__":