Learning_MulticlassPA.java revision 6b4eebc73439cbc3ddfb547444a341d1f9be7996
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.bordeaux.services;
18
19import android.bordeaux.learning.MulticlassPA;
20import java.util.List;
21import java.util.ArrayList;
22
23public class Learning_MulticlassPA extends ILearning_MulticlassPA.Stub {
24    private MulticlassPA mMulticlassPA_learner;
25
26    class IntFloatArray {
27        int[] indexArray;
28        float[] floatArray;
29    };
30
31    private IntFloatArray splitIntFloatArray(List<IntFloat> sample) {
32        IntFloatArray splited = new IntFloatArray();
33        ArrayList<IntFloat> s = (ArrayList<IntFloat>)sample;
34        splited.indexArray = new int[s.size()];
35        splited.floatArray = new float[s.size()];
36        for (int i = 0; i < s.size(); i++) {
37            splited.indexArray[i] = s.get(i).index;
38            splited.floatArray[i] = s.get(i).value;
39        }
40        return splited;
41    }
42
43    public Learning_MulticlassPA() {
44        mMulticlassPA_learner = new MulticlassPA(2, 2, 0.001f);
45    }
46
47    // This implementation, combines training and prediction in one step.
48    // The return value is the prediction value for the supplied sample. It
49    // also update the model with the current sample.
50    public void TrainOneSample(List<IntFloat> sample, int target) {
51        IntFloatArray splited = splitIntFloatArray(sample);
52        mMulticlassPA_learner.sparseTrainOneExample(splited.indexArray,
53                                                    splited.floatArray,
54                                                    target);
55    }
56
57    public int Classify(List<IntFloat> sample) {
58        IntFloatArray splited = splitIntFloatArray(sample);
59        int prediction = mMulticlassPA_learner.sparseGetClass(splited.indexArray,
60                                                              splited.floatArray);
61        return prediction;
62    }
63
64}
65