我创建的实例未进入培训模块,我该怎么办?
public static int classify(int Newlength,int count7, int count8,int count2,int count6,int count3,int count1,int count4, int count5,boolean Backslash,boolean if_https, boolean if_http,boolean ifIP) {
// Create attributes to be used with classifiers
int result = -1;
try {
ArrayList<Attribute> attributeList = new ArrayList<Attribute>();
Attribute length = new Attribute("length");
Attribute hashtag = new Attribute("#");
Attribute percentage = new Attribute("%");
Attribute at = new Attribute("@");
Attribute qustionmark = new Attribute("?");
Attribute and = new Attribute("&");
Attribute singdoller = new Attribute("$");
Attribute dot = new Attribute(".");
Attribute colon = new Attribute(":");// its mean :
Attribute doublebackshalsh = new Attribute("//");
Attribute https = new Attribute("https");
Attribute http = new Attribute("http");
Attribute ip = new Attribute("Ip");
ArrayList<String> classVal = new ArrayList<String>();
classVal.add("YES");
classVal.add("NO");
attributeList.add(length);
attributeList.add(hashtag);
attributeList.add(percentage);
attributeList.add(at);
attributeList.add(qustionmark);
attributeList.add(and);
attributeList.add(singdoller);
attributeList.add(dot);
attributeList.add(colon);
attributeList.add(doublebackshalsh);
attributeList.add(https);
attributeList.add(http);
attributeList.add(ip);
attributeList.add(new Attribute("@@Target@",classVal));
Instances data = new Instances("TestInstances",attributeList,0);
System.out.println(data);
Instance inst_co = new DenseInstance(data.numAttributes());
data.add(inst_co);
String bs=Boolean.toString(Backslash);//for backslash
String hs=Boolean.toString(if_https);//for https
String hp=Boolean.toString(if_http);//for http
String ips=Boolean.toString(ifIP);// for ip
inst_co.setValue(length,Newlength);
inst_co.setValue(hashtag,count7);
inst_co.setValue(percentage,count8);
inst_co.setValue(at,count2);
inst_co.setValue(qustionmark, count6);
inst_co.setValue(and, count3);
inst_co.setValue(singdoller,count1);
inst_co.setValue(dot,count4);
inst_co.setValue(colon,count5);
inst_co.setValue(doublebackshalsh,2);
inst_co.setValue(https,3);
inst_co.setValue(http, 5);
inst_co.setValue(ip, 3);
System.out.println("The instance: " + inst_co);
// inst_co.setDataset(race);
// load classifier from file
Classifier cls_co = (Classifier) weka.core.SerializationHelper
.read("src/user_withexcel3/FL.model");
result = (int) cls_co.classifyInstance(inst_co);
System.out.println(result);
System.out.println(result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
在设置实例值后设置实例数据集:
inst_co.setValue(ip, 3);
// set dataset
inst_co.setDataset(data);