Search in sources :

Example 1 with PMMLPCell

use of org.knime.base.node.mine.regression.pmmlgreg.PMMLPCell in project knime-core by knime.

the class LogRegPredictor method determineTargetCategories.

/**
 * Retrieve the target values from the PMML model.
 * @throws InvalidSettingsException if PMML model is inconsistent or ambiguous
 */
private static List<DataCell> determineTargetCategories(final DataColumnSpec targetCol, final PMMLGeneralRegressionContent content) throws InvalidSettingsException {
    Map<String, DataCell> domainValues = new HashMap<String, DataCell>();
    for (DataCell cell : targetCol.getDomain().getValues()) {
        domainValues.put(cell.toString(), cell);
    }
    // Collect target categories from model
    Set<DataCell> modelTargetCategories = new LinkedHashSet<DataCell>();
    for (PMMLPCell cell : content.getParamMatrix()) {
        modelTargetCategories.add(domainValues.get(cell.getTargetCategory()));
    }
    String targetReferenceCategory = content.getTargetReferenceCategory();
    if (targetReferenceCategory == null || targetReferenceCategory.isEmpty()) {
        List<DataCell> targetCategories = new ArrayList<DataCell>();
        targetCategories.addAll(targetCol.getDomain().getValues());
        Collections.sort(targetCategories, targetCol.getType().getComparator());
        if (targetCategories.size() == modelTargetCategories.size() + 1) {
            targetReferenceCategory = targetCategories.get(targetCategories.size() - 1).toString();
            // the last target category is the target reference category
            LOGGER.debug("The target reference category is not explicitly set in PMML. Automatically choose : " + targetReferenceCategory);
        } else {
            throw new InvalidSettingsException("Please set the attribute \"targetReferenceCategory\" of the" + "\"GeneralRegression\" element in the PMML file.");
        }
    }
    modelTargetCategories.add(domainValues.get(targetReferenceCategory));
    List<DataCell> toReturn = new ArrayList<DataCell>();
    toReturn.addAll(modelTargetCategories);
    return toReturn;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PMMLPCell(org.knime.base.node.mine.regression.pmmlgreg.PMMLPCell) HashMap(java.util.HashMap) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ArrayList(java.util.ArrayList) DataCell(org.knime.core.data.DataCell)

Example 2 with PMMLPCell

use of org.knime.base.node.mine.regression.pmmlgreg.PMMLPCell in project knime-core by knime.

the class LogRegPredictor method determineTargetCategories.

/**
 * Retrieve the target values from the PMML model.
 * @throws InvalidSettingsException if PMML model is inconsistent or ambiguous
 */
private static List<DataCell> determineTargetCategories(final DataColumnSpec targetCol, final PMMLGeneralRegressionContent content) throws InvalidSettingsException {
    Map<String, DataCell> domainValues = new HashMap<>();
    for (DataCell cell : targetCol.getDomain().getValues()) {
        domainValues.put(cell.toString(), cell);
    }
    // Collect target categories from model
    Set<DataCell> modelTargetCategories = new LinkedHashSet<>();
    for (PMMLPCell cell : content.getParamMatrix()) {
        modelTargetCategories.add(domainValues.get(cell.getTargetCategory()));
    }
    String targetReferenceCategory = content.getTargetReferenceCategory();
    if (targetReferenceCategory == null || targetReferenceCategory.isEmpty()) {
        List<DataCell> targetCategories = new ArrayList<>();
        targetCategories.addAll(targetCol.getDomain().getValues());
        Collections.sort(targetCategories, targetCol.getType().getComparator());
        if (targetCategories.size() == modelTargetCategories.size() + 1) {
            targetReferenceCategory = targetCategories.get(targetCategories.size() - 1).toString();
            // the last target category is the target reference category
            LOGGER.debug("The target reference category is not explicitly set in PMML. Automatically choose : " + targetReferenceCategory);
        } else {
            throw new InvalidSettingsException("Please set the attribute \"targetReferenceCategory\" of the" + "\"GeneralRegression\" element in the PMML file.");
        }
    }
    modelTargetCategories.add(domainValues.get(targetReferenceCategory));
    List<DataCell> toReturn = new ArrayList<>();
    toReturn.addAll(modelTargetCategories);
    return toReturn;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PMMLPCell(org.knime.base.node.mine.regression.pmmlgreg.PMMLPCell) HashMap(java.util.HashMap) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ArrayList(java.util.ArrayList) DataCell(org.knime.core.data.DataCell)

Example 3 with PMMLPCell

use of org.knime.base.node.mine.regression.pmmlgreg.PMMLPCell in project knime-core by knime.

the class RegressionContent method createGeneralRegressionContent.

/**
 * Creates a new PMML General Regression Content from this linear regression model.
 *
 * @return the PMMLGeneralRegressionContent
 */
public PMMLGeneralRegressionContent createGeneralRegressionContent() {
    List<PMMLPredictor> factors = new ArrayList<PMMLPredictor>();
    for (String factor : m_factorList) {
        PMMLPredictor predictor = new PMMLPredictor(factor);
        factors.add(predictor);
    }
    List<PMMLPredictor> covariates = new ArrayList<PMMLPredictor>();
    for (String covariate : m_covariateList) {
        PMMLPredictor predictor = new PMMLPredictor(covariate);
        covariates.add(predictor);
    }
    // the ParameterList, the PPMatrix and the ParamMatrix
    List<PMMLParameter> parameterList = new ArrayList<PMMLParameter>();
    List<PMMLPPCell> ppMatrix = new ArrayList<PMMLPPCell>();
    List<PMMLPCell> paramMatrix = new ArrayList<PMMLPCell>();
    int p = 0;
    if (m_includeConstant) {
        // Define the intercept
        parameterList.add(new PMMLParameter("p" + p, "Intercept"));
        paramMatrix.add(new PMMLPCell("p" + p, m_beta.getEntry(0, 0), 1));
        p++;
    }
    for (String colName : m_outSpec.getLearningFields()) {
        if (m_factorList.contains(colName)) {
            Iterator<DataCell> designIter = m_factorDomainValues.get(colName).iterator();
            if (!designIter.hasNext()) {
                continue;
            }
            // Omit first
            designIter.next();
            while (designIter.hasNext()) {
                DataCell dvValue = designIter.next();
                String pName = "p" + p;
                parameterList.add(new PMMLParameter(pName, "[" + colName + "=" + dvValue + "]"));
                ppMatrix.add(new PMMLPPCell(dvValue.toString(), colName, pName));
                paramMatrix.add(new PMMLPCell(pName, m_beta.getEntry(0, p), 1));
                p++;
            }
        } else {
            String pName = "p" + p;
            parameterList.add(new PMMLParameter("p" + p, colName));
            ppMatrix.add(new PMMLPPCell("1", colName, pName));
            paramMatrix.add(new PMMLPCell(pName, m_beta.getEntry(0, p), 1));
            p++;
        }
    }
    List<PMMLPCovCell> pCovMatrix = new ArrayList<PMMLPCovCell>();
    PMMLGeneralRegressionContent content = new PMMLGeneralRegressionContent(ModelType.generalLinear, "KNIME Linear Regression", FunctionName.regression, "LinearRegression", parameterList.toArray(new PMMLParameter[0]), factors.toArray(new PMMLPredictor[0]), covariates.toArray(new PMMLPredictor[0]), ppMatrix.toArray(new PMMLPPCell[0]), pCovMatrix.toArray(new PMMLPCovCell[0]), paramMatrix.toArray(new PMMLPCell[0]));
    if (!m_includeConstant) {
        content.setOffsetValue(m_offsetValue);
    }
    return content;
}
Also used : PMMLPCell(org.knime.base.node.mine.regression.pmmlgreg.PMMLPCell) PMMLParameter(org.knime.base.node.mine.regression.pmmlgreg.PMMLParameter) PMMLPCovCell(org.knime.base.node.mine.regression.pmmlgreg.PMMLPCovCell) ArrayList(java.util.ArrayList) PMMLPredictor(org.knime.base.node.mine.regression.pmmlgreg.PMMLPredictor) PMMLGeneralRegressionContent(org.knime.base.node.mine.regression.pmmlgreg.PMMLGeneralRegressionContent) PMMLPPCell(org.knime.base.node.mine.regression.pmmlgreg.PMMLPPCell) DataCell(org.knime.core.data.DataCell)

Example 4 with PMMLPCell

use of org.knime.base.node.mine.regression.pmmlgreg.PMMLPCell in project knime-core by knime.

the class PolyRegContent method createGeneralRegressionContent.

/**
 * Creates a new PMML General Regression Content from this polynomial regression model.
 *
 * @return the PMMLGeneralRegressionContent
 */
@Override
public PMMLGeneralRegressionContent createGeneralRegressionContent() {
    List<PMMLPredictor> factors = new ArrayList<PMMLPredictor>();
    for (String factor : m_factorList) {
        PMMLPredictor predictor = new PMMLPredictor(factor);
        factors.add(predictor);
    }
    List<PMMLPredictor> covariates = new ArrayList<PMMLPredictor>();
    for (String covariate : m_covariateList) {
        PMMLPredictor predictor = new PMMLPredictor(covariate);
        covariates.add(predictor);
    }
    // the ParameterList, the PPMatrix and the ParamMatrix
    List<PMMLParameter> parameterList = new ArrayList<PMMLParameter>();
    List<PMMLPPCell> ppMatrix = new ArrayList<PMMLPPCell>();
    List<PMMLPCell> paramMatrix = new ArrayList<PMMLPCell>();
    int p = 0;
    if (m_includeConstant) {
        // Define the intercept
        parameterList.add(new PMMLParameter("p" + p, "Intercept"));
        paramMatrix.add(new PMMLPCell("p" + p, m_beta.getEntry(0, 0), 1));
        p++;
    }
    for (String colName : m_outSpec.getLearningFields()) {
        if (m_factorList.contains(colName)) {
            Iterator<DataCell> designIter = m_factorDomainValues.get(colName).iterator();
            // Omit first
            designIter.next();
            while (designIter.hasNext()) {
                DataCell dvValue = designIter.next();
                String pName = "p" + p;
                parameterList.add(new PMMLParameter(pName, "[" + colName + "=" + dvValue + "]"));
                ppMatrix.add(new PMMLPPCell(dvValue.toString(), colName, pName));
                paramMatrix.add(new PMMLPCell(pName, m_beta.getEntry(0, p), 1));
                p++;
            }
        } else {
            String pName = "p" + p;
            parameterList.add(new PMMLParameter(pName, colName));
            ppMatrix.add(new PMMLPPCell("1", colName, pName));
            paramMatrix.add(new PMMLPCell(pName, m_beta.getEntry(0, p), 1));
            p++;
        }
    }
    List<PMMLPCovCell> pCovMatrix = new ArrayList<PMMLPCovCell>();
    PMMLGeneralRegressionContent content = new PMMLGeneralRegressionContent(ModelType.generalLinear, "KNIME Polynomial Regression", FunctionName.regression, "LinearRegression", parameterList.toArray(new PMMLParameter[0]), factors.toArray(new PMMLPredictor[0]), covariates.toArray(new PMMLPredictor[0]), ppMatrix.toArray(new PMMLPPCell[0]), pCovMatrix.toArray(new PMMLPCovCell[0]), paramMatrix.toArray(new PMMLPCell[0]));
    if (!m_includeConstant) {
        content.setOffsetValue(m_offsetValue);
    }
    return content;
}
Also used : PMMLPCell(org.knime.base.node.mine.regression.pmmlgreg.PMMLPCell) PMMLParameter(org.knime.base.node.mine.regression.pmmlgreg.PMMLParameter) PMMLPCovCell(org.knime.base.node.mine.regression.pmmlgreg.PMMLPCovCell) ArrayList(java.util.ArrayList) PMMLPredictor(org.knime.base.node.mine.regression.pmmlgreg.PMMLPredictor) PMMLGeneralRegressionContent(org.knime.base.node.mine.regression.pmmlgreg.PMMLGeneralRegressionContent) PMMLPPCell(org.knime.base.node.mine.regression.pmmlgreg.PMMLPPCell) DataCell(org.knime.core.data.DataCell)

Example 5 with PMMLPCell

use of org.knime.base.node.mine.regression.pmmlgreg.PMMLPCell in project knime-core by knime.

the class LogisticRegressionContent method createGeneralRegressionContent.

/**
 * Creates a new PMML General Regression Content from this logistic
 * regression model.
 * @return the PMMLGeneralRegressionContent
 */
public PMMLGeneralRegressionContent createGeneralRegressionContent() {
    List<PMMLPredictor> factors = new ArrayList<PMMLPredictor>();
    for (String factor : m_factorList) {
        PMMLPredictor predictor = new PMMLPredictor(factor);
        factors.add(predictor);
    }
    List<PMMLPredictor> covariates = new ArrayList<PMMLPredictor>();
    for (String covariate : m_covariateList) {
        PMMLPredictor predictor = new PMMLPredictor(covariate);
        covariates.add(predictor);
    }
    // the ParameterList, the PPMatrix and the ParamMatrix
    List<PMMLParameter> parameterList = new ArrayList<PMMLParameter>();
    List<PMMLPPCell> ppMatrix = new ArrayList<PMMLPPCell>();
    List<PMMLPCell> paramMatrix = new ArrayList<PMMLPCell>();
    int pCount = m_beta.getColumnDimension() / (m_targetCategories.size() - 1);
    int p = 0;
    parameterList.add(new PMMLParameter("p" + p, "Intercept"));
    for (int k = 0; k < m_targetCategories.size() - 1; k++) {
        paramMatrix.add(new PMMLPCell("p" + p, m_beta.getEntry(0, p + (k * pCount)), 1, m_targetCategories.get(k).toString()));
    }
    p++;
    final List<String> learningFields = new ArrayList<>(m_outSpec.getLearningFields());
    // learningFields.addAll(m_vectorLengths.keySet());
    for (String colName : learningFields) {
        if (m_factorList.contains(colName)) {
            Iterator<DataCell> designIter = m_factorDomainValues.get(colName).iterator();
            // Omit first
            designIter.next();
            while (designIter.hasNext()) {
                DataCell dvValue = designIter.next();
                String pName = "p" + p;
                parameterList.add(new PMMLParameter(pName, "[" + colName + "=" + dvValue + "]"));
                ppMatrix.add(new PMMLPPCell(dvValue.toString(), colName, pName));
                for (int k = 0; k < m_targetCategories.size() - 1; k++) {
                    paramMatrix.add(new PMMLPCell(pName, m_beta.getEntry(0, p + (k * pCount)), 1, m_targetCategories.get(k).toString()));
                }
                p++;
            }
        } else {
            if (m_vectorLengths.containsKey(colName)) {
                final int length = m_vectorLengths.get(colName);
                final int pFrozen = p;
                for (int idx = 0; idx < length; ++idx) {
                    final String pName = "p" + pFrozen + "_" + idx;
                    final String predictorName = VectorHandling.valueAt(colName, idx);
                    parameterList.add(new PMMLParameter(pName, predictorName));
                    ppMatrix.add(new PMMLPPCell("1", predictorName, pName));
                    for (int k = 0; k < m_targetCategories.size() - 1; k++) {
                        paramMatrix.add(new PMMLPCell(pName, m_beta.getEntry(0, p + (k * pCount)), 1, m_targetCategories.get(k).toString()));
                    }
                    p++;
                }
            } else {
                String pName = "p" + p;
                parameterList.add(new PMMLParameter("p" + p, colName));
                ppMatrix.add(new PMMLPPCell("1", colName, pName));
                for (int k = 0; k < m_targetCategories.size() - 1; k++) {
                    paramMatrix.add(new PMMLPCell(pName, m_beta.getEntry(0, p + (k * pCount)), 1, m_targetCategories.get(k).toString()));
                }
                p++;
            }
        }
    }
    // TODO PCovMatrix
    List<PMMLPCovCell> pCovMatrix = new ArrayList<PMMLPCovCell>();
    PMMLGeneralRegressionContent content = new PMMLGeneralRegressionContent(ModelType.multinomialLogistic, "KNIME Logistic Regression", FunctionName.classification, "LogisticRegression", parameterList.toArray(new PMMLParameter[0]), factors.toArray(new PMMLPredictor[0]), covariates.toArray(new PMMLPredictor[0]), m_vectorLengths, ppMatrix.toArray(new PMMLPPCell[0]), pCovMatrix.toArray(new PMMLPCovCell[0]), paramMatrix.toArray(new PMMLPCell[0]));
    content.setTargetReferenceCategory(m_targetCategories.get(m_targetCategories.size() - 1).toString());
    return content;
}
Also used : PMMLPCell(org.knime.base.node.mine.regression.pmmlgreg.PMMLPCell) PMMLParameter(org.knime.base.node.mine.regression.pmmlgreg.PMMLParameter) PMMLPCovCell(org.knime.base.node.mine.regression.pmmlgreg.PMMLPCovCell) ArrayList(java.util.ArrayList) PMMLPredictor(org.knime.base.node.mine.regression.pmmlgreg.PMMLPredictor) PMMLGeneralRegressionContent(org.knime.base.node.mine.regression.pmmlgreg.PMMLGeneralRegressionContent) PMMLPPCell(org.knime.base.node.mine.regression.pmmlgreg.PMMLPPCell) DataCell(org.knime.core.data.DataCell)

Aggregations

ArrayList (java.util.ArrayList)7 PMMLPCell (org.knime.base.node.mine.regression.pmmlgreg.PMMLPCell)7 DataCell (org.knime.core.data.DataCell)7 PMMLGeneralRegressionContent (org.knime.base.node.mine.regression.pmmlgreg.PMMLGeneralRegressionContent)5 PMMLPCovCell (org.knime.base.node.mine.regression.pmmlgreg.PMMLPCovCell)5 PMMLPPCell (org.knime.base.node.mine.regression.pmmlgreg.PMMLPPCell)5 PMMLParameter (org.knime.base.node.mine.regression.pmmlgreg.PMMLParameter)5 PMMLPredictor (org.knime.base.node.mine.regression.pmmlgreg.PMMLPredictor)5 HashMap (java.util.HashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)2