Search in sources :

Example 1 with OutlierTransformation

use of org.jpmml.converter.transformations.OutlierTransformation in project jpmml-r by jpmml.

the class SVMConverter method encodeModel.

@Override
public SupportVectorMachineModel encodeModel(Schema schema) {
    RGenericVector svm = getObject();
    RDoubleVector type = svm.getDoubleElement("type");
    RDoubleVector kernel = svm.getDoubleElement("kernel");
    RDoubleVector degree = svm.getDoubleElement("degree");
    RDoubleVector gamma = svm.getDoubleElement("gamma");
    RDoubleVector coef0 = svm.getDoubleElement("coef0");
    RGenericVector yScale = svm.getGenericElement("y.scale");
    RIntegerVector nSv = svm.getIntegerElement("nSV");
    RDoubleVector sv = svm.getDoubleElement("SV");
    RDoubleVector rho = svm.getDoubleElement("rho");
    RDoubleVector coefs = svm.getDoubleElement("coefs");
    Type svmType = Type.values()[ValueUtil.asInt(type.asScalar())];
    Kernel svmKernel = Kernel.values()[ValueUtil.asInt(kernel.asScalar())];
    org.dmg.pmml.support_vector_machine.Kernel pmmlKernel = svmKernel.createKernel(degree.asScalar(), gamma.asScalar(), coef0.asScalar());
    SupportVectorMachineModel supportVectorMachineModel;
    switch(svmType) {
        case C_CLASSIFICATION:
        case NU_CLASSIFICATION:
            {
                supportVectorMachineModel = encodeClassification(pmmlKernel, sv, nSv, rho, coefs, schema);
            }
            break;
        case ONE_CLASSIFICATION:
            {
                Transformation outlier = new OutlierTransformation() {

                    @Override
                    public Expression createExpression(FieldRef fieldRef) {
                        return PMMLUtil.createApply(PMMLFunctions.LESSOREQUAL, fieldRef, PMMLUtil.createConstant(0d));
                    }
                };
                supportVectorMachineModel = encodeRegression(pmmlKernel, sv, rho, coefs, schema).setOutput(ModelUtil.createPredictedOutput("decisionFunction", OpType.CONTINUOUS, DataType.DOUBLE, outlier));
                if (yScale != null && yScale.size() > 0) {
                    throw new IllegalArgumentException();
                }
            }
            break;
        case EPS_REGRESSION:
        case NU_REGRESSION:
            {
                supportVectorMachineModel = encodeRegression(pmmlKernel, sv, rho, coefs, schema);
                if (yScale != null && yScale.size() > 0) {
                    RDoubleVector yScaledCenter = yScale.getDoubleElement("scaled:center");
                    RDoubleVector yScaledScale = yScale.getDoubleElement("scaled:scale");
                    supportVectorMachineModel.setTargets(ModelUtil.createRescaleTargets(-1d * yScaledScale.asScalar(), yScaledCenter.asScalar(), (ContinuousLabel) schema.getLabel()));
                }
            }
            break;
        default:
            throw new IllegalArgumentException();
    }
    return supportVectorMachineModel;
}
Also used : Transformation(org.jpmml.converter.Transformation) OutlierTransformation(org.jpmml.converter.transformations.OutlierTransformation) FieldRef(org.dmg.pmml.FieldRef) OutlierTransformation(org.jpmml.converter.transformations.OutlierTransformation) OpType(org.dmg.pmml.OpType) DataType(org.dmg.pmml.DataType) Expression(org.dmg.pmml.Expression) SupportVectorMachineModel(org.dmg.pmml.support_vector_machine.SupportVectorMachineModel) RadialBasisKernel(org.dmg.pmml.support_vector_machine.RadialBasisKernel) PolynomialKernel(org.dmg.pmml.support_vector_machine.PolynomialKernel) LinearKernel(org.dmg.pmml.support_vector_machine.LinearKernel) SigmoidKernel(org.dmg.pmml.support_vector_machine.SigmoidKernel)

Aggregations

DataType (org.dmg.pmml.DataType)1 Expression (org.dmg.pmml.Expression)1 FieldRef (org.dmg.pmml.FieldRef)1 OpType (org.dmg.pmml.OpType)1 LinearKernel (org.dmg.pmml.support_vector_machine.LinearKernel)1 PolynomialKernel (org.dmg.pmml.support_vector_machine.PolynomialKernel)1 RadialBasisKernel (org.dmg.pmml.support_vector_machine.RadialBasisKernel)1 SigmoidKernel (org.dmg.pmml.support_vector_machine.SigmoidKernel)1 SupportVectorMachineModel (org.dmg.pmml.support_vector_machine.SupportVectorMachineModel)1 Transformation (org.jpmml.converter.Transformation)1 OutlierTransformation (org.jpmml.converter.transformations.OutlierTransformation)1