2013-11-20 3 views
3

위도와 경도로 지정된 점을 클러스터링하려고합니다. 사용하고 있습니다 WEKA API 문제는 Instances instances = new Instances(40.01,1.02); 입니다. ARFF 파일을 사용하지 않고 입력 데이터를 지정하는 방법은 무엇입니까? Instances에 배열을 읽으 려합니다.WEKA API를 사용하여 클러스터링을위한 입력 데이터 정의

import java.io.Reader; 

import weka.clusterers.ClusterEvaluation; 
import weka.clusterers.SimpleKMeans; 
import weka.core.Instances; 


public class test { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     Instances instances = new Instances(40.01,1.02); 

     SimpleKMeans simpleKMeans = new SimpleKMeans(); 
     simpleKMeans.buildClusterer(instances); 

     ClusterEvaluation eval = new ClusterEvaluation(); 
     eval.setClusterer(simpleKMeans); 
     eval.evaluateClusterer(new Instances(instances)); 

     eval.clusterResultsToString(); 
    } 

} 

답변

3

나는 자신 만의 인스턴스를 만들어야한다고 생각합니다. 아래에서는 위도와 경도의 두 속성을 가진 배열에서 새 인스턴스를 만드는 방법을 보여줍니다.


import weka.core.Attribute; 
import weka.core.DenseInstance; 
import weka.core.FastVector; 
import weka.core.Instances; 

public class AttTest { 

    public static void main(String[] args) throws Exception 
    { 
     double[] one={0,1,2,3}; 
     double[] two={3,2,1,0}; 
     double[][] both=new double[2][4]; 
     both[0]=one; 
     both[1]=two; 

     Instances to_use=AttTest.buildArff(both); 
     System.out.println(to_use.toString()); 
    } 

    public static Instances buildArff(double[][] array) throws Exception 
    { 
     FastVector  atts = new FastVector(); 
     atts.addElement(new Attribute("lat")); //latitude 
     atts.addElement(new Attribute("lon")); //longitude 

     // 2. create Instances object 
     Instances test = new Instances("location", atts, 0); 

     // 3. fill with data 
     for(int s1=0; s1 < array[0].length; s1=s1+1) 
     { 
      double vals[] = new double[test.numAttributes()]; 
      vals[0] = array[0][s1]; 
      vals[1] = array[1][s1]; 
      test.add(new DenseInstance(1.0, vals)); 
     } 

     return(test); 
    } 
}
관련 문제