24 from daal.algorithms
import logistic_regression
25 from daal.algorithms.logistic_regression
import prediction, training
26 from daal.algorithms
import classifier
27 from daal.data_management
import (
28 FileDataSource, DataSourceIface, NumericTableIface, HomogenNumericTable,
29 MergedNumericTable, features
32 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
33 if utils_folder
not in sys.path:
34 sys.path.insert(0, utils_folder)
35 from utils
import printNumericTable, printNumericTables
37 DAAL_PREFIX = os.path.join(
'..',
'data')
40 trainDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'logreg_train.csv')
41 testDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'logreg_test.csv')
48 predictionResult =
None
49 testGroundTruth =
None
55 trainDataSource = FileDataSource(
57 DataSourceIface.notAllocateNumericTable,
58 DataSourceIface.doDictionaryFromContext
62 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
63 trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
64 mergedData = MergedNumericTable(trainData, trainGroundTruth)
67 trainDataSource.loadDataBlock(mergedData)
70 algorithm = training.Batch(nClasses)
73 algorithm.input.set(classifier.training.data, trainData)
74 algorithm.input.set(classifier.training.labels, trainGroundTruth)
75 algorithm.parameter().penaltyL1=0.1;
76 algorithm.parameter().penaltyL2=0.1;
79 trainingResult = algorithm.compute()
80 model = trainingResult.get(classifier.training.model)
81 printNumericTable(model.getBeta(),
"Logistic Regression coefficients:")
84 global testGroundTruth, predictionResult
87 testDataSource = FileDataSource(
89 DataSourceIface.notAllocateNumericTable,
90 DataSourceIface.doDictionaryFromContext
94 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
95 testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
96 mergedData = MergedNumericTable(testData, testGroundTruth)
99 testDataSource.loadDataBlock(mergedData)
102 algorithm = prediction.Batch(nClasses)
105 algorithm.input.setTable(classifier.prediction.data, testData)
106 algorithm.input.setModel(classifier.prediction.model, model)
107 algorithm.parameter().resultsToCompute |= logistic_regression.prediction.computeClassesProbabilities | logistic_regression.prediction.computeClassesLogProbabilities
111 predictionResult = algorithm.compute()
116 printNumericTable(predictionResult.get(classifier.prediction.prediction),
"Logistic regression prediction results (first 10 rows):",10)
117 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):",10)
118 printNumericTable(predictionResult.get(logistic_regression.prediction.probabilities),
"Logistic regression prediction probabilities (first 10 rows):",10)
119 printNumericTable(predictionResult.get(logistic_regression.prediction.logProbabilities),
"Logistic regression prediction log probabilities (first 10 rows):",10)
121 if __name__ ==
"__main__":