You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中使用Weka神经网络报错UnassignedDatasetException求助

解决Weka神经网络的UnassignedDatasetException问题

这个UnassignedDatasetException我之前也碰到过,本质就是Weka的DenseInstance实例没有和数据集结构(Instances对象)绑定——Weka需要知道实例的属性定义、类别信息这些元数据,才能完成训练或预测操作。结合你的代码场景(输入4个元素预测第5个值),我给你梳理下具体的修复方案:

核心问题分析

当你创建DenseInstance后,如果没有通过add()将其加入到Instances集合,或者没有调用setDataset()绑定数据集结构,Weka就会抛出这个异常。因为单独的DenseInstance只是一堆数值,Weka不知道这些数值对应哪些属性、哪个是类别列。

具体修复步骤与代码示例

1. 完善属性定义与训练集初始化

首先要把4个输入属性+1个预测目标属性全部定义好,然后创建Instances对象作为数据集模板,并设置好类别索引:

public void readAndTrain(Object[][] data) throws IOException, Exception {
    // 1. 定义所有属性:4个输入属性 + 1个预测目标属性
    Attribute oldValue = new Attribute("oldValue");
    Attribute temp = new Attribute("temp"); // 替换成你的第二个属性名
    Attribute attr3 = new Attribute("attr3"); // 第三个属性
    Attribute attr4 = new Attribute("attr4"); // 第四个属性
    Attribute predictedVal = new Attribute("predictedVal"); // 预测的目标属性

    // 2. 将属性加入属性列表
    fvWekaAttributes = new FastVector();
    fvWekaAttributes.addElement(oldValue);
    fvWekaAttributes.addElement(temp);
    fvWekaAttributes.addElement(attr3);
    fvWekaAttributes.addElement(attr4);
    fvWekaAttributes.addElement(predictedVal);

    // 3. 创建训练集实例,指定关系名、属性列表、初始容量
    isTrainingSet = new Instances("TrainingDataset", fvWekaAttributes, data.length);
    // 设置类别索引为最后一个属性(目标值),索引从0开始
    isTrainingSet.setClassIndex(isTrainingSet.numAttributes() - 1);

2. 将训练数据转换为Weka实例并绑定数据集

遍历传入的Object[][]数据,把每一行转换成DenseInstance,并添加到训练集(添加后会自动绑定数据集结构):

// 4. 遍历数据,转换为Weka实例并加入训练集
    for (Object[] row : data) {
        // 创建实例,属性数量和属性列表一致
        DenseInstance instance = new DenseInstance(fvWekaAttributes.size());
        // 逐个设置属性值(确保类型匹配,这里假设都是数值型)
        instance.setValue(oldValue, (double) row[0]);
        instance.setValue(temp, (double) row[1]);
        instance.setValue(attr3, (double) row[2]);
        instance.setValue(attr4, (double) row[3]);
        instance.setValue(predictedVal, (double) row[4]);
        // 关键:将实例加入训练集,自动绑定数据集结构
        isTrainingSet.add(instance);
    }

3. 初始化并训练神经网络

完成训练集准备后,初始化MultilayerPerceptron并执行训练:

// 5. 初始化MLP并设置参数
    mlp = new MultilayerPerceptron();
    mlp.setHiddenLayers("4"); // 示例:1个隐藏层,4个节点
    mlp.setLearningRate(0.1);
    mlp.setTrainingTime(1000); // 训练迭代次数
    mlp.setValidationSetSize(10); // 可选:设置验证集比例

    // 6. 训练模型
    mlp.buildClassifier(isTrainingSet);
}

4. 预测方法的正确实现

预测时,新创建的实例必须和训练集的属性结构完全一致,并且绑定数据集:

public double predict(Object[] inputData) throws Exception {
    // 创建和训练集结构一致的临时数据集
    Instances tempDataset = new Instances(isTrainingSet);
    // 创建预测用的实例
    DenseInstance predictInstance = new DenseInstance(tempDataset.numAttributes());
    // 设置4个输入属性值
    predictInstance.setValue(tempDataset.attribute(0), (double) inputData[0]);
    predictInstance.setValue(tempDataset.attribute(1), (double) inputData[1]);
    predictInstance.setValue(tempDataset.attribute(2), (double) inputData[2]);
    predictInstance.setValue(tempDataset.attribute(3), (double) inputData[3]);
    // 关键:绑定数据集结构到实例
    predictInstance.setDataset(tempDataset);
    // 执行预测
    return mlp.classifyInstance(predictInstance);
}

关键注意点

  • 所有DenseInstance必须关联到Instances对象:要么通过add()加入集合,要么调用setDataset()手动绑定。
  • 预测用的实例结构必须和训练集完全匹配:属性数量、顺序、类型都不能变。
  • 必须设置setClassIndex():告诉Weka哪个属性是需要预测的目标列,否则模型无法正确训练。

内容的提问来源于stack exchange,提问作者user9355327

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:11:32