Github user chiwanpark commented on a diff in the pull request: https://github.com/apache/flink/pull/861#discussion_r37137811 --- Diff: flink-staging/flink-ml/src/main/scala/org/apache/flink/ml/statistics/FieldStats.scala --- @@ -0,0 +1,119 @@ +/* + * 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. + */ + +package org.apache.flink.ml.statistics + +import org.apache.flink.ml.statistics.FieldType._ + +import scala.collection.mutable + +/** Class to represent Field statistics. + * + * =Parameters= + * -[[fieldType]]: + * Type of this field, [[DISCRETE]] or [[CONTINUOUS]] + * + * For [[DISCRETE]] fields, [[entropy]], [[gini]] and [[categoryCounts]] are provided. + * For [[CONTINUOUS]] fields, [[min]], [[max]], [[mean]] and [[variance]] are provided. + * + */ +class FieldStats(val fieldType: FieldType) extends Serializable { + // field parameters + private [statistics] var _min: Double = _ + private [statistics] var _max: Double = _ + private [statistics] var _mean: Double = _ + private [statistics] var _variance: Double = _ + private [statistics] var _counts: mutable.HashMap[Double,Int] = _ + + //-------------------- Access methods ----------------------------// + def min: Double = { + exception(DISCRETE) + _min + } + + def max: Double = { + exception(DISCRETE) + _max + } + + def mean: Double = { + exception(DISCRETE) + _mean + } + + def variance: Double = { + exception(DISCRETE) + _variance + } + + def categoryCounts: mutable.HashMap[Double,Int] = { + exception(CONTINUOUS) + _counts + } + + /** + * Returns the entropy value for this [[DISCRETE]] field. + */ + def entropy: Double = { + exception(CONTINUOUS) + val total: Double = _counts.valuesIterator.sum + 3.322 * _counts.iterator.map(x => - (x._2 / total) * Math.log10(x._2 / total)).sum + } + + /** + * Returns the Gini impurity for this [[DISCRETE]] field. + */ + def gini: Double = { + exception(CONTINUOUS) + val total: Double = _counts.valuesIterator.sum + 1 - _counts.iterator.map(x => (x._2 * x._2)).sum / (total * total) + } + + //------------------ Setter methods ------------------// + private [statistics] def setContinuousParameters( + min: Double, + max: Double, + mean: Double, + variance: Double) + : Unit = { + exception(DISCRETE) + _min = min + _max = max + _mean = mean + _variance = variance + } + + private [statistics] def setDiscreteParameters(counts: mutable.HashMap[Double,Int]): Unit = { + exception(CONTINUOUS) + _counts = counts + } + + private def exception(checkFor: FieldType) = { + if(fieldType == checkFor){ + throw new RuntimeException("Invalid access of data. Check field types.") + } + } --- End diff -- Maybe If we check `FieldType` in each method (`min`, `max`, `mean`, ..., `gini`) with `Predef.assume` method, the user can understand what is the problem more properly. For example, when the user call `gini` method for continuous histogram, we can throw exception with detailed message such as "Gini impurity for continuous histogram is not supported.".
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---