如何解决此错误 weka.core.UnassignedDatasetException: 实例无法访问数据集

huangapple go评论85阅读模式
英文:

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&lt;Attribute&gt; attributeList = new ArrayList&lt;Attribute&gt;();
Attribute length = new Attribute(&quot;length&quot;);
Attribute hashtag = new Attribute(&quot;#&quot;);
Attribute percentage = new Attribute(&quot;%&quot;);
Attribute at = new Attribute(&quot;@&quot;);
Attribute qustionmark = new Attribute(&quot;?&quot;);
Attribute and = new Attribute(&quot;&amp;&quot;);
Attribute singdoller = new Attribute(&quot;$&quot;);
Attribute dot = new Attribute(&quot;.&quot;);
Attribute colon = new Attribute(&quot;:&quot;);//  its mean :
Attribute doublebackshalsh = new Attribute(&quot;//&quot;);
Attribute https = new Attribute(&quot;https&quot;);
Attribute http = new Attribute(&quot;http&quot;);
Attribute ip = new Attribute(&quot;Ip&quot;);
ArrayList&lt;String&gt; classVal = new ArrayList&lt;String&gt;();
classVal.add(&quot;YES&quot;);
classVal.add(&quot;NO&quot;);
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(&quot;@@Target@&quot;,classVal));
Instances data = new Instances(&quot;TestInstances&quot;,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(&quot;The instance: &quot; + inst_co); 
//    inst_co.setDataset(race);  
// load classifier from file
Classifier cls_co = (Classifier) weka.core.SerializationHelper
.read(&quot;src/user_withexcel3/FL.model&quot;);
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);

huangapple
  • 本文由 发表于 2020年4月10日 08:43:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/61132419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定