Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. package classifiers;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Random;
  6.  
  7. public class MeanObject extends ClassificationObject {
  8.  
  9. ArrayList<ClassificationObject> nearestObjects = new ArrayList<>();
  10. Double quantizationError;
  11.  
  12. public MeanObject(int classLabel, int featuresSize) {
  13. super(classLabel, featuresSize);
  14. }
  15.  
  16. public MeanObject(int classLabel, int featuresSize, double min, double max) {
  17. super(classLabel, featuresSize);
  18.  
  19. this.randomFeatures(min, max);
  20. }
  21.  
  22. public double calculate() {
  23. double changeThreshold = 0.0;
  24. this.quantizationError = 0.0;
  25.  
  26. if (!this.nearestObjects.isEmpty()) {
  27. double[] newFeatures = new double[this.features.length];
  28. Arrays.fill(newFeatures, 0);
  29.  
  30. // summarize appropriate features
  31. for (ClassificationObject nearestObject : this.nearestObjects) {
  32. for (int featNum = 0; featNum < nearestObject.features.length; featNum++) {
  33. newFeatures[featNum] += nearestObject.features[featNum];
  34. }
  35. }
  36.  
  37. // calculate mean
  38. for (int featNum = 0; featNum < newFeatures.length; featNum++) {
  39. newFeatures[featNum] /= this.nearestObjects.size();
  40. }
  41.  
  42. // calculate change
  43. changeThreshold = Classifier.distance(this.features, newFeatures);
  44.  
  45. this.features = newFeatures;
  46.  
  47. for (ClassificationObject nearestObject : this.nearestObjects) {
  48. quantizationError += Classifier.distance(nearestObject.features, this.features);
  49. }
  50.  
  51. this.nearestObjects = new ArrayList<>();
  52. }
  53.  
  54. return changeThreshold;
  55. }
  56.  
  57. private void randomFeatures(double min, double max) {
  58. Random rand = new Random();
  59. for (int i = 0; i < this.features.length; i++) {
  60. this.features[i] = rand.nextDouble() * max - min;
  61. }
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement