J48(ClassifierTree) classifyInstance & distributionForInstance
Weka の 決定木アルゴリズム(C4.5)の実装 J48 利用時にclassifyInstance と distributionForInstance の仕様が良くわからず、Javadoc 見てもイマイチだったのでソースで確認した。
http://weka.sourceforge.net/doc.dev/weka/classifiers/trees/J48.html
Sourcecode: ClassifierTree#classfiyInstance
/** * Classifies an instance. * * @param instance the instance to classify * @return the classification * @throws Exception if something goes wrong */ public double classifyInstance(Instance instance) throws Exception { double maxProb = -1; double currentProb; int maxIndex = 0; int j; for (j = 0; j < instance.numClasses(); j++) { currentProb = getProbs(j, instance, 1); if (Utils.gr(currentProb, maxProb)) { maxIndex = j; maxProb = currentProb; // find high probability to class } } return maxIndex; // return max probability class's index
Sourcecode: ClassifierTree#distributionForInstance
/** * Returns class probabilities for a weighted instance. * * @param instance the instance to get the distribution for * @param useLaplace whether to use laplace or not * @return the distribution * @throws Exception if something goes wrong */ public final double[] distributionForInstance(Instance instance, boolean useLaplace) throws Exception { double[] doubles = new double[instance.numClasses()]; for (int i = 0; i < doubles.length; i++) { if (!useLaplace) { doubles[i] = getProbs(i, instance, 1); } else { doubles[i] = getProbsLaplace(i, instance, 1); } } return doubles; }
API 呼出前に、instance に 学習時に指定したclass 群を指定しておき、
- classifyInstance であれば、最も確率の高い class の index を返却
- distributionForInstance であれば、class 毎の確率の配列を class の index と揃えた double[] で返却する
ということがわかった。