英文:
how to solve this error weka.core.UnassignedDatasetException: Instance doesn't have access to a dataset
问题
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) {
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(":");
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);
Instance inst_co = new DenseInstance(data.numAttributes());
data.add(inst_co);
String bs = Boolean.toString(Backslash);
String hs = Boolean.toString(if_https);
String hp = Boolean.toString(if_http);
String ips = Boolean.toString(ifIP);
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);
Classifier cls_co = (Classifier) weka.core.SerializationHelper.read("src/user_withexcel3/FL.model");
result = (int) cls_co.classifyInstance(inst_co);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
Note: The code you provided contains some HTML-encoded characters (e.g., "<" and ">") that are used to represent symbols like angle brackets. I've replaced these encoded characters with the actual symbols in the translated code.
英文:
the Instance that I create does not enter the training module what should I do?
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;
}
}
答案1
得分: 0
设置实例值后设置实例的数据集:
inst_co.setValue(ip, 3)
# 设置数据集
inst_co.setDataset(data)
英文:
Set data set of instance after setting values of instance:
inst_co.setValue(ip, 3);
// set dataset
inst_co.setDataset(data);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论