34 from daal.algorithms.ridge_regression
import training, prediction
35 from daal.data_management
import DataSource, FileDataSource, NumericTable, HomogenNumericTable, MergedNumericTable
37 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
38 if utils_folder
not in sys.path:
39 sys.path.insert(0, utils_folder)
40 from utils
import printNumericTable
43 trainDatasetFileName = os.path.join(
"..",
"data",
"batch",
"linear_regression_train.csv")
44 testDatasetFileName = os.path.join(
"..",
"data",
"batch",
"linear_regression_test.csv")
46 nTrainVectorsInBlock = 250
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 algorithm = training.Online()
65 while trainDataSource.loadDataBlock(nTrainVectorsInBlock, mergedData) == nTrainVectorsInBlock:
67 algorithm.input.set(training.data, trainData)
68 algorithm.input.set(training.dependentVariables, trainDependentVariables)
75 trainingResult = algorithm.finalizeCompute()
77 printNumericTable(trainingResult.get(training.model).getBeta(),
"Ridge Regression coefficients:")
81 def testModel(trainingResult):
83 testDataSource = FileDataSource(testDatasetFileName,
84 DataSource.doAllocateNumericTable,
85 DataSource.doDictionaryFromContext)
88 testData = HomogenNumericTable(nFeatures, 0, NumericTable.doNotAllocate)
89 testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTable.doNotAllocate)
90 mergedData = MergedNumericTable(testData, testGroundTruth)
93 testDataSource.loadDataBlock(mergedData)
96 algorithm = prediction.Batch()
99 algorithm.input.setTable(prediction.data, testData)
100 algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
103 predictionResult = algorithm.compute()
105 printNumericTable(predictionResult.get(prediction.prediction),
106 "Ridge Regression prediction results: (first 10 rows):", 10)
107 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):", 10)
110 if __name__ ==
"__main__":
111 trainingResult = trainModel()
112 testModel(trainingResult)