47 from daal.algorithms.linear_regression
import training, prediction
48 from daal.data_management
import (
49 DataSourceIface, FileDataSource, HomogenNumericTable, MergedNumericTable, NumericTableIface
52 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
53 if utils_folder
not in sys.path:
54 sys.path.insert(0, utils_folder)
55 from utils
import printNumericTable
57 DAAL_PREFIX = os.path.join(
'..',
'data')
60 trainDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'linear_regression_train.csv')
61 testDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'linear_regression_test.csv')
64 nDependentVariables = 2
67 predictionResult =
None
74 trainDataSource = FileDataSource(
75 trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
76 DataSourceIface.doDictionaryFromContext
80 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
81 trainDependentVariables = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
82 mergedData = MergedNumericTable(trainData, trainDependentVariables)
85 trainDataSource.loadDataBlock(mergedData)
88 algorithm = training.Batch()
91 algorithm.input.set(training.data, trainData)
92 algorithm.input.set(training.dependentVariables, trainDependentVariables)
95 trainingResult = algorithm.compute()
96 printNumericTable(trainingResult.get(training.model).getBeta(),
"Linear Regression coefficients:")
100 global trainingResult, predictionResult
103 testDataSource = FileDataSource(
104 testDatasetFileName, DataSourceIface.doAllocateNumericTable,
105 DataSourceIface.doDictionaryFromContext
109 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
110 testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
111 mergedData = MergedNumericTable(testData, testGroundTruth)
114 testDataSource.loadDataBlock(mergedData)
117 algorithm = prediction.Batch()
120 algorithm.input.setTable(prediction.data, testData)
121 algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
124 predictionResult = algorithm.compute()
125 printNumericTable(predictionResult.get(prediction.prediction),
"Linear Regression prediction results: (first 10 rows):", 10)
126 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):", 10)
128 if __name__ ==
"__main__":