Java Code Examples for org.dmg.pmml.MiningSchema#getMiningFields()

The following examples show how to use org.dmg.pmml.MiningSchema#getMiningFields() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AppPMMLUtilsTest.java    From oryx with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildMiningSchema() {
  MiningSchema miningSchema = AppPMMLUtils.buildMiningSchema(buildTestSchema());

  List<MiningField> miningFields = miningSchema.getMiningFields();
  assertEquals(4, miningFields.size());

  String[] fieldNames = { "foo", "bar", "baz", "bing" };
  for (int i = 0; i < fieldNames.length; i++) {
    assertEquals(fieldNames[i], miningFields.get(i).getName().getValue());
  }

  assertEquals(MiningField.UsageType.SUPPLEMENTARY, miningFields.get(0).getUsageType());
  assertEquals(MiningField.UsageType.PREDICTED, miningFields.get(1).getUsageType());
  assertEquals(MiningField.UsageType.SUPPLEMENTARY, miningFields.get(2).getUsageType());
  assertEquals(MiningField.UsageType.ACTIVE, miningFields.get(3).getUsageType());

  assertEquals(OpType.CATEGORICAL, miningFields.get(1).getOpType());
  assertEquals(OpType.CONTINUOUS, miningFields.get(3).getOpType());
}
 
Example 2
Source File: AppPMMLUtils.java    From oryx with Apache License 2.0 5 votes vote down vote up
/**
 * @param miningSchema {@link MiningSchema} from a model
 * @return index of the {@link MiningField.UsageType#PREDICTED} feature
 */
public static Integer findTargetIndex(MiningSchema miningSchema) {
  List<MiningField> miningFields = miningSchema.getMiningFields();
  for (int i = 0; i < miningFields.size(); i++) {
    if (miningFields.get(i).getUsageType() == MiningField.UsageType.PREDICTED) {
      return i;
    }
  }
  return null;
}
 
Example 3
Source File: AbstractAppMLlibIT.java    From oryx with Apache License 2.0 5 votes vote down vote up
protected static void checkMiningSchema(InputSchema schema, MiningSchema miningSchema) {
  assertNotNull(miningSchema);
  List<MiningField> miningFields = miningSchema.getMiningFields();
  List<String> expectedFeatureNames = schema.getFeatureNames();
  assertEquals("Wrong number of features",
               expectedFeatureNames.size(), miningFields.size());
  for (int i = 0; i < expectedFeatureNames.size(); i++) {
    MiningField miningField = miningFields.get(i);
    String expectedFeature = expectedFeatureNames.get(i);
    String featureName = miningField.getName().getValue();
    assertEquals("Wrong feature at position " + i, expectedFeature, featureName);
    if (schema.isNumeric(expectedFeature) || schema.isCategorical(expectedFeature)) {
      assertEquals("Wrong op type for feature + " + featureName,
                   schema.isNumeric(expectedFeature) ? OpType.CONTINUOUS : OpType.CATEGORICAL,
                   miningField.getOpType());
      if (schema.isTarget(expectedFeature)) {
        assertEquals("Wrong usage type for feature " + featureName,
                     MiningField.UsageType.PREDICTED,
                     miningField.getUsageType());
      } else {
        assertEquals("Wrong usage type for feature " + featureName,
                     MiningField.UsageType.ACTIVE,
                     miningField.getUsageType());
        assertRange(miningField.getImportance().doubleValue(), 0.0, 1.0);
      }
    } else {
      assertEquals("Wrong usage type for feature " + featureName,
                   MiningField.UsageType.SUPPLEMENTARY,
                   miningField.getUsageType());
    }

  }
}
 
Example 4
Source File: ModelManager.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
protected List<InputField> createInputFields(MiningField.UsageType usageType){
	M model = getModel();

	MiningSchema miningSchema = model.getMiningSchema();

	List<InputField> inputFields = new ArrayList<>();

	if(miningSchema.hasMiningFields()){
		List<MiningField> miningFields = miningSchema.getMiningFields();

		for(MiningField miningField : miningFields){
			FieldName name = miningField.getName();

			if(!(miningField.getUsageType()).equals(usageType)){
				continue;
			}

			Field<?> field = getDataField(name);
			if(field == null){
				field = new VariableField(name);
			}

			InputField inputField = new InputField(field, miningField);

			inputFields.add(inputField);
		}
	}

	return ImmutableList.copyOf(inputFields);
}
 
Example 5
Source File: TargetCategoryParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private void processModel(Model model){
	MiningSchema miningSchema = model.getMiningSchema();
	if(miningSchema == null){
		throw new MissingElementException(MissingElementException.formatMessage(XPathUtil.formatElement(model.getClass()) + "/" + XPathUtil.formatElement(MiningSchema.class)), model);
	}

	Map<FieldName, DataType> targetDataTypes = new LinkedHashMap<>();

	if(miningSchema.hasMiningFields()){
		List<MiningField> miningFields = miningSchema.getMiningFields();

		for(MiningField miningField : miningFields){
			FieldName name = miningField.getName();
			if(name == null){
				throw new MissingAttributeException(miningField, PMMLAttributes.MININGFIELD_NAME);
			}

			MiningField.UsageType usageType = miningField.getUsageType();
			switch(usageType){
				case PREDICTED:
				case TARGET:
					DataType dataType = resolveTargetDataType(name);

					targetDataTypes.put(name, dataType);
					break;
				default:
					break;
			}
		}
	}

	this.targetDataTypes.push(targetDataTypes);

	this.dataType = getDataType();
}
 
Example 6
Source File: MiningFieldInterner.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public VisitorAction visit(MiningSchema miningSchema){

	if(miningSchema.hasMiningFields()){
		List<MiningField> miningFields = miningSchema.getMiningFields();

		for(ListIterator<MiningField> it = miningFields.listIterator(); it.hasNext(); ){
			it.set(intern(it.next()));
		}
	}

	return super.visit(miningSchema);
}
 
Example 7
Source File: ModelManager.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
protected ModelManager(PMML pmml, M model){
	setPMML(Objects.requireNonNull(pmml));
	setModel(Objects.requireNonNull(model));

	DataDictionary dataDictionary = pmml.getDataDictionary();
	if(dataDictionary == null){
		throw new MissingElementException(pmml, PMMLElements.PMML_DATADICTIONARY);
	} // End if

	if(dataDictionary.hasDataFields()){
		this.dataFields = CacheUtil.getValue(dataDictionary, ModelManager.dataFieldCache);
	}

	TransformationDictionary transformationDictionary = pmml.getTransformationDictionary();
	if(transformationDictionary != null && transformationDictionary.hasDerivedFields()){
		this.derivedFields = CacheUtil.getValue(transformationDictionary, ModelManager.derivedFieldCache);
	} // End if

	if(transformationDictionary != null && transformationDictionary.hasDefineFunctions()){
		this.defineFunctions = CacheUtil.getValue(transformationDictionary, ModelManager.defineFunctionCache);
	}

	MiningFunction miningFunction = model.getMiningFunction();
	if(miningFunction == null){
		throw new MissingAttributeException(MissingAttributeException.formatMessage(XPathUtil.formatElement(model.getClass()) + "@miningFunction"), model);
	}

	MiningSchema miningSchema = model.getMiningSchema();
	if(miningSchema == null){
		throw new MissingElementException(MissingElementException.formatMessage(XPathUtil.formatElement(model.getClass()) + "/" + XPathUtil.formatElement(MiningSchema.class)), model);
	} // End if

	if(miningSchema.hasMiningFields()){
		List<MiningField> miningFields = miningSchema.getMiningFields();

		for(MiningField miningField : miningFields){
			FieldName name = miningField.getName();
			if(name == null){
				throw new MissingAttributeException(miningField, PMMLAttributes.MININGFIELD_NAME);
			}
		}

		this.miningFields = CacheUtil.getValue(miningSchema, ModelManager.miningFieldCache);
	}

	LocalTransformations localTransformations = model.getLocalTransformations();
	if(localTransformations != null && localTransformations.hasDerivedFields()){
		this.localDerivedFields = CacheUtil.getValue(localTransformations, ModelManager.localDerivedFieldCache);
	}

	Targets targets = model.getTargets();
	if(targets != null && targets.hasTargets()){
		this.targets = CacheUtil.getValue(targets, ModelManager.targetCache);
	}

	Output output = model.getOutput();
	if(output != null && output.hasOutputFields()){
		this.outputFields = CacheUtil.getValue(output, ModelManager.outputFieldCache);
		this.resultFeatures = CacheUtil.getValue(output, ModelManager.resultFeaturesCache);
	}
}
 
Example 8
Source File: ObjectMapperTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
@Test
public void jsonClone() throws Exception {
	DataField dataField = new DataField(FieldName.create("x"), OpType.CATEGORICAL, DataType.BOOLEAN);

	DataDictionary dataDictionary = new DataDictionary()
		.addDataFields(dataField);

	MiningField miningField = new MiningField(FieldName.create("x"));

	MiningSchema miningSchema = new MiningSchema()
		.addMiningFields(miningField);

	assertSame(dataField.getName(), miningField.getName());

	SimplePredicate simplePredicate = new SimplePredicate(FieldName.create("x"), SimplePredicate.Operator.IS_NOT_MISSING, null);

	Node node = new ComplexNode(simplePredicate);

	TreeModel treeModel = new TreeModel()
		.setMiningSchema(miningSchema)
		.setNode(node);

	PMML pmml = new PMML()
		.setDataDictionary(dataDictionary)
		.addModels(treeModel);

	DirectByteArrayOutputStream buffer = new DirectByteArrayOutputStream(1024);

	JacksonUtil.writePMML(pmml, buffer);

	PMML jsonPmml;

	try(InputStream is = buffer.getInputStream()){
		jsonPmml = JacksonUtil.readPMML(is);
	}

	DataDictionary jsonDataDictionary = jsonPmml.getDataDictionary();

	List<DataField> jsonDataFields = jsonDataDictionary.getDataFields();

	assertEquals(1, jsonDataFields.size());

	DataField jsonDataField = jsonDataFields.get(0);

	assertEquals(dataField.getName(), jsonDataField.getName());
	assertEquals(dataField.getOpType(), jsonDataField.getOpType());
	assertEquals(dataField.getDataType(), jsonDataField.getDataType());

	List<Model> jsonModels = jsonPmml.getModels();

	assertEquals(1, jsonModels.size());

	TreeModel jsonTreeModel = (TreeModel)jsonModels.get(0);

	MiningSchema jsonMiningSchema = jsonTreeModel.getMiningSchema();

	List<MiningField> jsonMiningFields = jsonMiningSchema.getMiningFields();

	assertEquals(1, jsonMiningFields.size());

	MiningField jsonMiningField = jsonMiningFields.get(0);

	assertEquals(miningField.getName(), jsonMiningField.getName());
	assertEquals(miningField.getUsageType(), jsonMiningField.getUsageType());

	assertSame(jsonDataField.getName(), jsonMiningField.getName());

	Node jsonNode = jsonTreeModel.getNode();

	SimplePredicate jsonSimplePredicate = (SimplePredicate)jsonNode.getPredicate();

	assertEquals(simplePredicate.getField(), jsonSimplePredicate.getField());
	assertEquals(simplePredicate.getOperator(), jsonSimplePredicate.getOperator());

	assertSame(jsonDataField.getName(), jsonSimplePredicate.getField());
	assertSame(jsonMiningField.getName(), jsonSimplePredicate.getField());
}