Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Weka过滤器会导致数据丢失_Java_Machine Learning_Classification_Weka_Multilabel Classification - Fatal编程技术网

Java Weka过滤器会导致数据丢失

Java Weka过滤器会导致数据丢失,java,machine-learning,classification,weka,multilabel-classification,Java,Machine Learning,Classification,Weka,Multilabel Classification,我用weka建立了一个随机森林模型。 我的数据存储在MySQL数据库中。我找不到直接从数据库(至少不是一个简单的数据库)创建weka数据集(“实例”对象)的方法,因此我使用以下代码查询数据库并将数据操作到weka数据集(实例): List<MetadataRecord> metadata = acquireMetadata(); // Loading from DB int datasetSize = metadata.size(); int numFeat

我用weka建立了一个随机森林模型。 我的数据存储在MySQL数据库中。我找不到直接从数据库(至少不是一个简单的数据库)创建weka数据集(“实例”对象)的方法,因此我使用以下代码查询数据库并将数据操作到weka数据集(实例):

    List<MetadataRecord> metadata = acquireMetadata(); // Loading from DB

    int datasetSize = metadata.size();
    int numFeatures = MetadataRecord.FEATURE_NUM;  // Currently set to 14

    ArrayList<Attribute> atts = new ArrayList<Attribute>();
    List<Instance> instances = new ArrayList<Instance>();
    for (int feature = 0; feature < numFeatures; feature++) {
        Attribute current = new Attribute("Attribute" + feature, feature);
        if (feature == 0) {
            for (int obj = 0; obj < datasetSize; obj++) {
                instances.add(new SparseInstance(numFeatures));
            }
        }

        for (int obj = 0; obj < datasetSize; obj++) {
            MetadataRecord record = metadata.get(obj);
            Instance inst = instances.get(obj);
            switch (feature) {
            case 0:
                inst.setValue(current, record.labelId);
                break;
            case 1:
                inst.setValue(current, record.isSecured ? 2 : 1);
                break;
            case 2:
                inst.setValue(current, record.pageCount);
                break;
                // Spared cases 3-13...
            }
        }
        atts.add(current);
    }

    Instances newDataset = new Instances("Metadata", atts, instances.size());

    for (Instance inst : instances) {
        newDataset.add(inst);
    }
    newDataset.setClassIndex(0);
这很好,但令人惊讶的是,在调试数据集时,一些数据丢失了

在应用过滤器之前:

@attribute Attribute0 numeric
@attribute Attribute1 numeric
@attribute Attribute2 numeric
// Spared the other 10 Attributes
@data
{0 1005,1 1,2 19,3 1123,4 7,5 25,6 0.66,7 49,8 2892.21,9 5.32,10 22.63,11 0.4,12 48.95,13 5.29}
@attribute Attribute0 {0,2,3,4,5,6,7,9,11,12,18,22,23,24,25,35,36,39,40,45,51,56,60,67,68,69,78,79,83,84,85,88,94,98,126,127,128,1001,1003,1004,1005,1007,1008,1009,1012,1013,1017,1018,1019,1022}
@attribute Attribute1 {1,2}
@attribute Attribute2 numeric
// Spared the other 10 Attributes
@data
{0 1005,2 19,3 1123,4 7,5 25,6 0.66,7 49,8 2892.21,9 5.32,10 22.63,11 0.4,12 48.95,13 5.29}
应用过滤器后:

@attribute Attribute0 numeric
@attribute Attribute1 numeric
@attribute Attribute2 numeric
// Spared the other 10 Attributes
@data
{0 1005,1 1,2 19,3 1123,4 7,5 25,6 0.66,7 49,8 2892.21,9 5.32,10 22.63,11 0.4,12 48.95,13 5.29}
@attribute Attribute0 {0,2,3,4,5,6,7,9,11,12,18,22,23,24,25,35,36,39,40,45,51,56,60,67,68,69,78,79,83,84,85,88,94,98,126,127,128,1001,1003,1004,1005,1007,1008,1009,1012,1013,1017,1018,1019,1022}
@attribute Attribute1 {1,2}
@attribute Attribute2 numeric
// Spared the other 10 Attributes
@data
{0 1005,2 19,3 1123,4 7,5 25,6 0.66,7 49,8 2892.21,9 5.32,10 22.63,11 0.4,12 48.95,13 5.29}

为什么我丢失了第二个属性的值?

该功能没有丢失,只是没有明确包含在输出中,因为它是稀疏格式的。看看:

稀疏ARFF文件与ARFF文件非常相似,但值为0的数据不能显式表示

稀疏ARFF文件具有相同的头(即@relation和@attribute标记),但数据部分不同。而不是按顺序表示每个值,如下所示:

@data
0, X, 0, Y, "class A"
0, 0, W, 0, "class B"
@data
{1 X, 3 Y, 4 "class A"}
{2 W, 4 "class B"}
非零属性由属性编号及其值明确标识,如下所示:

@data
0, X, 0, Y, "class A"
0, 0, W, 0, "class B"
@data
{1 X, 3 Y, 4 "class A"}
{2 W, 4 "class B"}
每个实例都用大括号括起来,每个条目的格式为:其中index是属性索引(从0开始)

请注意,稀疏实例中省略的值为0,它们不是“缺少”值!如果某个值未知,则必须用问号(?)显式表示该值

尤其是最后一句话很重要。您的
属性1
有两个可能的值,1和2。由于它现在是标称值,因此值1的索引为0。并且省略了索引为0的值


同样:这只是内存中的表示,当您将其打印到文件或屏幕时。您的数据集的实际内容没有更改。

感谢您提供了非常详尽的答案。这种表现,就像weka中的其他东西一样,在我看来很奇怪,但我想我不得不接受它。不客气。您可以只使用“Instance”而不是“SparseInstance”,它应该为您提供正常的输出。然而,“Instance”不是一个接口,因此如果没有具体的实现(如SparseInstance或DenseInstance)就无法实例化?这取决于您的Weka版本。在稳定分支(3.6.x)中,它是一个类。