2017-11-27 4 views
0

저는 Instances 객체를 구축하고 Attributes를 추가 한 다음 Instance 객체의 형태로 데이터를 추가합니다.Weka core의 문제 DenseInstance

필자가 작성하려고하면 toString() 메서드는 이미 OutOfBoundsException을 던지고 인스턴스의 데이터를 평가할 수 없습니다. 데이터를 인쇄하려고 할 때 오류가 발생하고 데이터 객체의 toString()을 평가할 수 없다는 것을 보여 주므로 예외가 디버거에 던져지는 것을 볼 수 있습니다.

단 하나의 단서는 오류 메시지가 첫 번째 데이터 요소 (StudentId)를 사용하여 색인으로 사용하는 것입니다. 나는 왜 그런지 혼란 스럽다.

코드 :

// Set up the attributes for the Weka data model 
ArrayList<Attribute> attributes = new ArrayList<>(); 
attributes.add(new Attribute("StudentIdentifier", true)); 
attributes.add(new Attribute("CourseGrade", true)); 
attributes.add(new Attribute("CourseIdentifier")); 
attributes.add(new Attribute("Term", true)); 
attributes.add(new Attribute("YearCourseTaken", true)); 

// Create the data model object - I'm not happy that capacity is required and fixed? But that's another issue 
Instances dataSet = new Instances("Records", attributes, 500); 
// Set the attribute that will be used for prediction purposes - that will be CourseIdentifier 
dataSet.setClassIndex(2); 

// Pull back all the records in this term range, create Weka Instance objects for each and add to the data set 
List<Record> records = recordsInTermRangeFindService.find(0, 10); 
int count = 0; 
for (Record r : records) { 
    Instance i = new DenseInstance(attributes.size()); 

    i.setValue(attributes.get(0), r.studentIdentifier); 
    i.setValue(attributes.get(1), r.courseGrade); 
    i.setValue(attributes.get(2), r.courseIdentifier); 
    i.setValue(attributes.get(3), r.term); 
    i.setValue(attributes.get(4), r.yearCourseTaken); 

    dataSet.add(i); 
} 

System.out.println(dataSet.size()); 
BufferedWriter writer = null; 
try { 
    writer = new BufferedWriter(new FileWriter("./test.arff")); 
    writer.write(dataSet.toString()); 
    writer.flush(); 
    writer.close(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

오류 메시지가 :

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1010, Size: 0 

답변

0

나는 마침내 그것을 알아 냈어. Constructors에서 'true'두 번째 매개 변수를 사용하여 Attributes를 문자열로 설정했지만 데이터베이스 테이블에서 정수가 나오게되었습니다. 내가 필요한 문자열을 정수로 변환 내 라인을 변경하려면 다음 문자열을 작동하지 않습니다 연역적 알고리즘 같은 것들로 나를 위해 문제의 다른 세트를 생성하지만

i.setValue(attributes.get(0), Integer.toString(r.studentIdentifier)); 

을! Weka를 계속 배우고 있습니다.

관련 문제