kpretterhofer commented on a change in pull request #1153:
URL: https://github.com/apache/systemds/pull/1153#discussion_r561928757



##########
File path: scripts/builtin/gaussianClassifier.dml
##########
@@ -0,0 +1,127 @@
+#-------------------------------------------------------------
+#
+# 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.
+#
+#-------------------------------------------------------------
+#
+# Computes the parameters needed for Gaussian Classification.
+# Thus it computes the following per class: the prior probability,
+# the inverse covariance matrix, the mean per feature and the determinant
+# of the covariance matrix. Furthermore (if not explicitely defined), it
+# adds some small smoothing value along the variances, to prevent
+# numerical errors / instabilities.
+#
+#
+# INPUT PARAMETERS:
+# 
-------------------------------------------------------------------------------------------------
+# NAME           TYPE               DEFAULT  MEANING
+# 
-------------------------------------------------------------------------------------------------
+# D              Matrix[Double]     ---      Input matrix (training set)
+# C              Matrix[Double]     ---      Target vector
+# varSmoothing   Double             1e-9     Smoothing factor for variances
+# verbose        Boolean            TRUE     Print accuracy of the training set
+# 
---------------------------------------------------------------------------------------------
+# OUTPUT:
+# 
---------------------------------------------------------------------------------------------
+# NAME                  TYPE             DEFAULT  MEANING
+# 
---------------------------------------------------------------------------------------------
+# classPriors           Matrix[Double]   ---      Vector storing the class 
prior probabilities
+# classMeans            Matrix[Double]   ---      Matrix storing the means of 
the classes
+# classInvCovariances   List[Unknown]    ---      List of inverse covariance 
matrices
+# determinants          Matrix[Double]   ---      Vector storing the 
determinants of the classes
+# 
---------------------------------------------------------------------------------------------
+#
+
+
+m_gaussianClassifier = function(Matrix[Double] D, Matrix[Double] C, Double 
varSmoothing=1e-9, Boolean verbose = TRUE)
+  return (Matrix[Double] classPriors, Matrix[Double] classMeans,
+  List[Unknown] classInvCovariances, Matrix[Double] determinants)
+{
+  #Retrieve number of samples, classes and features
+  nSamples = nrow(D)
+  nClasses = max(C)
+  nFeats = ncol(D)
+
+  #Set varSmoothing (to prevent numerical errors)
+  varSmoothing = 1e-9
+
+  #Compute means, variances and priors
+  classCounts = aggregate(target=C, groups=C, fn="count", 
ngroups=as.integer(nClasses));
+  classMeans = aggregate(target=D, groups=C, fn="mean", 
ngroups=as.integer(nClasses));
+  classVars = aggregate(target=D, groups=C, fn="variance", 
ngroups=as.integer(nClasses));
+  classPriors = classCounts / nSamples
+
+  smoothedVar = diag(matrix(1.0, rows=nFeats, cols=1)) * max(classVars) * 
varSmoothing
+
+  classInvCovariances = list()
+  determinants = matrix(0, rows=nClasses, cols=1)
+
+  #Compute determinants and inverseCovariances
+  for (class in 1:nClasses)

Review comment:
       I pushed a new commit. Unfortunately parfor in this case was not 
possible, since I append the results to a list, which can not be parallelized.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to