Context-detection API for Android developed as a university project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Classifier.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2009-2010 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package uk.co.md87.android.common;
  23. import java.util.Map;
  24. import java.util.Map.Entry;
  25. import java.util.Set;
  26. /**
  27. * Extracts basic features and applies a K-Nearest Network algorithm to an
  28. * array of data in order to determine the classification. The data consists
  29. * of two interleaved data sets, and each set has two features extracted -
  30. * the range and the mean.
  31. *
  32. * @author chris
  33. */
  34. public class Classifier {
  35. private final Set<Map.Entry<Float[], String>> model;
  36. public Classifier(final Set<Entry<Float[], String>> model) {
  37. this.model = model;
  38. }
  39. public String classify(final float[] data) {
  40. final float oddTotal = data[5], evenTotal = data[2];
  41. final float oddMin = data[3], oddMax = data[4];
  42. final float evenMin = data[0], evenMax = data[1];
  43. final float[] points = {
  44. Math.abs(evenTotal / 128),
  45. Math.abs(oddTotal / 128),
  46. evenMax - evenMin,
  47. oddMax - oddMin
  48. };
  49. float bestDistance = Float.MAX_VALUE;
  50. String bestActivity = "UNCLASSIFIED/UNKNOWN";
  51. for (Map.Entry<Float[], String> entry : model) {
  52. float distance = 0;
  53. for (int i = 0; i < points.length; i++) {
  54. distance += Math.pow(points[i] - entry.getKey()[i], 2);
  55. }
  56. if (distance < bestDistance) {
  57. bestDistance = distance;
  58. bestActivity = entry.getValue();
  59. }
  60. }
  61. return bestActivity;
  62. }
  63. }