Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # CROSS VALIDATION FOR CHOOSING SIGMA AND LAMBDA
- using Random
- function CV(X, n)
- # Random permutation for shuffle
- rng = MersenneTwister(1234);
- rndInd = randperm(n)
- mid = floor(Int, n/2)
- # Random shuffle, using the same permutation to conserve correct labels
- Xshuffle, yshuffle = X[rndInd], y[rndInd]
- # Dividing into train and validation set
- Xtrain, ytrain = Xshuffle[1:mid,:], yshuffle[1:mid]
- Xvali, yvali = Xshuffle[mid+1:end,:], yshuffle[mid+1:end]
- # Iterating through different combinations of sigma and lambda
- bestError = Inf
- bestLambda = 0
- bestSigma = 0
- for lambda in 1:100
- for sigma in 1:100
- model = leastSquaresRBFL2(Xtrain,ytrain,lambda,sigma)
- t = size(Xvali,1)
- yhat = model.predict(Xvali)
- testError = sum((yhat - yvali).^2)/t
- if testError < bestError
- bestError = testError
- bestLambda = lambda
- bestSigma = sigma
- end
- end
- end
- return (bestError, bestSigma, bestLambda)
- end
- show(CV(X,n))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement