6

我使用 Weka 成功构建了一个分类器。我现在想评估我的功能的有效性或重要性。为此,我使用 AttributeSelection。但我不知道如何输出具有相应重要性的不同特征。我只想按信息增益分数的降序列出这些特征!

4

1 回答 1

12

在 Weka 中有很多方法可以对特征进行评分,这些方法称为属性。这些方法可用作weka.attributeSelection.ASEvaluation的子类。

这些评估课程中的任何一个都会为您提供每个属性的分数。例如,如果您使用信息增益进行评分,您将在 class 中使用它InfoGainAttributeEval。有用的方法是

  • InfoGainAttributeEval.html#buildEvaluator(), 和
  • InfoGainAttributeEval.html#evaluateAttribute()

其他类型的特征评分(增益比、相关性等)具有相同的评分方法。使用其中任何一个,您都可以对所有功能进行排名。

排名本身独立于 Weka。在许多方法中,这是一种:

Map<Attribute, Double> infogainscores = new HashMap<Attribute, Double>();
for (int i = 0; i < instances.numAttributes(); i++) {
    Attribute t_attr = instaces.attribute(i);
    double infogain  = evaluation.evaluateAttribute(i);
    infogainscores.put(t_attr, infogain);
}

现在您有一个需要按值排序的地图。这是执行此操作的通用代码:

 /**
  * Provides a {@code SortedSet} of {@code Map.Entry} objects. The sorting is in ascending order if {@param order} > 0
  * and descending order if {@param order} <= 0.
  * @param map   The map to be sorted.
  * @param order The sorting order (positive means ascending, non-positive means descending).
  * @param <K>   Keys.
  * @param <V>   Values need to be {@code Comparable}.
  * @return      A sorted set of {@code Map.Entry} objects.
  */
 static <K,V extends Comparable<? super V>> SortedSet<Map.Entry<K,V>>
 entriesSortedByValues(Map<K,V> map, final int order) {
     SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<>(
         new Comparator<Map.Entry<K,V>>() {
             public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {
                 return (order > 0) ? compareToRetainDuplicates(e1.getValue(), e2.getValue()) : compareToRetainDuplicates(e2.getValue(), e1.getValue());
         }
     }
    );
    sortedEntries.addAll(map.entrySet());
    return sortedEntries;
}

最后,

private static <V extends Comparable<? super V>> int compareToRetainDuplicates(V v1, V v2) {
    return (v1.compareTo(v2) == -1) ? -1 : 1;
}

现在您有一个按值排序的条目列表(按您的意愿按升序或降序排列)。为它疯狂!

请注意,您应该处理多个属性具有相同信息增益的情况。这就是为什么我在保留重复项的同时经历了按值排序的过程。

于 2014-01-21T21:25:27.533 回答