getPrediction method

Map<String, double> getPrediction({
  1. required ARFF arff,
  2. required Model model,
  3. required List<ARFFData> data,
})

Implementation

Map<String, double> getPrediction(
    {required ARFF arff,
    required Model model,
    required List<ARFFData> data}) {
  Map<String, double> nomInputValues = {};
  Map<String, List<double>> normalizationValues =
      _normalizationReferenceValues(arffData: arff.data);
  for (ARFFAttributes arffAttributes in arff.attributesList) {
    for (ARFFData arffData in data) {
      if (arffData.name == arffAttributes.name) {
        if (arffAttributes.type == 'nominal') {
          for (String nomValue in arffAttributes.nominalValues!) {
            if (arffData.value == nomValue) {
              nomInputValues[nomValue] = 1.0;
            } else {
              nomInputValues[nomValue] = 0.0;
            }
          }
        } else if (arffAttributes.type == 'numeric') {
          int index = arff.attributesList
              .indexWhere((val) => val.name == arffData.name);
          double min = normalizationValues['minValues']![index];
          double max = normalizationValues['maxValues']![index];
          nomInputValues[arffData.name] =
              (double.parse(arffData.value) - min) / (max - min);
        }
      }
    }
  }
  for (Layer layer in model.layers) {
    if (layer.layerType == LayerType.input) {
      for (Neuron neuron in layer.neurons) {
        if (nomInputValues.containsKey(neuron.name)) {
          neuron.value = nomInputValues[neuron.name];
        } else {
          neuron.value = 0.0;
        }
      }
    } else {
      for (Neuron neuron in layer.neurons) {
        double netVal = 0.0;
        for (Edge edge in neuron.inputEdges!) {
          netVal += edge.weight! * edge.inputNeuron.value!;
        }
        neuron.oldValue = neuron.value;
        neuron.value = activationFunction(netVal);
      }
    }
  }
  Map<String, double> prediction = {};
  for (Layer layer in model.layers) {
    if (layer.layerType == LayerType.output) {
      for (Neuron neuron in layer.neurons) {
        prediction[neuron.name!] = neuron.value!;
      }
    }
  }
  return prediction;
}