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(
83 nDependentVariables, 0, NumericTableIface.doNotAllocate
85 mergedData = MergedNumericTable(trainData, trainDependentVariables)
88 trainDataSource.loadDataBlock(mergedData)
91 algorithm = training.Batch(method=training.qrDense)
94 algorithm.input.set(training.data, trainData)
95 algorithm.input.set(training.dependentVariables, trainDependentVariables)
98 trainingResult = algorithm.compute()
99 printNumericTable(trainingResult.get(training.model).getBeta(),
"Linear Regression coefficients:")
103 global predictionResult
106 testDataSource = FileDataSource(
107 testDatasetFileName, DataSourceIface.doAllocateNumericTable,
108 DataSourceIface.doDictionaryFromContext
112 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
113 testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTableIface.doNotAllocate)
114 mergedData = MergedNumericTable(testData, testGroundTruth)
116 testDataSource.loadDataBlock(mergedData)
119 algorithm = prediction.Batch()
122 algorithm.input.setTable(prediction.data, testData)
123 algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
126 predictionResult = algorithm.compute()
127 printNumericTable(predictionResult.get(prediction.prediction),
"Linear Regression prediction results: (first 10 rows):", 10)
128 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):", 10)
130 if __name__ ==
"__main__":