Intel® oneAPI Data Analytics Library
Learn from community members on how to build compute-intensive applications that run efficiently on Intel® architecture.

How to get model results out of PyDaal into python-usable format (Numpy, Pandas?)

Matt_P_1
Beginner
1,057 Views

Hi,

I've searched the documentation on PyDaal but haven't been able to find a way to access the results of a trained and tested model within a Python script; for example, is there a way to output it as a NumPy array which can then be used further?

Thanks,

Matt

0 Kudos
3 Replies
VictoriyaS_F_Intel
1,057 Views

Hello Matt,

The methods for accessing the components of the machine learning models in Intel DAAL are algorithm specific

The example below shows how to get the access to the support vectors in the Support Vector Machine (SVM) model.

# Build the SVM model
trainingResult = algorithm.compute()

# Retrieve SVM model object from the training result
model = trainingResult.get(classifier.training.model)

# Retrieve numeric table with support vectors from SVM model
svTable = model.getSupportVectors()

# Read the numeric table of support vectors into numpy array
block = BlockDescriptor()
svTable.getBlockOfRows(0, svTable.getNumberOfRows(), readOnly, block)

# Retrieve 2D numpy array with support vectors
svArray = block.getArray()
print(svArray)

svTable.releaseBlockOfRows(block)

I attach the full example source code for your reference.

The complete list of SVM model accessors is available in DAAL API Reference: https://software.intel.com/sites/products/documentation/doclib/daal/daal-user-and-reference-guides/index.htm#daal_python_api/classdaal_1_1algorithms_1_1svm_1_1Model.htm

Best regards,

Victoriya

0 Kudos
Matt_P_1
Beginner
1,057 Views

Hi Victoriya,

Thanks for the response. If I wanted access to the prediction result of a trained model on a new set of data, how would I access those predictions?

For example, in the file you attached, line 153 reads:
    predictionResult = algorithm.getResult()

How do I access the predictionResult's predicted classes with Python? For example, I would like to do some tuning analysis like computing precision, recall, F1, etc. and would prefer to use Scikit-Learn implementations as those are common to the other pipelines in my project; to do so, however, would require accessing the prediction results as a Python array-like structure.

Thanks!

Matt

0 Kudos
Preethi_V_Intel
Employee
1,057 Views

Hi Matt,

You can obtain prediction results as a numeric table using the below command line

predictionValues = predictionResult.get(classifier.prediction.prediction)

predictionValues will have the support vectors 

DAAL's SVM binary classifier only accepts class values 1 and -1. This is because predictionValues not only gives the value of support vectors but also a sign on each value which gives you information on the class. A negative sign on the predictionValues means that the observation belongs to '-1' class and '1' if not.

A numpy array format of predictionValues can be obtained using the following code

from daal.data_management import  BlockDescriptor_Float64
doubleBlock = BlockDescriptor_Float64()
predictionValues.getBlockOfRows(0, predictionValues.getNumberOfRows(), readOnly, doubleBlock)
getArray = doubleBlock.getArray()

There are pre built helper classes and usage examples for PyDaal's SVM in GitHub repository. These classes provide methods to calculate Training Result, Prediction Result, Model Evaluation, Model Storage & Retrieval, relatively simplifying your effort in model building process. These methods will only require you to pass appropriate input parameters to perform each stage. Also you can refer to a gentle introductory series on PyDaal analytics model building process to get more details on the workflow.

 

 

0 Kudos
Reply