How to create an arff file from an array in java?

I want to get the weighted linear regression coefficients of the pair xy represented by two arrays in java. I reset to weka, but it requests an object of the "Instances" class in the "LinearRegression" class. To create an Instances class file, you need an ARFF file that contains data. I came across solutions that use the FastVector class, but now it is deprecated in the latest version of weka. How to create an ARFF file for a pair of xy and corresponding weights, all represented by arrays in java?

Here is my Base based code. This gives an exception in the last line of “lr.buildClassifier (newDataset)” - Thread [main] (Suspended (UnassignedClassException exception))
  Capabilities.testWithFail (instances): 1302. Here the code is

public static void test() throws Exception
{
    double[][] data = {{4058.0, 4059.0, 4060.0, 214.0, 1710.0, 2452.0, 2473.0, 2474.0, 2475.0, 2476.0, 2477.0, 2478.0, 2688.0, 2905.0, 2906.0, 2907.0, 2908.0, 2909.0, 2950.0, 2969.0, 2970.0, 3202.0, 3342.0, 3900.0, 4007.0, 4052.0, 4058.0, 4059.0, 4060.0}, {19.0, 20.0, 21.0, 31.0, 103.0, 136.0, 141.0, 142.0, 143.0, 144.0, 145.0, 146.0, 212.0, 243.0, 244.0, 245.0, 246.0, 247.0, 261.0, 270.0, 271.0, 294.0, 302.0, 340.0, 343.0, 354.0, 356.0, 357.0, 358.0}};

    int numInstances = data[0].length;

    ArrayList<Attribute> atts = new ArrayList<Attribute>();
    List<Instance> instances = new ArrayList<Instance>();
    for(int dim = 0; dim < 2; dim++)
    {
        Attribute current = new Attribute("Attribute" + dim, dim);

        if(dim == 0)
        {
            for(int obj = 0; obj < numInstances; obj++)
            {
                instances.add(new SparseInstance(numInstances));
            }
        }

        for(int obj = 0; obj < numInstances; obj++)
        {
            instances.get(obj).setValue(current, data[dim][obj]);
            //instances.get(obj).setWeight(weights[obj]);
        }
        atts.add(current);
    }

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

    for(Instance inst : instances)
        newDataset.add(inst);

    LinearRegression lr = new LinearRegression();

    lr.buildClassifier(newDataset);             
}
+1
source share
2 answers

I think this may help you:

FastVector atts = new FastVector();
List<Instance> instances = new ArrayList<Instance>();
for(int dim = 0; dim < numDimensions; dim++)
{
    // Create new attribute / dimension
    Attribute current = new Attribute("Attribute" + dim, dim);
    // Create an instance for each data object
    if(dim == 0)
    {
        for(int obj = 0; obj < numInstances; obj++)
        {
            instances.add(new SparseInstance(numDimensions));
        }
    }

    // Fill the value of dimension "dim" into each object
    for(int obj = 0; obj < numInstances; obj++)
    {
        instances.get(obj).setValue(current, data[dim][obj]);
    }

    // Add attribute to total attributes
    atts.addElement(current);
}

// Create new dataset
Instances newDataset = new Instances("Dataset", atts, instances.size());

// Fill in data objects
for(Instance inst : instances)
    newDataset.add(inst);

Then Instancesis a data set.

Note : the current version (3.6.8) Weka did not complain, although I used FastVector.

However, for the Developer version (3.7.7), use the following command:

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

    for(int obj = 0; obj < numInstances; obj++)
    {
        instances.get(obj).setValue(current, data[dim][obj]);
    }

    atts.add(current);
}

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

for(Instance inst : instances)
    newDataset.add(inst);
+5
source

Instances, toString() ARFF. FastVector , Vector.

0

All Articles