【问题标题】:How to add a vector as values in Java如何在 Java 中添加向量作为值
【发布时间】:2015-07-14 13:18:23
【问题描述】:

在第 28 行,我想用 ch 数组替换 values。如果您运行该程序,直方图的值将出现在文件的开头。 .arff 文件打印正确,但 values@data 字段中。显然,ch 是一个不兼容的类型。 Java中这种结构的修饰符是什么?

public static void main(String[] args) throws Exception 
{
    int i;
    int[][][] ch = new int[4][4][4];
    FastVector attributes;
    Instances dataSet;
    double[] values;
    attributes = new FastVector();
    for (i = 0; i < 512; i++) 
    attributes.addElement(new Attribute("bin" +(i+1)))  ; 

        dataSet = new Instances("NormalizedHistogram_512bins", attributes, 3);

/*28*/  values = new double[dataSet.numAttributes()]; 
        values[0] = 3;
        values[1] =7;
        values[3] = 1;
        dataSet.add(new Instance(1.0, values));

        values = new double[dataSet.numAttributes()]; 
        values[2] = 2;
        values[3] = 8;
/*37*/  dataSet.add(new Instance(1.0, values));

    BufferedImage Image = ImageIO.read(new File("airplane_training1.jpg"));
    for(int x = 0; x < Image.getWidth(); x++)
        for(int y = 0; y < Image.getHeight(); y++) {
            int color = Image.getRGB(x, y);
            int alpha = (color & 0xff000000) >> 24;
            int red = (color & 0x00ff0000) >> 16;
            int green = (color & 0x0000ff00) >> 8;
            int blue = color & 0x000000ff ;
            ch[red / 64][green / 64][blue / 64]++;
        }
    for(int k = 0; k < ch.length; k++)
        for(int j = 0; j < ch[k].length; j++)
            for(int p = 0; p < ch[k][j].length; p++)
                System.out.println( ch[k][j][p]);
    NonSparseToSparse nonSparseToSparseInstance = new NonSparseToSparse(); 
    nonSparseToSparseInstance.setInputFormat(dataSet); 
    Instances sparseImage = Filter.useFilter(dataSet, nonSparseToSparseInstance);
    System.out.println(sparseImage);

    ArffSaver arffSaverInstance = new ArffSaver(); 
    arffSaverInstance.setInstances(sparseImage); 
    arffSaverInstance.setFile(new File("ESDN.arff")); 
    arffSaverInstance.writeBatch();
 }
}

【问题讨论】:

  • 已解决。我在 IDreamInCode 上发布了这个问题,但没有得到回复,我自己设法解决了这个问题。

标签: java arrays histogram arff


【解决方案1】:
package com.test;

import java.util.Vector;

public class VectorDemo {

   public static void main(String[] args) {
   // create an empty Vector vec with an initial capacity of 4      
   Vector<Integer> vec = new Vector<Integer>(4);

   // use add() method to add elements in the vector
   vec.add(0,4);
   vec.add(1,3);
   vec.add(2,2);
   vec.add(3,1);

   // let us print all the elements available in vector
   System.out.println("Added numbers are :- "); 
   for (Integer number : vec) {         
   System.out.println("Index :"+vec.indexOf(number) +" Number: " + number);
   }

   // added new number 10 at 3rd position/index
   vec.add(3,10);

   // let us print all the elements available in vector after insertion
   System.out.println("Added numbers after insertion are :- "); 
   for (Integer number : vec) {         
   System.out.println("Index :"+vec.indexOf(number) +" Number: " + number);
   }
   }  
}

【讨论】:

    【解决方案2】:
    import java.io.File;
    import weka.core.Attribute;
    import weka.core.FastVector;
    import weka.core.Instance;
    import weka.core.Instances;
    import weka.core.converters.ArffSaver;
    import weka.filters.Filter;
    import weka.filters.unsupervised.instance.NonSparseToSparse;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    
    public class AttTest {
    
     public static void main(String[] args) throws Exception 
    {
    
    
        int i;
        FastVector attributes;
        Instances dataSet;
        double[] values;
        attributes = new FastVector();
        for (i = 0; i < 64; i++) 
        attributes.addElement(new Attribute("bin" +(i+1)))  ; 
    
    
            dataSet = new Instances("NormalizedHistogram_512bins", attributes, 0);
    
        //values = new double[dataSet.numAttributes()]; 
        //values[0] = 3;
        //values[1] =7;
        //values[3] = 1;
        //dataSet.add(new Instance(1.0, values));
    
        //values = new double[dataSet.numAttributes()]; 
        //values[2] = 2;
        //values[3] = 8;
        //dataSet.add(new Instance(1.0, values));
    
        NonSparseToSparse nonSparseToSparseInstance = new NonSparseToSparse(); 
        nonSparseToSparseInstance.setInputFormat(dataSet); 
        Instances sparseDataset = Filter.useFilter(dataSet, nonSparseToSparseInstance);
    
        System.out.println(sparseDataset);
    
        ArffSaver arffSaverInstance = new ArffSaver(); 
        arffSaverInstance.setInstances(sparseDataset); 
        arffSaverInstance.setFile(new File("ESDN.arff")); 
        arffSaverInstance.writeBatch();
    
        Histogram();
    
    
     }
    
    private static void Histogram() throws IOException {
        int[][][] ch = new int[4][4][4];
        BufferedImage image = ImageIO.read(new File("airplane_training3.jpg"));
        for(int x = 0; x < image.getWidth(); x++)
            for(int y = 0; y < image.getHeight(); y++) {
                int color = image.getRGB(x, y);
                int alpha = (color & 0xff000000) >> 24;
                int red = (color & 0x00ff0000) >> 16;
                int green = (color & 0x0000ff00) >> 8;
                int blue = color & 0x000000ff;
                ch[red / 64][green / 64][blue / 64]++;
            }
        for(int i = 0; i < ch.length; i++)
            for(int j = 0; j < ch[i].length; j++)
                for(int p = 0; p < ch[i][j].length; p++)
                    System.out.print( ch[i][j][p] + ",");
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-03
      • 1970-01-01
      • 2013-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多