35 from daal.algorithms.ridge_regression
import training, prediction
36 from daal.data_management
import DataSource, FileDataSource, NumericTable, HomogenNumericTable, MergedNumericTable
38 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
39 if utils_folder
not in sys.path:
40 sys.path.insert(0, utils_folder)
41 from utils
import printNumericTable
44 trainDatasetFileName = os.path.join(
"..",
"data",
"batch",
"linear_regression_train.csv")
45 testDatasetFileName = os.path.join(
"..",
"data",
"batch",
"linear_regression_test.csv")
48 nDependentVariables = 2
53 trainDataSource = FileDataSource(trainDatasetFileName,
54 DataSource.notAllocateNumericTable,
55 DataSource.doDictionaryFromContext)
58 trainData = HomogenNumericTable(nFeatures, 0, NumericTable.doNotAllocate)
59 trainDependentVariables = HomogenNumericTable(nDependentVariables, 0, NumericTable.doNotAllocate)
60 mergedData = MergedNumericTable(trainData, trainDependentVariables)
63 trainDataSource.loadDataBlock(mergedData)
66 algorithm = training.Batch()
69 algorithm.input.set(training.data, trainData)
70 algorithm.input.set(training.dependentVariables, trainDependentVariables)
73 trainingResult = algorithm.compute()
75 printNumericTable(trainingResult.get(training.model).getBeta(),
"Ridge Regression coefficients:")
79 def testModel(trainingResult):
81 testDataSource = FileDataSource(testDatasetFileName,
82 DataSource.doAllocateNumericTable,
83 DataSource.doDictionaryFromContext)
86 testData = HomogenNumericTable(nFeatures, 0, NumericTable.doNotAllocate)
87 testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTable.doNotAllocate)
88 mergedData = MergedNumericTable(testData, testGroundTruth)
91 testDataSource.loadDataBlock(mergedData)
94 algorithm = prediction.Batch()
97 algorithm.input.setTable(prediction.data, testData)
98 algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
101 predictionResult = algorithm.compute()
103 printNumericTable(predictionResult.get(prediction.prediction),
104 "Ridge Regression prediction results: (first 10 rows):", 10)
105 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):", 10)
108 if __name__ ==
"__main__":
109 trainingResult = trainModel()
110 testModel(trainingResult)