All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jpmml.evaluator.naive_bayes.RichBayesInput Maven / Gradle / Ivy

/*
 * Copyright (c) 2015 Villu Ruusmann
 *
 * This file is part of JPMML-Evaluator
 *
 * JPMML-Evaluator is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JPMML-Evaluator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with JPMML-Evaluator.  If not, see .
 */
package org.jpmml.evaluator.naive_bayes;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.google.common.collect.ImmutableMap;
import jakarta.xml.bind.annotation.XmlTransient;
import org.dmg.pmml.DataType;
import org.dmg.pmml.naive_bayes.BayesInput;
import org.dmg.pmml.naive_bayes.PairCounts;
import org.dmg.pmml.naive_bayes.TargetValueCounts;
import org.jpmml.evaluator.MapHolder;
import org.jpmml.evaluator.TypeUtil;
import org.jpmml.model.ReflectionUtil;

public class RichBayesInput extends BayesInput implements MapHolder {

	@XmlTransient
	private DataType dataType = null;

	@XmlTransient
	private Map targetValueCountMap = null;


	private RichBayesInput(){
	}

	public RichBayesInput(DataType dataType){
		setDataType(dataType);
	}

	public RichBayesInput(DataType dataType, BayesInput bayesInput){
		setDataType(dataType);

		ReflectionUtil.copyState(bayesInput, this);
	}

	@Override
	public Map getMap(){

		if(this.targetValueCountMap == null){
			this.targetValueCountMap = ImmutableMap.copyOf(parsePairCounts());
		}

		return this.targetValueCountMap;
	}

	@Override
	public DataType getDataType(){
		return this.dataType;
	}

	private void setDataType(DataType dataType){
		this.dataType = Objects.requireNonNull(dataType);
	}

	private Map parsePairCounts(){
		DataType dataType = getDataType();

		Map result = new LinkedHashMap<>();

		List pairCounts = getPairCounts();
		for(PairCounts pairCount : pairCounts){
			Object category = pairCount.requireValue();

			Object value = TypeUtil.parseOrCast(dataType, category);

			TargetValueCounts targetValueCounts = pairCount.requireTargetValueCounts();

			result.put(value, targetValueCounts);
		}

		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy