本篇文章針對weka API的幾個簡單使用介紹,weka官網文檔介紹非常詳細,可參考。
1.?weka官網:https://www.cs.waikato.ac.nz/~ml/weka/
2. weka官方文檔:https://waikato.github.io/weka-wiki/
3. weka API文檔:https://weka.sourceforge.io/doc.stable-3-8/
4. 推薦值得學習的blog:https://www.cnblogs.com/zlslch/category/999872.html
此博客是介紹weka比較系統(tǒng)和詳細的博客冤灾,有些不明白的地方可以查看官網。
備注:weka官方文檔上有相應例子辕近,有些例子對應版本比較舊,可對應更新為自己使用的版本
建議看下官網的有關weka視頻的介紹:https://youtu.be/TF1yh5PKaqI
一匿垄、Java調用Weka API創(chuàng)建Arff文件
Arff(attribute relation file format 關系屬性格式)文件是一種由獨立移宅、無序實例組成的數(shù)據集文件归粉,是一種 ASCII 文本文件。在 Arff 文件中漏峰,%開始表示注釋糠悼;@relation 表示數(shù)據之間的關系;@attribute 表示字段名稱和字段類型浅乔;@data 表示具體的數(shù)據倔喂,同時數(shù)據的順序要和@attribute 中的屬性保持一致靖苇。文件內容中悼枢,最開始的部分顯示文件注釋,之后顯示關系的名字和屬性的具體定義,在屬性下是具體的數(shù)據集合。
1. 總共就這么四步,關鍵代碼:
1)一開始需要實例化一個Vector來保存數(shù)據屬性:
FastVector atts =newFastVector();// 保存屬性
2)同時需要有保存單條數(shù)據
double[]vals;// arff保存單條數(shù)據
3)判斷數(shù)據格式之后新建數(shù)據屬性:
atts.addElement(newAttribute(numName));
4)之后填充數(shù)據:
vals[j] =instances.attribute(j).addStringValue("XXXXX");
2.?完整代碼:
/** * 創(chuàng)建合法格式的Arff文件 * * @param data 該參數(shù)為封裝好的Data參數(shù),數(shù)據類型為 * Map<String, List<Object>> * @return * @throws Exception */
? ? public String createArffFile(Data data, int userID) throws Exception {
? ? ? ? FastVector atts = new FastVector();// 保存屬性
? ? ? ? double[] vals;// arff保存單條數(shù)據
? ? ? ? // 創(chuàng)建List作為數(shù)據類型flag
? ? ? ? List<Object> indexList = new LinkedList<Object>();
? ? ? ? // 獲取data數(shù)據
? ? ? ? wekaData = XMLParser.praseXML(data.getData_Content());
? ? ? ? // 獲取data中的第0行為title,第一行為判斷數(shù)據類型
? ? ? ? List<Object> titleList = wekaData.get("0");
? ? ? ? List<Object> firstList = wekaData.get("1");
? ? ? ? // 遍歷title中的數(shù)據類型,為arff文件創(chuàng)建attribute
? ? ? ? for (int i = 0; i < firstList.size(); i++) {
? ? ? ? ? ? Object obj = firstList.get(i);
? ? ? ? ? ? String objStr = obj.toString();
? ? ? ? ? ? if (isNumeric(objStr)) {
? ? ? ? ? ? ? ? indexList.add("Numeric");
? ? ? ? ? ? ? ? String numName = String.valueOf(titleList.get(i));
? ? ? ? ? ? ? ? atts.addElement(new Attribute(numName));
? ? ? ? ? ? } else if (isValidDate(objStr)) {
? ? ? ? ? ? ? ? indexList.add("Date");
? ? ? ? ? ? ? ? atts.addElement(new Attribute((String) titleList.get(i), "yyyy-MM-dd"));
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? indexList.add("String");
? ? ? ? ? ? ? ? atts.addElement(new Attribute((String) titleList.get(i), (FastVector) null));
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? // 必須放在創(chuàng)建attribute之后 否則會報異常
? ? ? ? Instances instances = new Instances(data.getData_Name(), atts, 0); // 創(chuàng)建一個weka data實例
? ? ? ? System.out.println(indexList);
? ? ? ? // 填充數(shù)據
? ? ? ? for (int i = 1; i < wekaData.size(); i++) {
? ? ? ? ? ? vals = new double[instances.numAttributes()];
? ? ? ? ? ? String index = String.valueOf(i);
? ? ? ? ? ? int length = wekaData.get(index).size();
? ? ? ? ? ? System.out.println(length);
? ? ? ? ? ? // 遍歷填充
? ? ? ? ? ? for (int j = 0; j < length; j++) {
? ? ? ? ? ? ? ? if (indexList.get(j).equals("String")) {
? ? ? ? ? ? ? ? ? ? System.out.println("string");
? ? ? ? ? ? ? ? ? ? vals[j] = instances.attribute(j).addStringValue((String) wekaData.get(index).get(j));
? ? ? ? ? ? ? ? } else if (indexList.get(j).equals("Numeric")) {
? ? ? ? ? ? ? ? ? ? System.out.println("number");
? ? ? ? ? ? ? ? ? ? System.out.println((String) wekaData.get(index).get(j));
? ? ? ? ? ? ? ? ? ? double num = Double.valueOf((String) wekaData.get(index).get(j));
? ? ? ? ? ? ? ? ? ? vals[j] = num;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? instances.add(new Instance(1.0, vals));
? ? ? ? ? ? // end
? ? ? ? }
? ? ? ? System.out.println(instances.toString());
? ? ? ? // IP時間戳工具
? ? ? ? IPTimeStamp ipTimeStamp = new IPTimeStamp();
? ? ? ? // 保存文件加時間戳重命名获印,防止不同文件重名覆蓋
? ? ? ? String fileName = ipTimeStamp.getIPTimeRand() + "_" + userID + "_" + data.getData_Name();
? ? ? ? String path = configProperty.getArffPath() + fileName + ".arff";
? ? ? ? File outFile = new File(path);
? ? ? ? FileUtils.writeStringToFile(outFile, instances.toString(), false);
? ? ? ? System.out.println("ArffPath" + configProperty.getArffPath());
? ? ? ? return path;
? ? }
構建好的格式舉例:K-means聚類樣本.arff
@relation K-means聚類樣本
@attribute X numeric
@attribute Y numeric
@data
0,0
1,0
二鳍征、Weka API使用訓練好的已知模型進行實時預測
1、自己根據屬性構建instance實例。
2、調用之前已經訓練的模型,調用時需要將模型強制轉變?yōu)槟P蜋C器學習類型,如NaiveBayes的模型需要這樣操作。
Classifier m_Classifier =?(NaiveBayes)SerializationHelper.read(new FileInputStream("model/bayes.model"));
以下是本文構建的Weka實時預測功能:
本實例中instance具有四個屬性衷佃,第一個為double屬性惯悠,第二個是double屬性克婶,第三個是Nominal類型,第四個是Class類型(預測值)。
注:本文中使用的模型可以使用Weka圖形界面操作生成鸠补。
代碼思路:
首先萝风,構建一個instances結構,構建instances具有什么樣的屬性紫岩;其次,指定instances的類別索引睬塌,即指定哪個屬性代表的是類別泉蝌。之后,構建instance實例揩晴,將instances的結構框架指定為instance的數(shù)據集勋陪,給instance賦值,此處傳值時不需要傳入Class值硫兰,因為這是我們要預測的诅愚;最后,使用已知模型的classifyInstance方法對instance進行預測劫映,再根據預測出的索引得到預測類別的值违孝。
以下是編寫的Weka實時預測代碼:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import weka.classifiers.Classifier;
import weka.classifiers.bayes.NaiveBayes;
import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.SerializationHelper;
public class WekaTestInstance
{
? ? Instances m_Data = null;
? ? Classifier m_Classifier = null;
? ? public WekaTestInstance() throws FileNotFoundException, Exception
? ? {
? ? ? ? m_Classifier = (NaiveBayes)SerializationHelper.read(new FileInputStream("model/bayes.model"));?
? ? ? ? String nameOfDataset = "messDataset";
? ? ? ? FastVector attributes = new FastVector();
? ? ? ? attributes.addElement(new Attribute("aa"));
? ? ? ? attributes.addElement(new Attribute("bb"));
? ? ? ? FastVector fvNominalVal = new FastVector(3);
? ? ? ? fvNominalVal.addElement("blue");
? ? ? ? fvNominalVal.addElement("gray");
? ? ? ? fvNominalVal.addElement("black");? ? ? ? ? ? ?
? ? ? ? attributes.addElement(new Attribute("Nominal", fvNominalVal));
? ? ? ? FastVector classValues = new FastVector(2);
? ? ? ? classValues.addElement("pos");
? ? ? ? classValues.addElement("neg");
? ? ? ? attributes.addElement(new Attribute("Class", classValues));
? ? ? ? m_Data = new Instances(nameOfDataset, attributes, 10);
? ? ? ? m_Data.setClassIndex(m_Data.numAttributes()-1);
? ? }
? ? public void classifyMessage(double aa,double bb,String nominal) throws Exception
? ? {
? ? ? ? Instances testset = m_Data.stringFreeStructure();
? ? ? ? Instance instance = makeInstance(aa,bb,nominal,testset);
? ? ? ? System.out.println(m_Data.numAttributes());
? ? ? ? System.out.println(instance);
? ? ? ? double predicted = m_Classifier.classifyInstance(instance);
? ? ? ? System.out.println("predicted:"+predicted);
? ? ? ? System.out.println("Message classified as : " +
? ? ? ? ? ? ? ? m_Data.classAttribute().value((int)predicted));
? ? }
? ? private Instance makeInstance(double aa,double bb,String nominal,Instances data)
? ? {
? ? ? ? Instance instance = new Instance(4);
? ? ? ? instance.setDataset(data);
? ? ? ? Attribute aaAtt = data.attribute("aa");
? ? ? ? Attribute bbAtt = data.attribute("bb");
? ? ? ? Attribute nominalAtt = data.attribute("Nominal");
? ? ? ? instance.setValue(aaAtt, aa);
? ? ? ? instance.setValue(bbAtt, bb);
? ? ? ? instance.setValue(nominalAtt, nominal);
//? ? ? instance.setValue((Attribute)instance.attribute(0), aa);
//? ? ? instance.setValue((Attribute)instance.attribute(1), bb);
//? ? ? instance.setValue((Attribute)instance.attribute(2),nominal);? ? ? ?
? ? ? ? return instance;
? ? }
? ? public static void main(String[] args) throws Exception
? ? {
? ? ? ? WekaTestInstance wTestInstance = new WekaTestInstance();
? ? ? ? wTestInstance.classifyMessage(5.6,9.9,"gray");
? ? }
}