blob: 213f7dd6cdeea08c610e09b8fe4ca97363ed8e4c [file] [log] [blame]
#-------------------------------------------------------------
#
# 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.
#
#-------------------------------------------------------------
#THIS SCRIPT APPLIES THE ESTIMATED PARAMETERS OF MULTINOMIAL LOGISTIC REGRESSION TO A NEW (TEST) DATASET
#
# INPUT PARAMETERS:
# ---------------------------------------------------------------------------------------------
# NAME TYPE DEFAULT MEANING
# ---------------------------------------------------------------------------------------------
# X Double --- Data Matrix X
# B Double --- Regression parameters betas
# Y Double --- Response vector Y
# ---------------------------------------------------------------------------------------------
# OUTPUT: Matrix M of predicted means/probabilities, some statistics in CSV format (see below)
# OUTPUT:
# ---------------------------------------------------------------------------------------------
# NAME TYPE DEFAULT MEANING
# ---------------------------------------------------------------------------------------------
# M Double --- Matrix M of predicted means/probabilities
# predicted_Y Double --- Predicted response vector
# accuracy Double --- scalar value of accuracy
# ---------------------------------------------------------------------------------------------
m_multiLogRegPredict = function(Matrix[Double] X, Matrix[Double] B, Matrix[Double] Y, Boolean verbose = FALSE)
return(Matrix[Double] M, Matrix[Double] predicted_Y, Double accuracy)
{
if(min(Y) <= 0)
stop("class labels should be greater than zero")
num_records = nrow(X);
num_features = ncol(X);
beta = B[1:ncol(X), ];
intercept = B[nrow(B), ];
if (nrow(B) == ncol(X))
intercept = 0.0 * intercept;
else
num_features = num_features + 1;
ones_rec = matrix(1, rows = num_records, cols = 1);
linear_terms = X %*% beta + ones_rec %*% intercept;
M = probabilities(linear_terms); # compute the probablitites on unknown data
predicted_Y = rowIndexMax(M); # extract the class labels
if(nrow(Y) != 0)
accuracy = sum((predicted_Y - Y) == 0) / num_records * 100;
if(verbose)
print("Accuracy (%): " + accuracy);
}
probabilities = function (Matrix[double] linear_terms)
return (Matrix[double] means) {
# PROBABLITIES FOR MULTINOMIAL LOGIT DISTRIBUTION
num_points = nrow (linear_terms);
elt = exp (linear_terms);
ones_pts = matrix (1, rows = num_points, cols = 1);
elt = cbind (elt, ones_pts);
ones_ctg = matrix (1, rows = ncol (elt), cols = 1);
means = elt / (rowSums (elt) %*% t(ones_ctg));
}