Search in sources :

Example 1 with ClusteringModel

use of org.dmg.pmml.ClusteringModelDocument.ClusteringModel in project knime-core by knime.

the class PMMLPortObject method moveGlobalTransformationsToModel.

/**
 * Moves the content of the transformation dictionary to local
 * transformations of the model if a model exists.
 */
public void moveGlobalTransformationsToModel() {
    PMML pmml = m_pmmlDoc.getPMML();
    TransformationDictionary transDict = pmml.getTransformationDictionary();
    if (transDict == null || transDict.getDerivedFieldArray() == null || transDict.getDerivedFieldArray().length == 0) {
        // nothing to be moved
        return;
    }
    DerivedField[] globalDerivedFields = transDict.getDerivedFieldArray();
    LocalTransformations localTrans = null;
    if (pmml.getTreeModelArray().length > 0) {
        TreeModel model = pmml.getTreeModelArray(0);
        localTrans = model.getLocalTransformations();
        if (localTrans == null) {
            localTrans = model.addNewLocalTransformations();
        }
    } else if (pmml.getClusteringModelArray().length > 0) {
        ClusteringModel model = pmml.getClusteringModelArray(0);
        localTrans = model.getLocalTransformations();
        if (localTrans == null) {
            localTrans = model.addNewLocalTransformations();
        }
    } else if (pmml.getNeuralNetworkArray().length > 0) {
        NeuralNetwork model = pmml.getNeuralNetworkArray(0);
        localTrans = model.getLocalTransformations();
        if (localTrans == null) {
            localTrans = model.addNewLocalTransformations();
        }
    } else if (pmml.getSupportVectorMachineModelArray().length > 0) {
        SupportVectorMachineModel model = pmml.getSupportVectorMachineModelArray(0);
        localTrans = model.getLocalTransformations();
        if (localTrans == null) {
            localTrans = model.addNewLocalTransformations();
        }
    } else if (pmml.getRegressionModelArray().length > 0) {
        RegressionModel model = pmml.getRegressionModelArray(0);
        localTrans = model.getLocalTransformations();
        if (localTrans == null) {
            localTrans = model.addNewLocalTransformations();
        }
    } else if (pmml.getGeneralRegressionModelArray().length > 0) {
        GeneralRegressionModel model = pmml.getGeneralRegressionModelArray(0);
        localTrans = model.getLocalTransformations();
        if (localTrans == null) {
            localTrans = model.addNewLocalTransformations();
        }
    } else if (pmml.sizeOfRuleSetModelArray() > 0) {
        RuleSetModel model = pmml.getRuleSetModelArray(0);
        localTrans = model.getLocalTransformations();
        if (localTrans == null) {
            localTrans = model.addNewLocalTransformations();
        }
    }
    if (localTrans != null) {
        DerivedField[] derivedFields = appendDerivedFields(localTrans.getDerivedFieldArray(), globalDerivedFields);
        localTrans.setDerivedFieldArray(derivedFields);
        // remove derived fields from TransformationDictionary
        transDict.setDerivedFieldArray(new DerivedField[0]);
    }
// else do nothing as no model exists yet
}
Also used : TreeModel(org.dmg.pmml.TreeModelDocument.TreeModel) RuleSetModel(org.dmg.pmml.RuleSetModelDocument.RuleSetModel) LocalTransformations(org.dmg.pmml.LocalTransformationsDocument.LocalTransformations) TransformationDictionary(org.dmg.pmml.TransformationDictionaryDocument.TransformationDictionary) GeneralRegressionModel(org.dmg.pmml.GeneralRegressionModelDocument.GeneralRegressionModel) PMML(org.dmg.pmml.PMMLDocument.PMML) NeuralNetwork(org.dmg.pmml.NeuralNetworkDocument.NeuralNetwork) SupportVectorMachineModel(org.dmg.pmml.SupportVectorMachineModelDocument.SupportVectorMachineModel) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField) ClusteringModel(org.dmg.pmml.ClusteringModelDocument.ClusteringModel) GeneralRegressionModel(org.dmg.pmml.GeneralRegressionModelDocument.GeneralRegressionModel) RegressionModel(org.dmg.pmml.RegressionModelDocument.RegressionModel)

Example 2 with ClusteringModel

use of org.dmg.pmml.ClusteringModelDocument.ClusteringModel in project knime-core by knime.

the class PMMLModelWrapper method getSegmentContent.

/**
 * Returns the content of a segment as a model wrapper.
 * @param s The segment
 * @return Returns a wrapper around the model
 */
public static PMMLModelWrapper getSegmentContent(final Segment s) {
    TreeModel treemodel = s.getTreeModel();
    if (treemodel != null) {
        return new PMMLTreeModelWrapper(treemodel);
    }
    RegressionModel regrmodel = s.getRegressionModel();
    if (regrmodel != null) {
        return new PMMLRegressionModelWrapper(regrmodel);
    }
    GeneralRegressionModel genregrmodel = s.getGeneralRegressionModel();
    if (genregrmodel != null) {
        return new PMMLGeneralRegressionModelWrapper(genregrmodel);
    }
    ClusteringModel clustmodel = s.getClusteringModel();
    if (clustmodel != null) {
        return new PMMLClusteringModelWrapper(clustmodel);
    }
    NaiveBayesModel nbmodel = s.getNaiveBayesModel();
    if (nbmodel != null) {
        return new PMMLNaiveBayesModelWrapper(nbmodel);
    }
    NeuralNetwork nn = s.getNeuralNetwork();
    if (nn != null) {
        return new PMMLNeuralNetworkWrapper(nn);
    }
    RuleSetModel rsmodel = s.getRuleSetModel();
    if (rsmodel != null) {
        return new PMMLRuleSetModelWrapper(rsmodel);
    }
    SupportVectorMachineModel svmmodel = s.getSupportVectorMachineModel();
    if (svmmodel != null) {
        return new PMMLSupportVectorMachineModelWrapper(svmmodel);
    }
    return null;
}
Also used : RuleSetModel(org.dmg.pmml.RuleSetModelDocument.RuleSetModel) NaiveBayesModel(org.dmg.pmml.NaiveBayesModelDocument.NaiveBayesModel) NeuralNetwork(org.dmg.pmml.NeuralNetworkDocument.NeuralNetwork) RegressionModel(org.dmg.pmml.RegressionModelDocument.RegressionModel) GeneralRegressionModel(org.dmg.pmml.GeneralRegressionModelDocument.GeneralRegressionModel) TreeModel(org.dmg.pmml.TreeModelDocument.TreeModel) GeneralRegressionModel(org.dmg.pmml.GeneralRegressionModelDocument.GeneralRegressionModel) SupportVectorMachineModel(org.dmg.pmml.SupportVectorMachineModelDocument.SupportVectorMachineModel) ClusteringModel(org.dmg.pmml.ClusteringModelDocument.ClusteringModel)

Example 3 with ClusteringModel

use of org.dmg.pmml.ClusteringModelDocument.ClusteringModel in project knime-core by knime.

the class PMMLClusterTranslator method exportTo.

/**
 * {@inheritDoc}
 */
@Override
public SchemaType exportTo(final PMMLDocument pmmlDoc, final PMMLPortObjectSpec spec) {
    DerivedFieldMapper mapper = new DerivedFieldMapper(pmmlDoc);
    PMML pmml = pmmlDoc.getPMML();
    ClusteringModelDocument.ClusteringModel clusteringModel = pmml.addNewClusteringModel();
    PMMLMiningSchemaTranslator.writeMiningSchema(spec, clusteringModel);
    // ---------------------------------------------------
    // set clustering model attributes
    clusteringModel.setModelName("k-means");
    clusteringModel.setFunctionName(MININGFUNCTION.CLUSTERING);
    clusteringModel.setModelClass(ModelClass.CENTER_BASED);
    clusteringModel.setNumberOfClusters(BigInteger.valueOf(m_nrOfClusters));
    // ---------------------------------------------------
    // set comparison measure
    ComparisonMeasureDocument.ComparisonMeasure pmmlComparisonMeasure = clusteringModel.addNewComparisonMeasure();
    pmmlComparisonMeasure.setKind(Kind.DISTANCE);
    if (ComparisonMeasure.squaredEuclidean.equals(m_measure)) {
        pmmlComparisonMeasure.addNewSquaredEuclidean();
    } else {
        pmmlComparisonMeasure.addNewEuclidean();
    }
    // set clustering fields
    for (String colName : m_usedColumns) {
        ClusteringFieldDocument.ClusteringField pmmlClusteringField = clusteringModel.addNewClusteringField();
        pmmlClusteringField.setField(mapper.getDerivedFieldName(colName));
        pmmlClusteringField.setCompareFunction(COMPAREFUNCTION.ABS_DIFF);
    }
    // ----------------------------------------------------
    // set clusters
    int i = 0;
    for (double[] prototype : m_prototypes) {
        ClusterDocument.Cluster pmmlCluster = clusteringModel.addNewCluster();
        String name = CLUSTER_NAME_PREFIX + i;
        pmmlCluster.setName(name);
        if (m_clusterCoverage != null && m_clusterCoverage.length == m_prototypes.length) {
            pmmlCluster.setSize(BigInteger.valueOf(m_clusterCoverage[i]));
        }
        i++;
        ArrayType pmmlArray = pmmlCluster.addNewArray();
        pmmlArray.setN(BigInteger.valueOf(prototype.length));
        pmmlArray.setType(Type.REAL);
        StringBuffer buff = new StringBuffer();
        for (double d : prototype) {
            buff.append(d + " ");
        }
        XmlCursor xmlCursor = pmmlArray.newCursor();
        xmlCursor.setTextValue(buff.toString());
        xmlCursor.dispose();
    }
    return ClusteringModel.type;
}
Also used : ClusteringModel(org.dmg.pmml.ClusteringModelDocument.ClusteringModel) ClusteringModelDocument(org.dmg.pmml.ClusteringModelDocument) ComparisonMeasureDocument(org.dmg.pmml.ComparisonMeasureDocument) ClusterDocument(org.dmg.pmml.ClusterDocument) XmlCursor(org.apache.xmlbeans.XmlCursor) ArrayType(org.dmg.pmml.ArrayType) DerivedFieldMapper(org.knime.core.node.port.pmml.preproc.DerivedFieldMapper) ClusteringFieldDocument(org.dmg.pmml.ClusteringFieldDocument) PMML(org.dmg.pmml.PMMLDocument.PMML) ClusteringField(org.dmg.pmml.ClusteringFieldDocument.ClusteringField)

Example 4 with ClusteringModel

use of org.dmg.pmml.ClusteringModelDocument.ClusteringModel in project knime-core by knime.

the class PMMLUtils method getFirstMiningSchema.

/**
 * Retrieves the mining schema of the first model of a specific type.
 *
 * @param pmmlDoc the PMML document to extract the mining schema from
 * @param type the type of the model
 * @return the mining schema of the first model of the given type or null if
 *         there is no model of the given type contained in the pmmlDoc
 */
public static MiningSchema getFirstMiningSchema(final PMMLDocument pmmlDoc, final SchemaType type) {
    Map<PMMLModelType, Integer> models = getNumberOfModels(pmmlDoc);
    if (!models.containsKey(PMMLModelType.getType(type))) {
        return null;
    }
    PMML pmml = pmmlDoc.getPMML();
    /*
         * Unfortunately the PMML models have no common base class. Therefore a
         * cast to the specific type is necessary for being able to add the
         * mining schema.
         */
    if (AssociationModel.type.equals(type)) {
        AssociationModel model = pmml.getAssociationModelArray(0);
        return model.getMiningSchema();
    } else if (ClusteringModel.type.equals(type)) {
        ClusteringModel model = pmml.getClusteringModelArray(0);
        return model.getMiningSchema();
    } else if (GeneralRegressionModel.type.equals(type)) {
        GeneralRegressionModel model = pmml.getGeneralRegressionModelArray(0);
        return model.getMiningSchema();
    } else if (MiningModel.type.equals(type)) {
        MiningModel model = pmml.getMiningModelArray(0);
        return model.getMiningSchema();
    } else if (NaiveBayesModel.type.equals(type)) {
        NaiveBayesModel model = pmml.getNaiveBayesModelArray(0);
        return model.getMiningSchema();
    } else if (NeuralNetwork.type.equals(type)) {
        NeuralNetwork model = pmml.getNeuralNetworkArray(0);
        return model.getMiningSchema();
    } else if (RegressionModel.type.equals(type)) {
        RegressionModel model = pmml.getRegressionModelArray(0);
        return model.getMiningSchema();
    } else if (RuleSetModel.type.equals(type)) {
        RuleSetModel model = pmml.getRuleSetModelArray(0);
        return model.getMiningSchema();
    } else if (SequenceModel.type.equals(type)) {
        SequenceModel model = pmml.getSequenceModelArray(0);
        return model.getMiningSchema();
    } else if (SupportVectorMachineModel.type.equals(type)) {
        SupportVectorMachineModel model = pmml.getSupportVectorMachineModelArray(0);
        return model.getMiningSchema();
    } else if (TextModel.type.equals(type)) {
        TextModel model = pmml.getTextModelArray(0);
        return model.getMiningSchema();
    } else if (TimeSeriesModel.type.equals(type)) {
        TimeSeriesModel model = pmml.getTimeSeriesModelArray(0);
        return model.getMiningSchema();
    } else if (TreeModel.type.equals(type)) {
        TreeModel model = pmml.getTreeModelArray(0);
        return model.getMiningSchema();
    } else {
        return null;
    }
}
Also used : RuleSetModel(org.dmg.pmml.RuleSetModelDocument.RuleSetModel) SequenceModel(org.dmg.pmml.SequenceModelDocument.SequenceModel) TextModel(org.dmg.pmml.TextModelDocument.TextModel) NaiveBayesModel(org.dmg.pmml.NaiveBayesModelDocument.NaiveBayesModel) TimeSeriesModel(org.dmg.pmml.TimeSeriesModelDocument.TimeSeriesModel) NeuralNetwork(org.dmg.pmml.NeuralNetworkDocument.NeuralNetwork) RegressionModel(org.dmg.pmml.RegressionModelDocument.RegressionModel) GeneralRegressionModel(org.dmg.pmml.GeneralRegressionModelDocument.GeneralRegressionModel) TreeModel(org.dmg.pmml.TreeModelDocument.TreeModel) MiningModel(org.dmg.pmml.MiningModelDocument.MiningModel) GeneralRegressionModel(org.dmg.pmml.GeneralRegressionModelDocument.GeneralRegressionModel) PMML(org.dmg.pmml.PMMLDocument.PMML) SupportVectorMachineModel(org.dmg.pmml.SupportVectorMachineModelDocument.SupportVectorMachineModel) AssociationModel(org.dmg.pmml.AssociationModelDocument.AssociationModel) ClusteringModel(org.dmg.pmml.ClusteringModelDocument.ClusteringModel)

Example 5 with ClusteringModel

use of org.dmg.pmml.ClusteringModelDocument.ClusteringModel in project knime-core by knime.

the class PMMLPortObject method moveDerivedFields.

/**
 * Moves the content of the transformation dictionary to local
 * transformations.
 * @param type the type of model to move the derived fields to
 * @return the {@link LocalTransformations} element containing the moved
 *      derived fields or an empty local transformation object if nothing
 *      has to be moved
 */
private LocalTransformations moveDerivedFields(final SchemaType type) {
    PMML pmml = m_pmmlDoc.getPMML();
    TransformationDictionary transDict = pmml.getTransformationDictionary();
    LocalTransformations localTrans = LocalTransformations.Factory.newInstance();
    if (transDict == null) {
        // nothing to be moved
        return localTrans;
    }
    localTrans.setDerivedFieldArray(transDict.getDerivedFieldArray());
    localTrans.setExtensionArray(transDict.getExtensionArray());
    /*
         * Unfortunately the PMML models have no common base class. Therefore a
         * cast to the specific type is necessary for being able to add the
         * mining schema.
         */
    boolean known = true;
    if (AssociationModel.type.equals(type)) {
        AssociationModel model = pmml.getAssociationModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (ClusteringModel.type.equals(type)) {
        ClusteringModel model = pmml.getClusteringModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (GeneralRegressionModel.type.equals(type)) {
        GeneralRegressionModel model = pmml.getGeneralRegressionModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (MiningModel.type.equals(type)) {
        MiningModel model = pmml.getMiningModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (NaiveBayesModel.type.equals(type)) {
        NaiveBayesModel model = pmml.getNaiveBayesModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (NeuralNetwork.type.equals(type)) {
        NeuralNetwork model = pmml.getNeuralNetworkArray(0);
        model.setLocalTransformations(localTrans);
    } else if (RegressionModel.type.equals(type)) {
        RegressionModel model = pmml.getRegressionModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (RuleSetModel.type.equals(type)) {
        RuleSetModel model = pmml.getRuleSetModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (SequenceModel.type.equals(type)) {
        SequenceModel model = pmml.getSequenceModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (SupportVectorMachineModel.type.equals(type)) {
        SupportVectorMachineModel model = pmml.getSupportVectorMachineModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (TextModel.type.equals(type)) {
        TextModel model = pmml.getTextModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (TimeSeriesModel.type.equals(type)) {
        TimeSeriesModel model = pmml.getTimeSeriesModelArray(0);
        model.setLocalTransformations(localTrans);
    } else if (TreeModel.type.equals(type)) {
        TreeModel model = pmml.getTreeModelArray(0);
        model.setLocalTransformations(localTrans);
    } else {
        if (type != null) {
            LOGGER.error("Could not move TransformationDictionary to " + "unsupported model of type \"" + type + "\".");
        }
        known = false;
    }
    if (known) {
        // remove derived fields from TransformationDictionary
        transDict.setDerivedFieldArray(new DerivedField[0]);
        transDict.setExtensionArray(new ExtensionDocument.Extension[0]);
    }
    return localTrans;
}
Also used : RuleSetModel(org.dmg.pmml.RuleSetModelDocument.RuleSetModel) SequenceModel(org.dmg.pmml.SequenceModelDocument.SequenceModel) TransformationDictionary(org.dmg.pmml.TransformationDictionaryDocument.TransformationDictionary) TextModel(org.dmg.pmml.TextModelDocument.TextModel) ExtensionDocument(org.dmg.pmml.ExtensionDocument) NaiveBayesModel(org.dmg.pmml.NaiveBayesModelDocument.NaiveBayesModel) TimeSeriesModel(org.dmg.pmml.TimeSeriesModelDocument.TimeSeriesModel) NeuralNetwork(org.dmg.pmml.NeuralNetworkDocument.NeuralNetwork) GeneralRegressionModel(org.dmg.pmml.GeneralRegressionModelDocument.GeneralRegressionModel) RegressionModel(org.dmg.pmml.RegressionModelDocument.RegressionModel) TreeModel(org.dmg.pmml.TreeModelDocument.TreeModel) LocalTransformations(org.dmg.pmml.LocalTransformationsDocument.LocalTransformations) MiningModel(org.dmg.pmml.MiningModelDocument.MiningModel) GeneralRegressionModel(org.dmg.pmml.GeneralRegressionModelDocument.GeneralRegressionModel) PMML(org.dmg.pmml.PMMLDocument.PMML) SupportVectorMachineModel(org.dmg.pmml.SupportVectorMachineModelDocument.SupportVectorMachineModel) AssociationModel(org.dmg.pmml.AssociationModelDocument.AssociationModel) ClusteringModel(org.dmg.pmml.ClusteringModelDocument.ClusteringModel)

Aggregations

ClusteringModel (org.dmg.pmml.ClusteringModelDocument.ClusteringModel)5 GeneralRegressionModel (org.dmg.pmml.GeneralRegressionModelDocument.GeneralRegressionModel)4 NeuralNetwork (org.dmg.pmml.NeuralNetworkDocument.NeuralNetwork)4 PMML (org.dmg.pmml.PMMLDocument.PMML)4 RegressionModel (org.dmg.pmml.RegressionModelDocument.RegressionModel)4 RuleSetModel (org.dmg.pmml.RuleSetModelDocument.RuleSetModel)4 SupportVectorMachineModel (org.dmg.pmml.SupportVectorMachineModelDocument.SupportVectorMachineModel)4 TreeModel (org.dmg.pmml.TreeModelDocument.TreeModel)4 NaiveBayesModel (org.dmg.pmml.NaiveBayesModelDocument.NaiveBayesModel)3 AssociationModel (org.dmg.pmml.AssociationModelDocument.AssociationModel)2 LocalTransformations (org.dmg.pmml.LocalTransformationsDocument.LocalTransformations)2 MiningModel (org.dmg.pmml.MiningModelDocument.MiningModel)2 SequenceModel (org.dmg.pmml.SequenceModelDocument.SequenceModel)2 TextModel (org.dmg.pmml.TextModelDocument.TextModel)2 TimeSeriesModel (org.dmg.pmml.TimeSeriesModelDocument.TimeSeriesModel)2 TransformationDictionary (org.dmg.pmml.TransformationDictionaryDocument.TransformationDictionary)2 XmlCursor (org.apache.xmlbeans.XmlCursor)1 ArrayType (org.dmg.pmml.ArrayType)1 ClusterDocument (org.dmg.pmml.ClusterDocument)1 ClusteringFieldDocument (org.dmg.pmml.ClusteringFieldDocument)1