/* file: svm_two_class_model_builder.cpp */
* Copyright 2014-2019 Intel Corporation.
* This software and the related documents are Intel copyrighted  materials,  and
* your use of  them is  governed by the  express license  under which  they were
* provided to you (License).  Unless the License provides otherwise, you may not
* use, modify, copy, publish, distribute,  disclose or transmit this software or
* the related documents without Intel's prior written permission.
* This software and the related documents  are provided as  is,  with no express
* or implied  warranties,  other  than those  that are  expressly stated  in the
* License.

!  Content:
!    C++ example of two-class support vector machine (SVM) classification

#include "daal.h"
#include "service.h"

using namespace std;
using namespace daal;
using namespace daal::algorithms;

/* Input data set parameters */
string trainedModelsFileName     = "../data/batch/svm_two_class_trained_model.csv";

string testDatasetFileName      = "../data/batch/svm_two_class_test_dense.csv";

const size_t nFeatures          = 20;
const float bias = -0.562F;

/* Parameters for the SVM kernel function */
kernel_function::KernelIfacePtr kernel(new kernel_function::linear::Batch<>());

NumericTablePtr testGroundTruth;

void testModel(svm::ModelPtr&);
svm::ModelPtr buildModelFromTraining();

int main(int argc, char *argv[])
    checkArguments(argc, argv, 2, &trainedModelsFileName, &testDatasetFileName);

    svm::ModelPtr builtModel = buildModelFromTraining();

    return 0;

svm::ModelPtr buildModelFromTraining()

    /* Initialize FileDataSource<CSVFeatureManager> to retrieve trained model .csv file */
    FileDataSource<CSVFeatureManager> modelSource(trainedModelsFileName,

    /* Create Numeric Tables for supportVectors and classification coefficients */
    NumericTablePtr supportVectors(new HomogenNumericTable<>(nFeatures, 0, NumericTable::doNotAllocate));
    NumericTablePtr classificationCoefficients(new HomogenNumericTable<>(1, 0, NumericTable::doNotAllocate));
    NumericTablePtr mergedModel(new MergedNumericTable(supportVectors, classificationCoefficients));

    /* Retrieve the model from input file */

    size_t nSV = supportVectors->getNumberOfRows();

    svm::ModelBuilder<> modelBuilder(nFeatures, nSV);
    /* write numbers in model */
    BlockDescriptor<> blockResult;
    supportVectors->getBlockOfRows(0, nSV, readOnly, blockResult);
    float* first = blockResult.getBlockPtr();
    float* last = first + nSV*nFeatures;



    /* set Classification Coefficients */
    classificationCoefficients->getBlockOfRows(0, nSV, readOnly, blockResult);
    first = blockResult.getBlockPtr();
    last = first + nSV;




    return modelBuilder.getModel();

void testModel(svm::ModelPtr& inputModel)
    /* Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file */
    FileDataSource<CSVFeatureManager> testDataSource(testDatasetFileName,

    /* Create Numeric Tables for testing data and labels */
    NumericTablePtr testData(new HomogenNumericTable<>(nFeatures, 0, NumericTable::doNotAllocate));
    testGroundTruth = NumericTablePtr(new HomogenNumericTable<>(1, 0, NumericTable::doNotAllocate));
    NumericTablePtr mergedData(new MergedNumericTable(testData, testGroundTruth));

    /* Retrieve the data from input file */

    /* Create an algorithm object to predict SVM values */
    svm::prediction::Batch<float> algorithm;

    algorithm.parameter.kernel = kernel;

    /* Pass a testing data set and the trained model to the algorithm */
    algorithm.input.set(classifier::prediction::data, testData);

    /* Set model created externaly */
    algorithm.input.set(classifier::prediction::model, inputModel);

    /* Predict SVM values */

    printNumericTables<int, float>(testGroundTruth,
                                 "Ground truth", "Classification results",
                                 "SVM classification sample program results (first 20 observations):", 20);
Optimization Notice: 
For more complete information about compiler optimizations, see our Optimization Notice.
Select sticky button color: 
Orange (only for download buttons)