| #------------------------------------------------------------- |
| # |
| # Licensed to the Apache Software Foundation (ASF) under one |
| # or more contributor license agreements. See the NOTICE file |
| # distributed with this work for additional information |
| # regarding copyright ownership. The ASF licenses this file |
| # to you under the Apache License, Version 2.0 (the |
| # "License"); you may not use this file except in compliance |
| # with the License. You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, |
| # software distributed under the License is distributed on an |
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| # KIND, either express or implied. See the License for the |
| # specific language governing permissions and limitations |
| # under the License. |
| # |
| #------------------------------------------------------------- |
| |
| l2norm = function(Matrix[Double] X, Matrix[Double] y, Matrix[Double] B) return (Matrix[Double] loss) { |
| loss = as.matrix(sum((y - X%*%B)^2)); |
| } |
| |
| X = read($1); |
| y = read($2); |
| verbose = $4; |
| |
| N = 200; |
| Xtrain = X[1:N,]; |
| ytrain = y[1:N,]; |
| Xtest = X[(N+1):nrow(X),]; |
| ytest = y[(N+1):nrow(X),]; |
| |
| args = list(X=X, y=y, icpt=0, reg=-1, tol=-1, maxi=-1, verbose=FALSE); |
| params = list("reg", "tol", "maxi", "verbose"); |
| paramRanges = list(10^seq(0,-4), 10^seq(-6,-12), 10^seq(1,3), as.matrix(as.double(verbose))); |
| [B1, opt] = gridSearch(X=Xtrain, y=ytrain, train="lm", predict="l2norm", |
| numB=ncol(X), params=params, paramValues=paramRanges, trainArgs=args); |
| B2 = lm(X=Xtrain, y=ytrain, verbose=FALSE); |
| |
| l1 = l2norm(Xtest, ytest, B1); |
| l2 = l2norm(Xtest, ytest, B2); |
| R = as.scalar(l1 < l2); |
| |
| write(R, $3) |