use of org.knime.base.node.mine.regression.PMMLRegressionTranslator.NumericPredictor in project knime-core by knime.
the class PolyRegLearnerNodeModel method createPMMLModel.
private PMMLPortObject createPMMLModel(final PMMLPortObject inPMMLPort, final DataTableSpec inSpec) throws InvalidSettingsException, SAXException {
NumericPredictor[] preds = new NumericPredictor[m_betas.length - 1];
int deg = m_settings.getDegree();
for (int i = 0; i < m_columnNames.length; i++) {
for (int k = 0; k < deg; k++) {
preds[i * deg + k] = new NumericPredictor(m_columnNames[i], k + 1, m_betas[i * deg + k + 1]);
}
}
RegressionTable tab = new RegressionTable(m_betas[0], preds);
PMMLPortObjectSpec pmmlSpec = null;
if (inPMMLPort != null) {
pmmlSpec = inPMMLPort.getSpec();
}
PMMLPortObjectSpec spec = createModelSpec(pmmlSpec, inSpec);
/* To maintain compatibility with the previous SAX-based implementation.
* */
String targetField = "Response";
List<String> targetFields = spec.getTargetFields();
if (!targetFields.isEmpty()) {
targetField = targetFields.get(0);
}
PMMLPortObject outPMMLPort = new PMMLPortObject(spec, inPMMLPort, inSpec);
PMMLRegressionTranslator trans = new PMMLRegressionTranslator("KNIME Polynomial Regression", "PolynomialRegression", tab, targetField);
outPMMLPort.addModelTranslater(trans);
return outPMMLPort;
}
use of org.knime.base.node.mine.regression.PMMLRegressionTranslator.NumericPredictor in project knime-core by knime.
the class RegressionPredictorNodeModel method createRearranger.
private ColumnRearranger createRearranger(final DataTableSpec inSpec, final PMMLPortObjectSpec regModelSpec, final PMMLRegressionTranslator regModel) throws InvalidSettingsException {
if (regModelSpec == null) {
throw new InvalidSettingsException("No input");
}
// exclude last (response column)
String targetCol = "Response";
for (String s : regModelSpec.getTargetFields()) {
targetCol = s;
break;
}
final List<String> learnFields;
if (regModel != null) {
RegressionTable regTable = regModel.getRegressionTable();
learnFields = new ArrayList<String>();
for (NumericPredictor p : regTable.getVariables()) {
learnFields.add(p.getName());
}
} else {
learnFields = new ArrayList<String>(regModelSpec.getLearningFields());
}
final int[] colIndices = new int[learnFields.size()];
int k = 0;
for (String learnCol : learnFields) {
int index = inSpec.findColumnIndex(learnCol);
if (index < 0) {
throw new InvalidSettingsException("Missing column for " + "regressor variable : \"" + learnCol + "\"");
}
DataColumnSpec regressor = inSpec.getColumnSpec(index);
String name = regressor.getName();
DataColumnSpec col = inSpec.getColumnSpec(index);
if (!col.getType().isCompatible(DoubleValue.class)) {
throw new InvalidSettingsException("Incompatible type of " + "column \"" + name + "\": " + col.getType());
}
colIndices[k++] = index;
}
// try to use some smart naming scheme for the append column
String oldName = targetCol;
if (inSpec.containsName(oldName) && !oldName.toLowerCase().endsWith("(prediction)")) {
oldName = oldName + " (prediction)";
}
String newColName = DataTableSpec.getUniqueColumnName(inSpec, oldName);
DataColumnSpec newCol = new DataColumnSpecCreator(newColName, DoubleCell.TYPE).createSpec();
SingleCellFactory fac = new SingleCellFactory(newCol) {
@Override
public DataCell getCell(final DataRow row) {
RegressionTable t = regModel.getRegressionTable();
int j = 0;
double result = t.getIntercept();
for (NumericPredictor p : t.getVariables()) {
DataCell c = row.getCell(colIndices[j++]);
if (c.isMissing()) {
return DataType.getMissingCell();
}
double v = ((DoubleValue) c).getDoubleValue();
if (p.getExponent() != 1) {
v = Math.pow(v, p.getExponent());
}
result += p.getCoefficient() * v;
}
return new DoubleCell(result);
}
};
ColumnRearranger c = new ColumnRearranger(inSpec);
c.append(fac);
return c;
}
use of org.knime.base.node.mine.regression.PMMLRegressionTranslator.NumericPredictor in project knime-core by knime.
the class PolyRegLearnerNodeModel method createPMMLModel.
private PMMLPortObject createPMMLModel(final PMMLPortObject inPMMLPort, final DataTableSpec inSpec) throws InvalidSettingsException, SAXException {
NumericPredictor[] preds = new NumericPredictor[m_betas.length - 1];
int deg = m_settings.getDegree();
for (int i = 0; i < m_columnNames.length; i++) {
for (int k = 0; k < deg; k++) {
preds[i * deg + k] = new NumericPredictor(m_columnNames[i], k + 1, m_betas[i * deg + k + 1]);
}
}
RegressionTable tab = new RegressionTable(m_betas[0], preds);
PMMLPortObjectSpec pmmlSpec = null;
if (inPMMLPort != null) {
pmmlSpec = inPMMLPort.getSpec();
}
PMMLPortObjectSpec spec = createModelSpec(pmmlSpec, inSpec);
/* To maintain compatibility with the previous SAX-based implementation.
* */
String targetField = "Response";
List<String> targetFields = spec.getTargetFields();
if (!targetFields.isEmpty()) {
targetField = targetFields.get(0);
}
PMMLPortObject outPMMLPort = new PMMLPortObject(spec, inPMMLPort, inSpec);
PMMLRegressionTranslator trans = new PMMLRegressionTranslator("KNIME Polynomial Regression", "PolynomialRegression", tab, targetField);
outPMMLPort.addModelTranslater(trans);
return outPMMLPort;
}
use of org.knime.base.node.mine.regression.PMMLRegressionTranslator.NumericPredictor in project knime-core by knime.
the class LinearRegressionContent method createPortObject.
/**
* Creates a new PMML regression port object from this linear regression
* model.
* @param inPMMLPort the incoming PMMLPort object (can be null)
* @param dts the full data table spec with which the regression
* model was created.
* @param learningSpec a data table spec containing only learning columns
* @return a port object
* @throws InvalidSettingsException if the settings are invalid
*/
public PMMLPortObject createPortObject(final PMMLPortObject inPMMLPort, final DataTableSpec dts, final DataTableSpec learningSpec) throws InvalidSettingsException {
PMMLPortObjectSpec inPMMLSpec = null;
if (inPMMLPort != null) {
inPMMLSpec = inPMMLPort.getSpec();
}
PMMLPortObjectSpec spec = createPortObjectSpec(inPMMLSpec, dts, learningSpec);
PMMLPortObject outPMMLPort = new PMMLPortObject(spec, inPMMLPort);
NumericPredictor[] nps = new NumericPredictor[m_multipliers.length];
for (int i = 0; i < nps.length; i++) {
nps[i] = new NumericPredictor(m_spec.getColumnSpec(i).getName(), 1, m_multipliers[i]);
}
RegressionTable regressionTable = new RegressionTable(m_offset, nps);
/* To maintain compatibility with the previous SAX-based implementation.
* */
String targetField = "Response";
List<String> targetFields = spec.getTargetFields();
if (!targetFields.isEmpty()) {
targetField = targetFields.get(0);
}
PMMLRegressionTranslator trans = new PMMLRegressionTranslator(MODEL_NAME, ALGORITHM_NAME, regressionTable, targetField);
outPMMLPort.addModelTranslater(trans);
return outPMMLPort;
}
Aggregations