33 from daal.algorithms.ridge_regression
import training, prediction
34 from daal.data_management
import DataSource, FileDataSource, NumericTable, HomogenNumericTable, MergedNumericTable
36 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
37 if utils_folder
not in sys.path:
38 sys.path.insert(0, utils_folder)
39 from utils
import printNumericTable
42 trainDatasetFileName = os.path.join(
"..",
"data",
"batch",
"linear_regression_train.csv")
43 testDatasetFileName = os.path.join(
"..",
"data",
"batch",
"linear_regression_test.csv")
46 nDependentVariables = 2
51 trainDataSource = FileDataSource(trainDatasetFileName,
52 DataSource.notAllocateNumericTable,
53 DataSource.doDictionaryFromContext)
56 trainData = HomogenNumericTable(nFeatures, 0, NumericTable.doNotAllocate)
57 trainDependentVariables = HomogenNumericTable(nDependentVariables, 0, NumericTable.doNotAllocate)
58 mergedData = MergedNumericTable(trainData, trainDependentVariables)
61 trainDataSource.loadDataBlock(mergedData)
64 algorithm = training.Batch()
67 algorithm.input.set(training.data, trainData)
68 algorithm.input.set(training.dependentVariables, trainDependentVariables)
71 trainingResult = algorithm.compute()
73 printNumericTable(trainingResult.get(training.model).getBeta(),
"Ridge Regression coefficients:")
77 def testModel(trainingResult):
79 testDataSource = FileDataSource(testDatasetFileName,
80 DataSource.doAllocateNumericTable,
81 DataSource.doDictionaryFromContext)
84 testData = HomogenNumericTable(nFeatures, 0, NumericTable.doNotAllocate)
85 testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTable.doNotAllocate)
86 mergedData = MergedNumericTable(testData, testGroundTruth)
89 testDataSource.loadDataBlock(mergedData)
92 algorithm = prediction.Batch()
95 algorithm.input.setTable(prediction.data, testData)
96 algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
99 predictionResult = algorithm.compute()
101 printNumericTable(predictionResult.get(prediction.prediction),
102 "Ridge Regression prediction results: (first 10 rows):", 10)
103 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):", 10)
106 if __name__ ==
"__main__":
107 trainingResult = trainModel()
108 testModel(trainingResult)