2012-08-25 31 views
6

Làm cách nào để thêm một thể hiện mới vào đối tượng Instances hiện có mà tôi đã tạo?Thêm một thể hiện mới trong weka

Dưới đây là một ví dụ:

ArrayList<Attribute> atts = new ArrayList<Attribute>(2); 
ArrayList<String> classVal = new ArrayList<String>(); 
classVal.add("A"); 
classVal.add("B"); 
atts.add(new Attribute("content",(ArrayList<String>)null)); 
atts.add(new Attribute("@@[email protected]@",classVal)); 

Instances dataRaw = new Instances("TestInstances",atts,0); 

Tôi muốn thêm một trường hợp mới để dataRaw. Theo như tôi biết tôi phải sử dụng dataRaw.add (Instance i) .... Làm thế nào tôi có thể tạo một đối tượng thể hiện nếu lớp Instance là một giao diện?

Cảm ơn trước Advance

+0

Vượt qua một thực hiện của 'Instance', mà bạn có thể nhìn thấy [ở đây] (http://weka.sourceforge.net/doc.dev/weka/core/Instance.html) trong __All Lớp học triển khai đã biết__. – oldrinb

+0

PS bạn sẽ không thể thêm bất kỳ thứ gì, vì bạn đã chuyển '0' thành đối số' capacity' trong hàm tạo. – oldrinb

+0

Tôi đã làm theo hướng dẫn này tại đây (http://weka.wikispaces.com/Creating+an+ARFF+file) và theo như tôi hiểu thì đây là dung lượng ban đầu. bạn có thể xin vui lòng cho tôi một ví dụ làm thế nào để thêm một thể hiện mới cho đối tượng mà tôi tạo ra? Cảm ơn ... – TeFa

Trả lời

15

Hãy bắt đầu với một số điểm nổi bật.

  • weka lưu trữ mọi giá trị mẫu thành gấp đôi []. Do đó bạn tạo double [] instanceValue1 và thêm các giá trị vào mảng này.
  • Thậm chí các chuỗi được lưu trong cặp đôi này []. Bạn thêm các giá trị chuỗi của bạn với đoạn mã sau:

instanceValue1 [0] = dataRaw.attribute (0) .addStringValue ("! Đây là một chuỗi");

  • Instance là giao diện, và nó được thực hiện bởi hai lớp, {SparseInstance, DenseInstance}. Chúng tôi thêm mảng giá trị của chúng tôi để tập dữ liệu sử dụng đoạn mã sau:

dataRaw.add (DenseInstance mới (1.0, instanceValue1));

Dưới đây là hoàn toàn ví dụ chạy:

import java.util.ArrayList; 

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

public class Program { 
    public static void main(String[] args) { 
     ArrayList<Attribute> atts = new ArrayList<Attribute>(2); 
     ArrayList<String> classVal = new ArrayList<String>(); 
     classVal.add("A"); 
     classVal.add("B"); 
     atts.add(new Attribute("content",(ArrayList<String>)null)); 
     atts.add(new Attribute("@@[email protected]@",classVal)); 

     Instances dataRaw = new Instances("TestInstances",atts,0); 
     System.out.println("Before adding any instance"); 
     System.out.println("--------------------------"); 
     System.out.println(dataRaw); 
     System.out.println("--------------------------"); 

     double[] instanceValue1 = new double[dataRaw.numAttributes()]; 

     instanceValue1[0] = dataRaw.attribute(0).addStringValue("This is a string!"); 
     instanceValue1[1] = 0; 

     dataRaw.add(new DenseInstance(1.0, instanceValue1)); 

     System.out.println("After adding a instance"); 
     System.out.println("--------------------------"); 
     System.out.println(dataRaw); 
     System.out.println("--------------------------"); 

     double[] instanceValue2 = new double[dataRaw.numAttributes()]; 

     instanceValue2[0] = dataRaw.attribute(0).addStringValue("This is second string!"); 
     instanceValue2[1] = 1; 

     dataRaw.add(new DenseInstance(1.0, instanceValue2)); 

     System.out.println("After adding second instance"); 
     System.out.println("--------------------------"); 
     System.out.println(dataRaw); 
     System.out.println("--------------------------"); 


    } 

} 

đầu ra của nó là như sau:

Before adding any instance 
-------------------------- 
@relation TestInstances 

@attribute content string 
@attribute @@[email protected]@ {A,B} 

@data 

-------------------------- 
After adding a instance 
-------------------------- 
@relation TestInstances 

@attribute content string 
@attribute @@[email protected]@ {A,B} 

@data 
'This is a string!',A 
-------------------------- 
After adding second instance 
-------------------------- 
@relation TestInstances 

@attribute content string 
@attribute @@[email protected]@ {A,B} 

@data 
'This is a string!',A 
'This is second string!',B 
-------------------------- 
+0

Tôi có thể biết bạn đã sử dụng api nào không? – aceminer

+0

Weka và bộ sưu tập java bình thường api. –

+0

Xin chào @AtillaOzgur Tôi đã nhận -1 cho "dataRaw.attribute (0) .addStringValue (" Đây là chuỗi thứ hai! ");" một phần trong mã của tôi khi tôi cố gắng gán cho đôi [] var Bạn có thể đưa ra gợi ý không ...... –

Các vấn đề liên quan