给定属性索引,WEKA生成的模型似乎无法预测类和分布

用户名

概述

我正在使用WEKA API 3.7.10(开发人员版本)来使用我的预制.model文件。

我制作了25个模型:五个算法的五个结果变量。

  • J48决策树
  • 交替决策树
  • 随机森林
  • LogitBoost
  • 随机子空间

我在J48,随机子空间和随机森林方面遇到问题。

必要的文件

以下是ARFF创建后我的数据表示形式:

@relation WekaData

@attribute ageDiagNum numeric
@attribute raceGroup {Black,Other,Unknown,White}
@attribute stage3 {0,I,IIA,IIB,IIIA,IIIB,IIIC,IIINOS,IV,'UNK Stage'}
@attribute m3 {M0,M1,MX}
@attribute reasonNoCancerSurg {'Not performed, patient died prior to recommended surgery','Not recommended','Not recommended, contraindicated due to other conditions','Recommended but not performed, patient refused','Recommended but not performed, unknown reason','Recommended, unknown if performed','Surgery performed','Unknown; death certificate or autopsy only case'}
@attribute ext2 {00,05,10,11,13,14,15,16,17,18,20,21,23,24,25,26,27,28,30,31,33,34,35,36,37,38,40,50,60,70,80,85,99}
@attribute time2 {}
@attribute time4 {}
@attribute time6 {}
@attribute time8 {}
@attribute time10 {}

@data
65,White,IIA,MX,'Not recommended, contraindicated due to other conditions',14,?,?,?,?,?

我需要得到二进制属性time2,以time10从他们各自的模型。


以下是我用来从所有模型文件中获取预测的代码片段

private static Map<String, Object> predict(Instances instances,
        Classifier classifier, int attributeIndex) {
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    int instanceIndex = 0; // do not change, equal to row 1
    double[] percentage = { 0 };
    double outcomeValue = 0;
    AbstractOutput abstractOutput = null;

    if(classifier.getClass() == RandomForest.class || classifier.getClass() == RandomSubSpace.class) {
        // has problems predicting time2 to time10
        instances.setClassIndex(5); 
    } else {
        // works as intended in LogitBoost and ADTree
        instances.setClassIndex(attributeIndex);    
    }

    try {
        outcomeValue = classifier.classifyInstance(instances.instance(0));
        percentage = classifier.distributionForInstance(instances
                .instance(instanceIndex));
    } catch (Exception e) {
        e.printStackTrace();
    }

    map.put("Class", outcomeValue);

    if (percentage.length > 0) {
        double percentageRaw = 0;
        if (outcomeValue == new Double(1)) {
            percentageRaw = percentage[1];
        } else {
            percentageRaw = 1 - percentage[0];
        }
        map.put("Percentage", percentageRaw);
    } else {
        // because J48 returns an error if percentage[i] because it's empty
        map.put("Percentage", new Double(0));
    }

    return map;
}

这是我用来预测结果的模型,time2因此我们将使用索引6:

instances.setClassIndex(5); 

问题

  • 正如我之前所说的,LogitBoostADTree其他三种方法相比,这种简单的方法没有问题,因为我遵循了在Java代码中使用WEKA教程。

  • [解决]从我tweakings基础,RandomForestRandomSubSpace返回一个ArrayOutOfBoundsException如果说预测time2time10

    java.lang.ArrayIndexOutOfBoundsException: 0
        at weka.classifiers.meta.Bagging.distributionForInstance(Bagging.java:586)
        at weka.classifiers.trees.RandomForest.distributionForInstance(RandomForest.java:602)
        at weka.classifiers.AbstractClassifier.classifyInstance(AbstractClassifier.java:70)
    

    堆栈跟踪将根本错误指向该行:

    outcomeValue = classifier.classifyInstance(instances.instance(0));
    

    解决方案:我的过程中有一些复制粘贴错误ARFF文件创建的二元变量time2time10FastVector<String>()到的值的分配FastVector<Attribute>()对象。我的十款RandomForestRandomSubSpace车型现在都工作正常!

  • [已解决] J48决策树现在有一个新问题。现在,它不再提供任何预测,而是返回一个错误:

    java.lang.ArrayIndexOutOfBoundsException: 11
        at weka.core.DenseInstance.value(DenseInstance.java:332)
        at weka.core.AbstractInstance.isMissing(AbstractInstance.java:315)
        at weka.classifiers.trees.j48.C45Split.whichSubset(C45Split.java:494)
        at weka.classifiers.trees.j48.ClassifierTree.getProbs(ClassifierTree.java:670)
        at weka.classifiers.trees.j48.ClassifierTree.classifyInstance(ClassifierTree.java:231)
        at weka.classifiers.trees.J48.classifyInstance(J48.java:266)
    

    它追溯到线

    outcomeValue = classifier.classifyInstance(instances.instance(0));
    

    解决方案:实际上,我是随机运行该程序的,J48并且可以运行-给结果变量和相关的分布。


我希望有人可以帮助我解决这个问题。我真的不知道这段代码有什么问题,因为我已经在线检查了Javadocs和示例,并且常量预测仍然持续存在。

(我目前正在检查WEKA GUI的主程序,但请在这里帮助我:-))

xtof54

我现在只看过RandomForest问题。这是因为Bagging类从数据实例本身而不是从模型中提取不同类的数量。您在文本中说time2到time10是二进制的,但是您没有在ARFF文件中说出来,因此Bagging类不知道有多少个类。

因此,您只需要在ARFF文件中指定time2是二进制的,例如:@attribute time2 {0,1}

而且您将不再获得任何例外。

我没有研究过J48问题,因为它与ARFF定义可能是相同的问题。

测试代码:

  public static void main(String [] argv) {
      try {
        Classifier cls = (Classifier) weka.core.SerializationHelper.read("bosom.100k.2.j48.MODEL");
        J48 c = (J48)cls;

        DataSource source = new DataSource("data.arff");
        Instances data = source.getDataSet();
        data.setClassIndex(6);        

        try {
            double outcomeValue = c.classifyInstance(data.instance(0));
            System.out.println("outcome "+outcomeValue);
            double[] p = c.distributionForInstance(data.instance(0));
            System.out.println(Arrays.toString(p));
        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Weka预测分布中类值的错误排序

使用自己的Java代码和模型获取WEKA中的预测百分比

使用给定的均值和标准在 python 中生成分布

为什么结果与Keras模型中的调用和预测不同?似乎预测忽略任何随机产生的值

训练和评估预测模型的预测误差

列表中的模型无法预测

ML模型无法正确预测

在具有给定属性的matlab中生成均匀分布的延迟

Django 模型 - 在类中混合模型字段和实例属性

如何获得模型预测的最后索引?

告诉Weka ARFF文件中要预测哪些属性?

Gcloud AI平台,无法使用自己的预测类创建模型

生成给定分布的随机数

使用Keras错误预测和/或模型创建不存在的类的文本分类(单词袋)

Predict() 与 sklearn LogisticRegression 和 RandomForest 模型总是预测少数类 (1)

pytorch模型加载和预测,AttributeError:'dict'对象没有属性'predict'

使用FeatureTools为预测模型生成标签

无法在Stardog中定义类和属性

在Shiny上显示预测模型的类概率

无法将类模型属性转换为在 .net 中列出

使用python从指数分布和模型生成随机数

插入符号和闪亮符号:无法创建由插入符号模型驱动的预测应用

UML域模型:如何区分属性和类?

恢复后无法预测Keras的冻结模型

我的Keras模型无法预测负值

无法获得预测模型的闪亮输出

swagger.json示例预测模型的json似乎不返回预测

以 CSV(或文本)格式导出 Weka 命令行预测,仅预测值和原始值

smallint无法生成模型和表导轨3+ postgreSQL