Deprecation Notice: With the introduction of daal4py, a package that supersedes PyDAAL, Intel is deprecating PyDAAL and will discontinue support starting with Intel® DAAL 2021 and Intel® Distribution for Python 2021. Until then Intel will continue to provide compatible pyDAAL pip and conda packages for newer releases of Intel DAAL and make it available in open source. However, Intel will not add the new features of Intel DAAL to pyDAAL. Intel recommends developers switch to and use daal4py.
Note: To find daal4py examples, refer to daal4py documentation or browse github repository.
33 from daal
import step1Local, step2Master
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
42 trainDatasetFileNames = [
43 os.path.join(
"..",
"data",
"distributed",
"linear_regression_train_1.csv"),
44 os.path.join(
"..",
"data",
"distributed",
"linear_regression_train_2.csv"),
45 os.path.join(
"..",
"data",
"distributed",
"linear_regression_train_3.csv"),
46 os.path.join(
"..",
"data",
"distributed",
"linear_regression_train_4.csv"),
50 testDatasetFileName = os.path.join(
"..",
"data",
"distributed",
"linear_regression_test.csv")
54 nDependentVariables = 2
59 masterAlgorithm = training.Distributed(step=step2Master)
61 for i
in range(nBlocks):
63 trainDataSource = FileDataSource(trainDatasetFileNames[i],
64 DataSource.notAllocateNumericTable,
65 DataSource.doDictionaryFromContext)
68 trainData = HomogenNumericTable(nFeatures, 0, NumericTable.doNotAllocate)
69 trainDependentVariables = HomogenNumericTable(nDependentVariables, 0, NumericTable.doNotAllocate)
70 mergedData = MergedNumericTable(trainData, trainDependentVariables)
73 trainDataSource.loadDataBlock(mergedData)
76 localAlgorithm = training.Distributed(step=step1Local)
79 localAlgorithm.input.set(training.data, trainData)
80 localAlgorithm.input.set(training.dependentVariables, trainDependentVariables)
83 presult = localAlgorithm.compute()
86 masterAlgorithm.input.add(training.partialModels, presult)
90 masterAlgorithm.compute()
93 trainingResult = masterAlgorithm.finalizeCompute()
95 printNumericTable(trainingResult.get(training.model).getBeta(),
"Ridge Regression coefficients:")
99 def testModel(trainingResult):
101 testDataSource = FileDataSource(testDatasetFileName,
102 DataSource.doAllocateNumericTable,
103 DataSource.doDictionaryFromContext)
106 testData = HomogenNumericTable(nFeatures, 0, NumericTable.doNotAllocate)
107 testGroundTruth = HomogenNumericTable(nDependentVariables, 0, NumericTable.doNotAllocate)
108 mergedData = MergedNumericTable(testData, testGroundTruth)
111 testDataSource.loadDataBlock(mergedData)
114 algorithm = prediction.Batch()
117 algorithm.input.setTable(prediction.data, testData)
118 algorithm.input.setModel(prediction.model, trainingResult.get(training.model))
121 predictionResult = algorithm.compute()
123 printNumericTable(predictionResult.get(prediction.prediction),
"Ridge Regression prediction results: (first 10 rows):", 10)
124 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):", 10)
127 if __name__ ==
"__main__":
128 trainingResult = trainModel()
129 testModel(trainingResult)