如何根据Weka分类器中的重要性对功能进行排名?

kha*_*dre 6 nlp machine-learning weka feature-selection text-classification

我使用Weka成功构建了一个分类器.我现在想评估一下我的功能是多么有效或重要.我使用AttributeSelection.但我不知道如何输出具有相应重要性的不同特征.我想简单地按照信息增益分数的降序列出功能!

Cht*_*ect 12

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

任何这些评估类都会为您提供每个属性的分数.例如,如果您使用信息获取进行评分,那么您将使用该课程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);
}
Run Code Online (Sandbox Code Playgroud)

现在您有一张需要按值排序的地图.这是一个通用代码:

 /**
  * 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;
}
Run Code Online (Sandbox Code Playgroud)

最后,

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

现在,您有一个按值排序的条目列表(按升序或降序排列,如您所愿).疯狂吧!

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