48 from daal.algorithms.linear_regression
import training, prediction
49 from daal.data_management
import (
50 DataSourceIface, FileDataSource, HomogenNumericTable, MergedNumericTable, NumericTableIface
53 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
54 if utils_folder
not in sys.path:
55 sys.path.insert(0, utils_folder)
56 from utils
import printNumericTable
58 DAAL_PREFIX = os.path.join(
'..',
'data')
61 trainDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'linear_regression_train.csv')
62 testDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'linear_regression_test.csv')
65 nDependentVariables = 2
68 predictionResult =
None 75 trainDataSource = FileDataSource(
76 trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
77 DataSourceIface.doDictionaryFromContext
81 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
82 trainDependentVariables = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
83 mergedData = MergedNumericTable(trainData, trainDependentVariables)
86 trainDataSource.loadDataBlock(mergedData)
89 algorithm = training.Batch()
92 algorithm.input.set(training.data, trainData)
93 algorithm.input.set(training.dependentVariables, trainDependentVariables)
96 trainingResult = algorithm.compute()
97 printNumericTable(trainingResult.get(training.model).getBeta(),
"Linear Regression coefficients:")
101 global trainingResult, predictionResult
104 testDataSource = FileDataSource(
105 testDatasetFileName, DataSourceIface.doAllocateNumericTable,
106 DataSourceIface.doDictionaryFromContext
110 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
111 testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
112 mergedData = MergedNumericTable(testData, testGroundTruth)
115 testDataSource.loadDataBlock(mergedData)
118 algorithm = prediction.Batch()
121 algorithm.input.setTable(prediction.data, testData)
122 algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
125 predictionResult = algorithm.compute()
126 printNumericTable(predictionResult.get(prediction.prediction),
"Linear Regression prediction results: (first 10 rows):", 10)
127 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):", 10)
129 if __name__ ==
"__main__":