use of org.dmg.pmml.support_vector_machine.SupportVectorMachineModel in project jpmml-r by jpmml.
the class SVMConverter method encodeModel.
@Override
public SupportVectorMachineModel encodeModel(Schema schema) {
RGenericVector svm = getObject();
RDoubleVector type = (RDoubleVector) svm.getValue("type");
RDoubleVector kernel = (RDoubleVector) svm.getValue("kernel");
RDoubleVector degree = (RDoubleVector) svm.getValue("degree");
RDoubleVector gamma = (RDoubleVector) svm.getValue("gamma");
RDoubleVector coef0 = (RDoubleVector) svm.getValue("coef0");
RGenericVector yScale = (RGenericVector) svm.getValue("y.scale");
RIntegerVector nSv = (RIntegerVector) svm.getValue("nSV");
RDoubleVector sv = (RDoubleVector) svm.getValue("SV");
RDoubleVector rho = (RDoubleVector) svm.getValue("rho");
RDoubleVector coefs = (RDoubleVector) svm.getValue("coefs");
Type svmType = Type.values()[ValueUtil.asInt(type.asScalar())];
Kernel svmKernel = Kernel.values()[ValueUtil.asInt(kernel.asScalar())];
SupportVectorMachineModel supportVectorMachineModel;
switch(svmType) {
case C_CLASSIFICATION:
case NU_CLASSIFICATION:
{
supportVectorMachineModel = encodeClassification(sv, nSv, rho, coefs, schema);
}
break;
case ONE_CLASSIFICATION:
{
Transformation outlier = new OutlierTransformation() {
@Override
public Expression createExpression(FieldRef fieldRef) {
return PMMLUtil.createApply("lessOrEqual", fieldRef, PMMLUtil.createConstant(0d));
}
};
supportVectorMachineModel = encodeRegression(sv, rho, coefs, schema).setOutput(ModelUtil.createPredictedOutput(FieldName.create("decisionFunction"), OpType.CONTINUOUS, DataType.DOUBLE, outlier));
if (yScale != null && yScale.size() > 0) {
throw new IllegalArgumentException();
}
}
break;
case EPS_REGRESSION:
case NU_REGRESSION:
{
supportVectorMachineModel = encodeRegression(sv, rho, coefs, schema);
if (yScale != null && yScale.size() > 0) {
RDoubleVector yScaledCenter = (RDoubleVector) yScale.getValue("scaled:center");
RDoubleVector yScaledScale = (RDoubleVector) yScale.getValue("scaled:scale");
supportVectorMachineModel.setTargets(ModelUtil.createRescaleTargets(-1d * yScaledScale.asScalar(), yScaledCenter.asScalar(), (ContinuousLabel) schema.getLabel()));
}
}
break;
default:
throw new IllegalArgumentException();
}
supportVectorMachineModel.setKernel(svmKernel.createKernel(degree.asScalar(), gamma.asScalar(), coef0.asScalar()));
return supportVectorMachineModel;
}
Aggregations