32 from daal.algorithms.ridge_regression
import training, prediction
33 from daal.data_management
import DataSource, FileDataSource, NumericTable, HomogenNumericTable, MergedNumericTable
35 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
36 if utils_folder
not in sys.path:
37 sys.path.insert(0, utils_folder)
38 from utils
import printNumericTable
41 trainDatasetFileName = os.path.join(
"..",
"data",
"batch",
"linear_regression_train.csv")
42 testDatasetFileName = os.path.join(
"..",
"data",
"batch",
"linear_regression_test.csv")
44 nTrainVectorsInBlock = 250
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 algorithm = training.Online()
63 while trainDataSource.loadDataBlock(nTrainVectorsInBlock, mergedData) == nTrainVectorsInBlock:
65 algorithm.input.set(training.data, trainData)
66 algorithm.input.set(training.dependentVariables, trainDependentVariables)
73 trainingResult = algorithm.finalizeCompute()
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)