Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. %% Task2.m
  2. % University of Birmingham
  3. % Machine Learning - Computer Based Test 1
  4. % Author: Andreas Farrenkopf
  5. clear all;close all;
  6.  
  7. %% Load data and extract the mens 100m data
  8. % Change directory/path for use on other computers
  9. load /Users/Andi/Studium/Birmingham/LH_Machine_Learning/MATLAB/data/olympics
  10.  
  11. % The olypmic years are stored in column 1 and the respective winning times
  12. % in column 2
  13. x = male100(:,1);
  14. t = male100(:,2);
  15.  
  16. % Rescale x (See also First Course in Machine Learning)
  17. % Further calculations and plots will be more intuitive with the rescale
  18. x = x - x(1);
  19. x = x./4;
  20.  
  21. % Plot the data points
  22. figure(1);hold on
  23. plot(x,t,'bo','markersize',10);
  24. % X axis does not show year anymore due to rescale
  25. xlabel('Number of olympic game');
  26. ylabel('Time');
  27.  
  28. %% 1st Order Model
  29. % Compute values from one year before first olympic until one year after
  30. % last olympic games
  31. % Compute value every 0.01 units
  32. % (See also First Course in Machine Learning)
  33. plotx = [x(1)-1:0.01:x(end)+1]';
  34. X = [];
  35. plotX = [];
  36. % Build up data so it has respective order
  37. for k = 0:1
  38. X = [X x.^k];
  39. plotX = [plotX plotx.^k];
  40. end
  41.  
  42. % Compute model paramteres for whole dataset
  43. w = inv(X'*X)*X'*t;
  44.  
  45. % Plot the linear model
  46. plot(plotx,plotX*w,'r','linewidth',2)
  47.  
  48. %% Cross Validation for 1st order model
  49. % The cross validation is to find the best lambda for the 1st order model
  50.  
  51. % Define parameters needed for cross validation
  52. N = length(x); %27
  53. K = 5; %K-fold CV
  54. % Define the range for lambda between 0 and 0.5
  55. lamb = [0:0.01:0.5];
  56.  
  57. % Call function cross_validation
  58. cv_loss_1 = cross_validation(lamb,K,N,X,t);
  59.  
  60. % Plot the average error over lambda
  61. figure(2);
  62. plot(lamb,mean(cv_loss_1,1),'linewidth',2)
  63. xlabel('Lambda');
  64. ylabel('Prediction Error');
  65. title('1st order model','fontsize',15)
  66.  
  67. %% 4th Order Model
  68. % For explanations on code segments and references, please see section '1st Order Model'
  69. plotx = [x(1)-1:0.01:x(end)+1]';
  70. X = [];
  71. plotX = [];
  72. for k = 0:4
  73. X = [X x.^k];
  74. plotX = [plotX plotx.^k];
  75. end
  76. w = inv(X'*X)*X'*t;
  77. figure(1);hold on
  78. plot(plotx,plotX*w,'y','linewidth',2)
  79. legend('Data Points','1st Order','4th Order')
  80.  
  81. %% Cross Validation for 4th order model
  82. % The cross validation is to find the best lambda for the 4th order model
  83. % For explanations on code segments and references, please see section '1st Order Model'
  84. N = length(x); %27
  85. K = 5;
  86. lamb = [0:0.01:0.5];
  87. cv_loss_4 = cross_validation(lamb,K,N,X,t);
  88. figure(3);
  89. plot(lamb,mean(cv_loss_4,1),'linewidth',2)
  90. xlabel('Lambda');
  91. ylabel('Prediction Error');
  92. title('4th order model','fontsize',15)
  93.  
  94. %% Function definition for cross validation
  95. % The function performs a K fold cross validation based on the input
  96. % parameters
  97. function [cv_loss] = cross_validation(lamb, K, N, X, t)
  98. sizes = repmat(floor(N/K),1,K);
  99. sizes(end) = sizes(end) + N - sum(sizes);
  100. csizes = [0 cumsum(sizes)]; % Sum of all fold sizes must equal 27 (number of data points N)
  101.  
  102. % Iterate through the range of lambda
  103. for l = 1:length(lamb)
  104. lambda = lamb(l);
  105. % Use K fold cross validation to determine lambda with the lowest
  106. % prediction error on test data
  107. for fold = 1:K
  108. % Code amended from 'First Course in Machine Learning'
  109. % Partition the data into K folds of equal size
  110. % foldX contains the data for just one fold (To validate the model)
  111. % trainX contains all other data (To train the model)
  112.  
  113. foldX = X(csizes(fold)+1:csizes(fold+1),:);
  114. trainX = X;
  115. trainX(csizes(fold)+1:csizes(fold+1),:) = [];
  116. foldt = t(csizes(fold)+1:csizes(fold+1));
  117. traint = t;
  118. traint(csizes(fold)+1:csizes(fold+1)) = [];
  119.  
  120. % Compute model parameters with formulae of regularized least
  121. % squares approach
  122. w = inv(trainX'*trainX + size(trainX,1)*lambda*eye(size(trainX,2)))*trainX'*traint;
  123.  
  124. % Compute prediction values and respective error
  125. fold_pred = foldX*w;
  126. cv_loss(fold,l) = mean((fold_pred-foldt).^2);
  127. end
  128. end
  129. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement