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(nDependentVariables, 0, NumericTableIface.doNotAllocate)
57 mergedData = MergedNumericTable(trainData, trainDependentVariables)
60 trainDataSource.loadDataBlock(mergedData)
63 algorithm = training.Batch()
66 algorithm.input.set(training.data, trainData)
67 algorithm.input.set(training.dependentVariables, trainDependentVariables)
70 trainingResult = algorithm.compute()
71 printNumericTable(trainingResult.get(training.model).getBeta(),
"Linear Regression coefficients:")
75 global trainingResult, predictionResult
78 testDataSource = FileDataSource(
79 testDatasetFileName, DataSourceIface.doAllocateNumericTable,
80 DataSourceIface.doDictionaryFromContext
84 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
85 testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
86 mergedData = MergedNumericTable(testData, testGroundTruth)
89 testDataSource.loadDataBlock(mergedData)
92 algorithm = prediction.Batch()
95 algorithm.input.setTable(prediction.data, testData)
96 algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
99 predictionResult = algorithm.compute()
100 printNumericTable(predictionResult.get(prediction.prediction),
"Linear Regression prediction results: (first 10 rows):", 10)
101 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):", 10)
103 if __name__ ==
"__main__":