Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. //
  2. // TrainAndTest.c
  3. // MLCoursework
  4. //
  5. // This is a fairly inefficient implementation that does not use any dynamic memory allocation
  6. // because that would not be safe on the DEWIS marking system
  7. //
  8. // Created by Jim Smith on 06/02/2017.
  9. // Copyright © 2017 Jim SmithJim Smith. All rights reserved.
  10. //
  11.  
  12.  
  13. #include "TrainAndTest.h"
  14. #include <math.h>
  15. #include <stdlib.h>
  16.  
  17. double calculateDistance(double *sample1, double *sample2);
  18.  
  19. /*int main(int argc, char **argv)
  20. {
  21. extern int Xmain(int argc, char **argv);
  22. Xmain(argc,argv);
  23. }*/
  24.  
  25. //declare this array as static but make it available to any function in this file
  26. //in case we want to store the training examples and use them later
  27. static double myModel[NUM_TRAINING_SAMPLES][NUM_FEATURES];
  28. //even if each item in the training set is from a different class we know how many there are
  29. static char myModelLabels[NUM_TRAINING_SAMPLES];
  30.  
  31. static int labelUsed[256];
  32.  
  33. static int trainingSetSize = 0;
  34.  
  35. int train(double **trainingSamples, char *trainingLabels, int numSamples, int numFeatures) {
  36. int returnval = 1;
  37. int sample, feature;
  38. char thisLabel;
  39. int ithisLabel;
  40.  
  41.  
  42. //clean the model because C leaves whatever is in the memory
  43. for (sample = 0; sample < NUM_TRAINING_SAMPLES; sample++) {
  44. for (feature = 0; feature < NUM_FEATURES; feature++) {
  45. myModel[sample][feature] = 0.0;
  46. }
  47. }
  48. for (sample = 0; sample < 256; sample++) {
  49. labelUsed[sample] = 0;
  50. }
  51.  
  52. //sanity checking
  53. if (numFeatures > NUM_FEATURES || numSamples > NUM_TRAINING_SAMPLES) {
  54. fprintf(stdout, "error: called train with data set larger than spaced allocated to store it");
  55. returnval = 0;
  56. }
  57.  
  58. //this is a silly trivial train()_ function
  59. fprintf(stdout,"no ML algorithm implemented yet\n");
  60.  
  61.  
  62. //make a simple copy of the data we are being passed but don't do anything with it
  63. //I'm just giving you this for the sake of people less familiar with pointers etc.
  64.  
  65.  
  66. if (returnval == 1) {
  67. //store the labels and the feature values
  68. trainingSetSize = numSamples;
  69. int index, feature;
  70. for (index = 0; index < numSamples; index++) {
  71. myModelLabels[index] = trainingLabels[index];
  72. for (feature = 0; feature < 4; feature++) {
  73. myModel[index][feature] = trainingSamples[index][feature];
  74. }
  75.  
  76. thisLabel = trainingLabels[index];
  77. ithisLabel = (int) thisLabel;
  78. labelUsed[ithisLabel]++;
  79.  
  80.  
  81. }
  82. fprintf(stdout, "data stored locally \n");
  83. }//end else
  84.  
  85.  
  86. //now you could do whatever you like with the data
  87. //for example, you could populate some rules etc.
  88. //you were given pseudocode in semester 1 to do this
  89. // you could also normalise the data to remove scaling effects if you want to use something like a MLP or kNN
  90. //just remember that anything that you want to access in your predictLabel() function
  91. //needs to be declared static at the top of this file - as I have done for the "myModel" and myModelLabels data .
  92.  
  93.  
  94.  
  95. return returnval;
  96. }
  97.  
  98.  
  99. double calculateDistance(double *sample1, double *sample2) {
  100. double distance = 0;
  101. double difference = 0;
  102.  
  103. for (int i = 0; i < NUM_FEATURES; i++) {
  104. difference = sample1[i] - sample2[i];
  105. distance = sqrt(distance + (difference * difference));
  106. }
  107.  
  108. return distance;
  109. }
  110.  
  111.  
  112. char predictLabel(double *sample, int numFeatures) {
  113.  
  114. char prediction = (char) NULL;
  115. double distances[NUM_TRAINING_SAMPLES];
  116. int choice, validChoice = 0;
  117. int nearest;
  118. int i;
  119.  
  120. for (i = 0; i < NUM_TRAINING_SAMPLES; i++) {
  121. distances[i] = calculateDistance(sample, myModel[i]);
  122. }
  123. nearest = 0;
  124.  
  125. for (i = 0; i < NUM_TRAINING_SAMPLES - 1; i++);
  126. {
  127.  
  128. if (distances[i] < distances[nearest]) {
  129. nearest = i;
  130. }
  131. }
  132.  
  133. prediction = myModelLabels[nearest];
  134.  
  135. return prediction;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement