8

Tôi đã đào tạo và tạo mô hình J48 sử dụng WEKA gui. Tôi đã lưu tệp mô hình vào máy tính của mình và bây giờ tôi muốn sử dụng nó để phân loại một cá thể đơn lẻ trong mã Java của tôi. Tôi muốn nhận được một dự đoán cho thuộc tính "cluster". Những gì tôi làm là như sau:Phân loại đơn lẻ trong Weka

public void classify(double lat, double lon, double co) 
{    

// Create attributes to be used with classifiers 
        Attribute latitude = new Attribute("latitude"); 
        Attribute longitude = new Attribute("longitude"); 
        Attribute carbonmonoxide = new Attribute("co"); 

        // Create instances for each pollutant with attribute values latitude, longitude and pollutant itself 
        inst_co = new DenseInstance(4); 

        // Set instance's values for the attributes "latitude", "longitude", and "pollutant concentration" 
        inst_co.setValue(latitude, lat); 
        inst_co.setValue(longitude, lon); 
        inst_co.setValue(carbonmonoxide, co); 
        inst_co.setMissing(cluster); 


    Classifier cls_co = (Classifier) weka.core.SerializationHelper.read("/CO_J48Model.model");//load classifier from file 

        // Test the model 
     double result = cls_co.classifyInstance(inst_co); 
} 

Tuy nhiên, tôi nhận được một IndexArrayOutofBoundsException trên dòng inst_co.setValue(latitude, lat);. Tôi không thể tìm ra lý do cho ngoại lệ này. Tôi sẽ đánh giá cao nếu ai đó có thể chỉ cho tôi đi đúng hướng.

Trả lời

8

Bạn cần thêm inst_co vào tập dữ liệu của mình, đối tượng Instances. Mã sau đây sẽ hoạt động.

import java.util.ArrayList; 

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

public class QuestionInstanceClassifiy { 

    public static void main(String[] args) { 
     QuestionInstanceClassifiy q = new QuestionInstanceClassifiy(); 
     double result = q.classify(1.0d, 1, 1); 
     System.out.println(result); 
    } 

    private Instance inst_co; 

    public double classify(double lat, double lon, double co) { 

     // Create attributes to be used with classifiers 
     // Test the model 
     double result = -1; 
     try { 

      ArrayList<Attribute> attributeList = new ArrayList<Attribute>(2); 

      Attribute latitude = new Attribute("latitude"); 
      Attribute longitude = new Attribute("longitude"); 
      Attribute carbonmonoxide = new Attribute("co"); 

      ArrayList<String> classVal = new ArrayList<String>(); 
      classVal.add("ClassA"); 
      classVal.add("ClassB"); 


      attributeList.add(latitude); 
      attributeList.add(longitude); 
      attributeList.add(carbonmonoxide); 
      attributeList.add(new Attribute("@@[email protected]@",classVal)); 

      Instances data = new Instances("TestInstances",attributeList,0); 


      // Create instances for each pollutant with attribute values latitude, 
      // longitude and pollutant itself 
      inst_co = new DenseInstance(data.numAttributes()); 
      data.add(inst_co); 

      // Set instance's values for the attributes "latitude", "longitude", and 
      // "pollutant concentration" 
      inst_co.setValue(latitude, lat); 
      inst_co.setValue(longitude, lon); 
      inst_co.setValue(carbonmonoxide, co); 
      // inst_co.setMissing(cluster); 

      // load classifier from file 
      Classifier cls_co = (Classifier) weka.core.SerializationHelper 
        .read("/CO_J48Model.model"); 

      result = cls_co.classifyInstance(inst_co); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return result; 
    } 
} 

Bạn tạo đối tượng dữ liệu từ Trường hợp. Thêm cá thể của bạn vào dữ liệu này. Sau đó bạn có thể thiết lập các giá trị của bạn trong Instance.

Instances data = new Instances("TestInstances",attributeList,0); 
inst_co = new DenseInstance(data.numAttributes()); 
data.add(inst_co); 

Tôi khuyên bạn nên lấy thông tin tiêu đề và Giá trị cá thể từ tệp bên ngoài hoặc chỉ tạo thông tin này một lần.

+0

Cảm ơn bạn đã có câu trả lời tuyệt vời. Chỉ cần làm rõ, classA và classB là kết quả có thể có của phân loại, tức là tên cụm của tôi, phải không? Tôi đoán họ cần phải giống với những cái được sử dụng trong khi tôi đang tạo mô hình. – Erol

+0

Không hoạt động, tôi nhận được weka.core.UnassignedDatasetException: DenseInstance không có quyền truy cập vào tập dữ liệu! lỗi. Đoán tôi sẽ phải gán nó cho một tập dữ liệu, có lẽ cái tôi dùng để huấn luyện nó? – Erol

+0

@babatenor bạn cần gán cho tập dữ liệu có cùng tiêu đề. Thông tin tiêu đề của chúng phải giống nhau –

3

Thực ra tôi đã thử trong trường hợp của mình là gọi phương thức instance.setDataSet(), không phải phương thức addInstance. Mã của bạn phải là inst_co.setDataSet (dữ liệu).