22 from daal.algorithms
import logistic_regression
23 from daal.algorithms.logistic_regression
import prediction, training
24 from daal.algorithms
import classifier
25 from daal.data_management
import (
26 FileDataSource, DataSourceIface, NumericTableIface, HomogenNumericTable,
27 MergedNumericTable, features
30 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
31 if utils_folder
not in sys.path:
32 sys.path.insert(0, utils_folder)
33 from utils
import printNumericTable, printNumericTables
35 DAAL_PREFIX = os.path.join(
'..',
'data')
38 trainDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'logreg_train.csv')
39 testDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'logreg_test.csv')
46 predictionResult =
None
47 testGroundTruth =
None
53 trainDataSource = FileDataSource(
55 DataSourceIface.notAllocateNumericTable,
56 DataSourceIface.doDictionaryFromContext
60 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
61 trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
62 mergedData = MergedNumericTable(trainData, trainGroundTruth)
65 trainDataSource.loadDataBlock(mergedData)
68 algorithm = training.Batch(nClasses)
71 algorithm.input.set(classifier.training.data, trainData)
72 algorithm.input.set(classifier.training.labels, trainGroundTruth)
73 algorithm.parameter().penaltyL1=0.1;
74 algorithm.parameter().penaltyL2=0.1;
77 trainingResult = algorithm.compute()
78 model = trainingResult.get(classifier.training.model)
79 printNumericTable(model.getBeta(),
"Logistic Regression coefficients:")
82 global testGroundTruth, predictionResult
85 testDataSource = FileDataSource(
87 DataSourceIface.notAllocateNumericTable,
88 DataSourceIface.doDictionaryFromContext
92 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
93 testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
94 mergedData = MergedNumericTable(testData, testGroundTruth)
97 testDataSource.loadDataBlock(mergedData)
100 algorithm = prediction.Batch(nClasses)
103 algorithm.input.setTable(classifier.prediction.data, testData)
104 algorithm.input.setModel(classifier.prediction.model, model)
105 algorithm.parameter().resultsToCompute |= logistic_regression.prediction.computeClassesProbabilities | logistic_regression.prediction.computeClassesLogProbabilities
109 predictionResult = algorithm.compute()
114 printNumericTable(predictionResult.get(classifier.prediction.prediction),
"Logistic regression prediction results (first 10 rows):",10)
115 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):",10)
116 printNumericTable(predictionResult.get(logistic_regression.prediction.probabilities),
"Logistic regression prediction probabilities (first 10 rows):",10)
117 printNumericTable(predictionResult.get(logistic_regression.prediction.logProbabilities),
"Logistic regression prediction log probabilities (first 10 rows):",10)
119 if __name__ ==
"__main__":