59 from daal.algorithms.ridge_regression
import training, prediction
60 from daal.data_management
import DataSource, FileDataSource, NumericTable, HomogenNumericTable, MergedNumericTable
62 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
63 if utils_folder
not in sys.path:
64 sys.path.insert(0, utils_folder)
65 from utils
import printNumericTable
68 trainDatasetFileName = os.path.join(
"..",
"data",
"batch",
"linear_regression_train.csv")
69 testDatasetFileName = os.path.join(
"..",
"data",
"batch",
"linear_regression_test.csv")
72 nDependentVariables = 2
77 trainDataSource = FileDataSource(trainDatasetFileName,
78 DataSource.notAllocateNumericTable,
79 DataSource.doDictionaryFromContext)
82 trainData = HomogenNumericTable(nFeatures, 0, NumericTable.doNotAllocate)
83 trainDependentVariables = HomogenNumericTable(nDependentVariables, 0, NumericTable.doNotAllocate)
84 mergedData = MergedNumericTable(trainData, trainDependentVariables)
87 trainDataSource.loadDataBlock(mergedData)
90 algorithm = training.Batch()
93 algorithm.input.set(training.data, trainData)
94 algorithm.input.set(training.dependentVariables, trainDependentVariables)
97 trainingResult = algorithm.compute()
99 printNumericTable(trainingResult.get(training.model).getBeta(),
"Ridge Regression coefficients:")
100 return trainingResult
103 def testModel(trainingResult):
105 testDataSource = FileDataSource(testDatasetFileName,
106 DataSource.doAllocateNumericTable,
107 DataSource.doDictionaryFromContext)
110 testData = HomogenNumericTable(nFeatures, 0, NumericTable.doNotAllocate)
111 testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTable.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()
127 printNumericTable(predictionResult.get(prediction.prediction),
128 "Ridge Regression prediction results: (first 10 rows):", 10)
129 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):", 10)
132 if __name__ ==
"__main__":
133 trainingResult = trainModel()
134 testModel(trainingResult)