eu.amidst.core.variables.Variables.getVariableById()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(110)

本文整理了Java中eu.amidst.core.variables.Variables.getVariableById()方法的一些代码示例,展示了Variables.getVariableById()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Variables.getVariableById()方法的具体详情如下:
包路径:eu.amidst.core.variables.Variables
类名称:Variables
方法名:getVariableById

Variables.getVariableById介绍

暂无

代码示例

代码示例来源:origin: amidst/toolbox

/**
 * Returns the class variable of the classifier
 * @return A <code>Variable</code> object
 */
public Variable getClassVariable(){
  return this.svb.getLearntBayesianNetwork().getVariables().getVariableById(this.classIndex);
}

代码示例来源:origin: amidst/toolbox

/**
 * This method returns a DAG object with naive Bayes structure for the attributes of the passed data stream.
 * @param dataStream object of the class DataStream<DataInstance>
 * @param classIndex integer value indicating the position of the class
 * @return object of the class DAG
 */
public static DAG getNaiveBayesStructure(DataStream<DataInstance> dataStream, int classIndex){
  //We create a Variables object from the attributes of the data stream
  Variables modelHeader = new Variables(dataStream.getAttributes());
  //We define the predicitive class variable
  Variable classVar = modelHeader.getVariableById(classIndex);
  //Then, we create a DAG object with the defined model header
  DAG dag = new DAG(modelHeader);
  //We set the linkds of the DAG.
  dag.getParentSets().stream().filter(w -> w.getMainVar() != classVar).forEach(w -> w.addParent(classVar));
  return dag;
}

代码示例来源:origin: amidst/toolbox

/**
 * Sets the distribution of a normal variable in the AMIDST model.
 * @param huginVar the Hugin variable with the corresponding distribution to be converted.
 * @throws ExceptionHugin
 */
private void setNormal(Node huginVar) throws ExceptionHugin {
  int indexNode = this.huginBN.getNodes().indexOf(huginVar);
  Variable amidstVar = this.amidstBN.getVariables().getVariableById(indexNode);
  Normal dist = this.amidstBN.getConditionalDistribution(amidstVar);
  this.setNormal(huginVar, dist, 0);
}

代码示例来源:origin: amidst/toolbox

Variable classVar = modelHeader.getVariableById(0);

代码示例来源:origin: amidst/toolbox

Variable classVar = modelHeader.getVariableById(0);

代码示例来源:origin: amidst/toolbox

sampler.setMARVar(bn.getVariables().getVariableById(0),0.2);
sampler.setMARVar(bn.getVariables().getVariableById(3),0.2);
sampler.setMARVar(bn.getVariables().getVariableById(8),0.2);
DataStream<DataInstance> data =  sampler.sampleToDataStream(sampleSize);

代码示例来源:origin: amidst/toolbox

Variable DiscreteVar0 = bn.getVariables().getVariableById(0);
Variable GaussianVar0 = bn.getVariables().getVariableById(1);
Variable GaussianVar1 = bn.getVariables().getVariableById(2);
Variable ClassVar = bn.getVariables().getVariableById(3);

代码示例来源:origin: amidst/toolbox

/**
 * Sets the distribution of a multinomial variable with no parents in the AMIDST model
 * from the corresponding distribution in the Hugin model.
 * @param huginVar the Hugin variable with the distribution to be converted.
 * @throws ExceptionHugin
 */
private void setMultinomial(Node huginVar) throws ExceptionHugin {
  int indexNode = this.huginBN.getNodes().indexOf(huginVar);
  Variable amidstVar = this.amidstBN.getVariables().getVariableById(indexNode);
  int numStates = amidstVar.getNumberOfStates();
  double[] huginProbabilities = huginVar.getTable().getData();
  double[] amidstProbabilities = new double[numStates];
  for (int k = 0; k < numStates; k++) {
    amidstProbabilities[k] = huginProbabilities[k];
  }
  Multinomial dist = this.amidstBN.getConditionalDistribution(amidstVar);
  dist.setProbabilities(amidstProbabilities);
}

代码示例来源:origin: amidst/toolbox

public static void main(String[] args) throws WrongConfigurationException {
  DataStream<DataInstance> data = DataSetGenerator.generate(0,1000, 0, 10);
  System.out.println(data.getAttributes().toString());
  String className = "GaussianVar0";
  BayesianLinearRegression BLR =
      new BayesianLinearRegression(data.getAttributes())
          .setClassName(className)
          .setWindowSize(100)
          .setDiagonal(false);
  BLR.updateModel(data);
  for (DataOnMemory<DataInstance> batch : data.iterableOverBatches(100)) {
    BLR.updateModel(batch);
  }
  System.out.println(BLR.getModel());
  System.out.println(BLR.getDAG());
  List<DataInstance> dataTest = data.stream().collect(Collectors.toList()).subList(0,5);
  for(DataInstance d : dataTest) {
    Assignment assignment = new HashMapAssignment(BLR.getModel().getNumberOfVars()-1);
    for (int i=0; i<BLR.getModel().getNumberOfVars(); i++) {
      Variable v = BLR.getModel().getVariables().getVariableById(i);
      if(!v.equals(BLR.getClassVar()))
        assignment.setValue(v,d.getValue(v));
    }
    UnivariateDistribution posterior = InferenceEngine.getPosterior(BLR.getClassVar(), BLR.getModel(),assignment);
    System.out.println(posterior.toString());
  }
}

代码示例来源:origin: amidst/toolbox

Variable amidstVar = amidstVariables.getVariableById(i);
Node huginVar = (Node)huginNodes.get(i);

代码示例来源:origin: amidst/toolbox

/**
 * Sets the distribution of a normal variable with multinomial parents in the AMIDST model from the corresponding
 * distribution in the Hugin model.
 * For each assignment of the multinomial parents, a univariate normal is set.
 * @param huginVar the Hugin variable with the distribution to be converted.
 * @throws ExceptionHugin
 */
private void setNormal_MultinomialParents(Node huginVar) throws ExceptionHugin {
  int indexNode = this.huginBN.getNodes().indexOf(huginVar);
  Variable amidstVar = this.amidstBN.getVariables().getVariableById(indexNode);
  List<Variable> conditioningVariables = this.amidstBN.getDAG().getParentSet(amidstVar).getParents();
  int numParentAssignments = MultinomialIndex.getNumberOfPossibleAssignments(conditioningVariables);
  Normal_MultinomialParents dist = this.amidstBN.getConditionalDistribution(amidstVar);
  for (int i = 0; i < numParentAssignments; i++) {
    Normal normal = dist.getNormal(i);
    this.setNormal(huginVar, normal, i);
  }
}

代码示例来源:origin: amidst/toolbox

/**
 * Sets the distribution of a normal variable with normal and multinomial parents in the AMIDST model from the
 * corresponding distribution in the Hugin model.
 * For each assignment of the multinomial parents, a CLG distribution is set.
 * @param huginVar the Hugin variable with the distribution to be converted.
 * @throws ExceptionHugin
 */
private void setNormal_MultinomialNormalParents(Node huginVar) throws ExceptionHugin {
  int indexNode = this.huginBN.getNodes().indexOf(huginVar);
  Variable amidstVar = this.amidstBN.getVariables().getVariableById(indexNode);
  Normal_MultinomialNormalParents dist = this.amidstBN.getConditionalDistribution(amidstVar);
  List<Variable> multinomialParents = dist.getMultinomialParents();
  int numParentAssignments = MultinomialIndex.getNumberOfPossibleAssignments(multinomialParents);
  for(int i=0;i<numParentAssignments;i++) {
    ConditionalLinearGaussian normalNormal = dist.getNormal_NormalParentsDistribution(i);
    double huginIntercept = ((ContinuousChanceNode)huginVar).getAlpha(i);
    normalNormal.setIntercept(huginIntercept);
    List<Variable> normalParents = dist.getNormalParents();
    int numParents = normalParents.size();
    double[] coefficients = new double[numParents];
    for(int j=0;j<numParents;j++){
      String nameAmidstNormalParent = normalParents.get(j).getName();
      ContinuousChanceNode huginParent =  (ContinuousChanceNode)this.huginBN.getNodeByName(nameAmidstNormalParent);
      coefficients[j]= ((ContinuousChanceNode)huginVar).getBeta(huginParent,i);
    }
    normalNormal.setCoeffParents(coefficients);
    double huginVariance = ((ContinuousChanceNode)huginVar).getGamma(i);
    normalNormal.setVariance(huginVariance);
  }
}

代码示例来源:origin: amidst/toolbox

Variable var = bn.getDAG().getVariables().getVariableById(i);
Node n = nodeList.get(i);

代码示例来源:origin: amidst/toolbox

/**
 * Sets the distribution of a multinomial variable with multinomial parents in the AMIDST model
 * from the corresponding distribution in the Hugin model.
 * @param huginVar the Hugin variable with the distribution to be converted.
 * @throws ExceptionHugin
 */
private void setMultinomial_MultinomialParents(Node huginVar) throws ExceptionHugin {
  int indexNode = this.huginBN.getNodes().indexOf(huginVar);
  Variable amidstVar = this.amidstBN.getVariables().getVariableById(indexNode);
  int numStates = amidstVar.getNumberOfStates();
  double[] huginProbabilities = huginVar.getTable().getData();
  List<Variable> parents = this.amidstBN.getDAG().getParentSet(amidstVar).getParents();
  int numParentAssignments = MultinomialIndex.getNumberOfPossibleAssignments(parents);
  for (int i = 0; i < numParentAssignments; i++) {
    double[] amidstProbabilities = new double[numStates];
    for (int k = 0; k < numStates; k++) {
      amidstProbabilities[k] = huginProbabilities[i * numStates + k];
    }
    Multinomial_MultinomialParents dist = this.amidstBN.getConditionalDistribution(amidstVar);
    dist.getMultinomial(i).setProbabilities(amidstProbabilities);
  }
}

代码示例来源:origin: amidst/toolbox

/**
 * Sets the distribution of a normal variable with normal parents in the AMIDST model
 * from the corresponding distribution in the Hugin model.
 * @param huginVar the Hugin variable with the distribution to be converted.
 * @throws ExceptionHugin
 */
private void setNormal_NormalParents(Node huginVar) throws ExceptionHugin {
  int indexNode = this.huginBN.getNodes().indexOf(huginVar);
  Variable amidstVar = this.amidstBN.getVariables().getVariableById(indexNode);
  ConditionalLinearGaussian dist = this.amidstBN.getConditionalDistribution(amidstVar);
  double huginIntercept = ((ContinuousChanceNode)huginVar).getAlpha(0);
  dist.setIntercept(huginIntercept);
  NodeList huginParents = huginVar.getParents();
  int numParents = huginParents.size();
  double[] coefficients = new double[numParents];
  for(int i=0;i<numParents;i++){
    ContinuousChanceNode huginParent = (ContinuousChanceNode)huginParents.get(i);
    coefficients[i]= ((ContinuousChanceNode)huginVar).getBeta(huginParent,0);
  }
  dist.setCoeffParents(coefficients);
  double huginVariance = ((ContinuousChanceNode)huginVar).getGamma(0);
  dist.setVariance(huginVariance);
}

代码示例来源:origin: amidst/toolbox

/**
 * Sets the Hugin nodes from the AMIDST variables.
 * @param amidstBN the Bayesian network model in AMIDST format.
 * @throws ExceptionHugin
 */
private void setNodes(BayesianNetwork amidstBN) throws ExceptionHugin {
  Variables amidstVars = amidstBN.getVariables();
  int size = amidstVars.getNumberOfVars();
  //Hugin always inserts variables at position 0, i.e, for an order A,B,C, it stores C,B,A
  //A reverse order of the variables is needed instead.
  for(int i=1;i<=size;i++){
    Variable amidstVar = amidstVars.getVariableById(size-i);
    if(amidstVar.isMultinomial()){
      LabelledDCNode n = new LabelledDCNode(this.huginBN);
      n.setName(amidstVar.getName());
      n.setNumberOfStates(amidstVar.getNumberOfStates());
      n.setLabel(amidstVar.getName());
      for (int j=0;j<n.getNumberOfStates();j++){
        String stateName = ((FiniteStateSpace)amidstVar.getStateSpaceType()).getStatesName(j);
        n.setStateLabel(j, stateName);
      }
    } else if (amidstVar.isNormal()) {
      ContinuousChanceNode c = new ContinuousChanceNode(this.huginBN);
      c.setName(amidstVar.getName());
    } else {
      throw new IllegalArgumentException("Unrecognized DistributionType:" + amidstVar.getDistributionTypeEnum().toString());
    }
  }
}

代码示例来源:origin: amidst/toolbox

Variable var = bn.getDAG().getVariables().getVariableById(i);
Node n = nodeList.get(i);
if (n.getKind().compareTo(NetworkModel.H_KIND_DISCRETE) == 0) {

相关文章