コード例 #1
0
 private List<String> toStringList(double[] doubleArray) {
   List<String> stringList = new ArrayList<String>(doubleArray.length);
   for (int i = 0; i < doubleArray.length; i++) {
     stringList.add(String.valueOf(doubleArray[i]));
   }
   return stringList;
 }
コード例 #2
0
 /**
  * @param features Array of names of features
  * @param weights Array of weights of features
  * @return List of FeatureImportance in the model {@link FeatureImportance}
  */
 private List<FeatureImportance> getFeatureWeights(
     SortedMap<Integer, String> features, double[] weights) {
   List<FeatureImportance> featureWeights = new ArrayList<FeatureImportance>();
   int i = 0;
   for (String featureName : features.values()) {
     FeatureImportance featureImportance = new FeatureImportance();
     featureImportance.setLabel(featureName);
     featureImportance.setValue(weights[i]);
     featureWeights.add(featureImportance);
     i++;
   }
   return featureWeights;
 }
コード例 #3
0
  /**
   * This method returns multiclass confusion matrix for a given multiclass metric object
   *
   * @param multiclassMetrics Multiclass metric object
   */
  private MulticlassConfusionMatrix getMulticlassConfusionMatrix(
      MulticlassMetrics multiclassMetrics, MLModel mlModel) {
    MulticlassConfusionMatrix multiclassConfusionMatrix = new MulticlassConfusionMatrix();
    if (multiclassMetrics != null) {
      int size = multiclassMetrics.confusionMatrix().numCols();
      double[] matrixArray = multiclassMetrics.confusionMatrix().toArray();
      double[][] matrix = new double[size][size];
      // set values of matrix into a 2D array
      for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
          matrix[i][j] = matrixArray[(j * size) + i];
        }
      }
      multiclassConfusionMatrix.setMatrix(matrix);

      List<Map<String, Integer>> encodings = mlModel.getEncodings();
      // decode only if encodings are available
      if (encodings != null) {
        // last index is response variable encoding
        Map<String, Integer> encodingMap = encodings.get(encodings.size() - 1);
        List<String> decodedLabels = new ArrayList<String>();
        for (double label : multiclassMetrics.labels()) {
          Integer labelInt = (int) label;
          String decodedLabel = MLUtils.getKeyByValue(encodingMap, labelInt);
          if (decodedLabel != null) {
            decodedLabels.add(decodedLabel);
          } else {
            continue;
          }
        }
        multiclassConfusionMatrix.setLabels(decodedLabels);
      } else {
        List<String> labelList = toStringList(multiclassMetrics.labels());
        multiclassConfusionMatrix.setLabels(labelList);
      }

      multiclassConfusionMatrix.setSize(size);
    }
    return multiclassConfusionMatrix;
  }