Hi Jaggu,
I am also working with 0.7 . I too tried input vector to the classifier .
Vector nbResult = nbClassifier.classifyFull(getVector());
But don't know how to get the correct label. I got the following piece of
code from
/mahout-distribution-0.7/core/src/main/java/org/apache/mahout/classifier/naivebayes/test/TestNaiveBayesDriver.java
for analyzing result.
private static void analyzeResults(Map<Integer, String> labelMap,
SequenceFileDirIterable<Text,
VectorWritable> dirIterable,
ResultAnalyzer analyzer) {
for (Pair<Text, VectorWritable> pair : dirIterable) {
int bestIdx = Integer.MIN_VALUE;
double bestScore = Long.MIN_VALUE;
for (Vector.Element element : pair.getSecond().get()) {
if (element.get() > bestScore) {
bestScore = element.get();
bestIdx = element.index();
}
}
if (bestIdx != Integer.MIN_VALUE) {
ClassifierResult classifierResult = new
ClassifierResult(labelMap.get(bestIdx), bestScore);
analyzer.addInstance(pair.getFirst().toString(), classifierResult);
}
}
}
But couldn't get the correct label.
On Thu, Oct 11, 2012 at 3:33 PM, JAGANADH G <[email protected]> wrote:
> Hi
>
> I just created a sample use class of NaiveBayes . Can somebody say wheather
> I am in the right track or not
>
> Here is my code
>
> public class NaiveBayesClassifierExample {
>
> public static void loadClassifier(String strModelPath, Vector v)
> throws IOException {
> Configuration conf = new Configuration();
>
> NaiveBayesModel model = NaiveBayesModel.materialize(new Path(
> strModelPath), conf);
> AbstractNaiveBayesClassifier classifier = new StandardNaiveBayesClassifier(
> model);
>
> Vector st = classifier.classifyFull(v);
> System.out.println(st.toString());
> }
>
> public static Vector createVect() throws IOException {
> FeatureVectorEncoder encoder = new StaticWordValueEncoder("text");
> Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
> StringReader in = new StringReader(
> "The movie sherk was very cool and attractive one. We like the movie"
> + "because of the theme and directon. All the actores were excellent");
>
> TokenStream ts = analyzer.tokenStream("body", in);
>
> CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
> Vector v1 = new RandomAccessSparseVector(100000);
>
> while (ts.incrementToken()) {
> char[] termBuffer = termAtt.buffer();
> int termLen = termAtt.length();
> String w = new String(termBuffer, 0, termLen);
> encoder.addToVector(w, 1.0, v1);
> }
> v1.normalize();
> return v1;
> }
>
> public static void main(String[] args) throws IOException {
> Vector v = createVect();
> String mp =
> "/home/u179995/Downloads/mahout-distribution-0.7/playg/movie_model";
> loadClassifier(mp, v);
> }
> }
>
> --
> **********************************
> JAGANADH G
> http://jaganadhg.in
> *ILUGCBE*
> http://ilugcbe.org.in
>
--
Thank You
Sarath P R | cell +91 99 95 02 4287 | http://sprism.blogspot.com