saberian | 984e52f | 2012-06-05 18:23:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
| 17 | |
| 18 | package android.bordeaux.learning; |
| 19 | |
| 20 | import java.util.HashMap; |
| 21 | import java.util.Map; |
| 22 | import android.util.Log; |
| 23 | |
| 24 | /** |
| 25 | * A simple impelentation of histograms with sparse enteries using HashMap. |
| 26 | * User can push examples or extract probabilites from this histogram. |
| 27 | */ |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame^] | 28 | public class PredictorHist { |
saberian | 984e52f | 2012-06-05 18:23:53 -0700 | [diff] [blame] | 29 | private HashMap<String, Integer> mCountHist; |
| 30 | private int mSampleCount; |
| 31 | String TAG = "PredicrtHist"; |
| 32 | |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame^] | 33 | public PredictorHist() { |
saberian | 984e52f | 2012-06-05 18:23:53 -0700 | [diff] [blame] | 34 | mCountHist = new HashMap<String, Integer>(); |
| 35 | mSampleCount = 0; |
| 36 | } |
| 37 | |
| 38 | // reset histogram |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame^] | 39 | public void resetPredictorHist() { |
saberian | 984e52f | 2012-06-05 18:23:53 -0700 | [diff] [blame] | 40 | mCountHist.clear(); |
| 41 | mSampleCount = 0; |
| 42 | } |
| 43 | |
| 44 | // getters |
| 45 | public final HashMap<String, Integer> getHist() { |
| 46 | return mCountHist; |
| 47 | } |
| 48 | |
| 49 | public int getHistCounts() { |
| 50 | return mSampleCount; |
| 51 | } |
| 52 | |
| 53 | //setter |
| 54 | public void set(HashMap<String, Integer> hist) { |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame^] | 55 | resetPredictorHist(); |
saberian | 984e52f | 2012-06-05 18:23:53 -0700 | [diff] [blame] | 56 | for (Map.Entry<String, Integer> x : hist.entrySet()) { |
| 57 | mCountHist.put(x.getKey(), x.getValue()); |
| 58 | mSampleCount = mSampleCount + x.getValue(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * pushes a new example to the histogram |
| 64 | */ |
| 65 | public void pushSample( String fs) { |
| 66 | int histValue = 1; |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame^] | 67 | if (mCountHist.containsKey(fs)) { |
saberian | 984e52f | 2012-06-05 18:23:53 -0700 | [diff] [blame] | 68 | histValue = histValue + mCountHist.get(fs); |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame^] | 69 | } |
saberian | 984e52f | 2012-06-05 18:23:53 -0700 | [diff] [blame] | 70 | mCountHist.put(fs,histValue); |
| 71 | mSampleCount++; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * return probabilty of an exmple using the histogram |
| 76 | */ |
| 77 | public float getProbability(String fs) { |
| 78 | float res = 0; |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame^] | 79 | if (mCountHist.containsKey(fs)) { |
saberian | 984e52f | 2012-06-05 18:23:53 -0700 | [diff] [blame] | 80 | res = ((float) mCountHist.get(fs)) / ((float)mSampleCount); |
Ruei-sung Lin | f0f7844 | 2012-08-13 19:04:29 -0700 | [diff] [blame^] | 81 | } |
saberian | 984e52f | 2012-06-05 18:23:53 -0700 | [diff] [blame] | 82 | return res; |
| 83 | } |
| 84 | } |