我使用WEKA gui训练并创建了一个Nativebases。我将模型文件保存到我的计算机上,现在我想用它来分类我的Java代码中的单个实例。我想获得属性“cluster”的预测。我的工作如下:
package level_4_project_weka;
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 wekaClass2 {
private Instance inst_co;
String classify(
float area_of_disease1,
float length_of_disease1,
int s_area_count1,
float sd_disease_r1,
float sd_disease_g1,
float sd_disease_b1,
float sd_background_r1,
float sd_background_g1,
float sd_background_b1,
int m_disease_r1,
int m_disease_g1,
int m_disease_b1,
int m_background_r1,
int m_background_g1,
int m_background_b1,
String fluid_filled_blisters1,
String feel_itchy1) {
String result = null;
try {
ArrayList<Attribute> attributeList = new ArrayList<>(17);
Attribute area_of_disease = new Attribute("area_of_disease");
Attribute length_of_disease = new Attribute("length_of_disease");
Attribute s_area_count = new Attribute("s_area_count");
Attribute sd_disease_r = new Attribute("sd_disease_r");
Attribute sd_disease_g = new Attribute("sd_disease_g");
Attribute sd_disease_b = new Attribute("sd_disease_b");
Attribute sd_background_r = new Attribute("sd_background_r");
Attribute sd_background_g = new Attribute("sd_background_g");
Attribute sd_background_b = new Attribute("sd_background_b");
Attribute m_disease_r = new Attribute("m_disease_r");
Attribute m_disease_g = new Attribute("m_disease_g");
Attribute m_disease_b = new Attribute("m_disease_b");
Attribute m_background_r = new Attribute("m_background_r");
Attribute m_background_g = new Attribute("m_background_g");
Attribute m_background_b = new Attribute("m_background_b");
Attribute fluid_filled_blisters = new Attribute("fluid_filled_blisters");
Attribute feel_itchy = new Attribute("feel_itchy");
ArrayList<String> classVal = new ArrayList<>();
classVal.add("melanoma");
classVal.add("eczema");
classVal.add("impetigo");
//classVal.add("ClassB");
attributeList.add(area_of_disease);
attributeList.add(length_of_disease);
attributeList.add(s_area_count);
attributeList.add(sd_disease_r);
attributeList.add(sd_disease_g);
attributeList.add(sd_disease_b);
attributeList.add(sd_background_r);
attributeList.add(sd_background_g);
attributeList.add(sd_background_b);
attributeList.add(m_disease_r);
attributeList.add(m_disease_g);
attributeList.add(m_disease_b);
attributeList.add(m_background_r);
attributeList.add(m_background_g);
attributeList.add(m_background_b);
attributeList.add(fluid_filled_blisters);
attributeList.add(feel_itchy);
attributeList.add(new Attribute("@@type@@",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(area_of_disease,area_of_disease1);
inst_co.setValue(length_of_disease,length_of_disease1);
inst_co.setValue(s_area_count,s_area_count1);
inst_co.setValue(sd_disease_r,sd_disease_r1);
inst_co.setValue(sd_disease_g,sd_disease_g1);
inst_co.setValue(sd_disease_b,sd_disease_b1);
inst_co.setValue(sd_background_r,sd_background_r1);
inst_co.setValue(sd_background_g,sd_background_g1);
inst_co.setValue(sd_background_b,sd_background_b1);
inst_co.setValue(m_disease_r,m_disease_r1);
inst_co.setValue(m_disease_g,m_disease_g1);
inst_co.setValue(m_disease_b,m_disease_b1);
inst_co.setValue(m_background_r,m_background_r1);
inst_co.setValue(m_background_g,m_background_g1);
inst_co.setValue(m_background_b,m_background_b1);
inst_co.setValue(fluid_filled_blisters,fluid_filled_blisters1);
inst_co.setValue(feel_itchy,feel_itchy1);
// inst_co.setMissing(cluster);
// load classifier from file
Classifier cls_co = (Classifier) weka.core.SerializationHelper
.read("C:/Users/Lahiru/Documents/NetBeansProjects/level_4_project_weka/diseases.model");
double result1 = cls_co.classifyInstance(inst_co);
System.out.println("a" + result1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
但是当我添加
时 `inst_co.setValue(fluid_filled_blisters,fluid_filled_blisters1);
inst_co.setValue(feel_itchy,feel_itchy1);`
我收到了以下错误。
java.lang.IllegalArgumentException: Attribute neither nominal nor string!
at weka.core.AbstractInstance.setValue(AbstractInstance.java:518)
at level_4_project_weka.wekaClass2.classify(wekaClass2.java:112)
at level_4_project_weka.Level_4_project_weka.PredictDisease(Level_4_project_weka.java:102)
at level_4_project_weka.Level_4_project_weka.main(Level_4_project_weka.java:27)
我知道,这个错误的发生是因为这两个是字符串变量。但我不知道如何处理字符串。有人能告诉我这样做的正确方法吗?
答案 0 :(得分:0)