[GitHub] incubator-hivemall pull request #107: [HIVEMALL-132] Generalize f1score UDAF...
Github user myui commented on a diff in the pull request: https://github.com/apache/incubator-hivemall/pull/107#discussion_r135693139 --- Diff: core/src/main/java/hivemall/evaluation/FMeasureUDAF.java --- @@ -18,118 +18,387 @@ */ package hivemall.evaluation; -import hivemall.utils.hadoop.WritableUtils; +import hivemall.UDAFEvaluatorWithOptions; +import hivemall.utils.hadoop.HiveUtils; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; +import hivemall.utils.lang.Primitives; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; + import org.apache.hadoop.hive.ql.exec.Description; -import org.apache.hadoop.hive.ql.exec.UDAF; -import org.apache.hadoop.hive.ql.exec.UDAFEvaluator; +import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException; +import org.apache.hadoop.hive.ql.exec.UDFArgumentException; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.parse.SemanticException; +import org.apache.hadoop.hive.ql.udf.generic.AbstractGenericUDAFResolver; +import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator; import org.apache.hadoop.hive.serde2.io.DoubleWritable; -import org.apache.hadoop.io.IntWritable; - -@SuppressWarnings("deprecation") -@Description(name = "f1score", -value = "_FUNC_(array[int], array[int]) - Return a F-measure/F1 score") -public final class FMeasureUDAF extends UDAF { - -public static class Evaluator implements UDAFEvaluator { - -public static class PartialResult { -long tp; -/** tp + fn */ -long totalAcutal; -/** tp + fp */ -long totalPredicted; - -PartialResult() { -this.tp = 0L; -this.totalPredicted = 0L; -this.totalAcutal = 0L; -} +import org.apache.hadoop.hive.serde2.objectinspector.*; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector; +import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo; +import org.apache.hadoop.io.LongWritable; -void updateScore(final List actual, final List predicted) { -final int numActual = actual.size(); -final int numPredicted = predicted.size(); -int countTp = 0; -for (int i = 0; i < numPredicted; i++) { -IntWritable p = predicted.get(i); -if (actual.contains(p)) { -countTp++; -} +import javax.annotation.Nonnull; + +@Description( +name = "fmeasure", +value = "_FUNC_(array | int | boolean actual , array | int | boolean predicted, String) - Return a F-measure (f1score is the special with beta=1.)") +public final class FMeasureUDAF extends AbstractGenericUDAFResolver { +@Override +public GenericUDAFEvaluator getEvaluator(@Nonnull TypeInfo[] typeInfo) throws SemanticException { +if (typeInfo.length != 2 && typeInfo.length != 3) { +throw new UDFArgumentTypeException(typeInfo.length - 1, +"_FUNC_ takes two or three arguments"); +} + +boolean isArg1ListOrIntOrBoolean = HiveUtils.isListTypeInfo(typeInfo[0]) +|| HiveUtils.isIntegerTypeInfo(typeInfo[0]) +|| HiveUtils.isBooleanTypeInfo(typeInfo[0]); +if (!isArg1ListOrIntOrBoolean) { +throw new UDFArgumentTypeException(0, +"The first argument `array/int/boolean actual` is invalid form: " + typeInfo[0]); +} + +boolean isArg2ListOrIntOrBoolean = HiveUtils.isListTypeInfo(typeInfo[1]) +|| HiveUtils.isIntegerTypeInfo(typeInfo[1]) +|| HiveUtils.isBooleanTypeInfo(typeInfo[1]); +if (!isArg2ListOrIntOrBoolean) { +throw new UDFArgumentTypeException(1, +"The second argument `array/int/boolean predicted` is invalid form: " + typeInfo[1]); +} + +if (typeInfo[0] != typeInfo[1]) { +throw new UDFArgumentTypeException(1, "The first argument `actual`'s type is " ++ typeInfo[0] + ", but the second argument `predicted`'s type is not match: " ++ typeInfo[1]); +} + +return new Evaluator(); +} + +public static class Evaluator
[GitHub] incubator-hivemall issue #107: [HIVEMALL-132] Generalize f1score UDAF to sup...
Github user nzw0301 commented on the issue: https://github.com/apache/incubator-hivemall/pull/107 But I found another issue for f1score and fmeasure. Both function cannot work on `EMR v5.8.0` ```sql hive> select f1score(array(1), array(1)); FAILED: IllegalArgumentException Size requested for unknown type: org.apache.hadoop.hive.ql.exec.UDAFEvaluator ``` ```sql hive> select fmeasure(array(1), array(1)); FAILED: IllegalArgumentException Size requested for unknown type: java.lang.String ``` However, they can work on `EMR v5.0.0`. I don't know why the failures occur on newer EMR. --- 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. ---
[GitHub] incubator-hivemall issue #107: [HIVEMALL-132] Generalize f1score UDAF to sup...
Github user nzw0301 commented on the issue: https://github.com/apache/incubator-hivemall/pull/107 I tested @takuti's query. The buggy code (previous code) returns `1.0`. On the other hand, the fixed code return correct value`0.5`. --- 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. ---
[GitHub] incubator-hivemall issue #110: [HIVEMALL-142] Implement SingularizeUDF
Github user coveralls commented on the issue: https://github.com/apache/incubator-hivemall/pull/110 [![Coverage Status](https://coveralls.io/builds/13016205/badge)](https://coveralls.io/builds/13016205) Coverage increased (+0.5%) to 41.301% when pulling **e230581d26c2a794ed0b6ccbb26c90122eb7dccd on takuti:singularize** into **7205de1e959f0d9b96ac756e415d8a8ada7e92af on apache:master**. --- 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. ---
[GitHub] incubator-hivemall pull request #107: [HIVEMALL-132] Generalize f1score UDAF...
Github user myui commented on a diff in the pull request: https://github.com/apache/incubator-hivemall/pull/107#discussion_r135514455 --- Diff: core/src/main/java/hivemall/evaluation/F1ScoreUDAF.java --- @@ -0,0 +1,134 @@ +/* + * 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 hivemall.evaluation; + +import hivemall.utils.hadoop.WritableUtils; + +import java.util.List; + +import org.apache.hadoop.hive.ql.exec.Description; +import org.apache.hadoop.hive.ql.exec.UDAF; +import org.apache.hadoop.hive.ql.exec.UDAFEvaluator; +import org.apache.hadoop.hive.serde2.io.DoubleWritable; +import org.apache.hadoop.io.IntWritable; + +@SuppressWarnings("deprecation") +@Description(name = "f1score", value = "_FUNC_(array[int], array[int]) - Return a F1 score") +public final class F1ScoreUDAF extends UDAF { + +public static class Evaluator implements UDAFEvaluator { + +public static class PartialResult { +long tp; +/** tp + fn */ +long totalActual; +/** tp + fp */ +long totalPredicted; + +PartialResult() { +this.tp = 0L; +this.totalPredicted = 0L; +this.totalActual = 0L; +} + +void updateScore(final List actual, final List predicted) { +final int numActual = actual.size(); +final int numPredicted = predicted.size(); +int countTp = 0; +for (int i = 0; i < numPredicted; i++) { +IntWritable p = predicted.get(i); +if (actual.contains(p)) { +countTp++; +} +} +this.tp += countTp; +this.totalActual += numActual; +this.totalPredicted += numPredicted; +} + +void merge(PartialResult other) { --- End diff -- oops. This should be fixed. --- 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. ---
[GitHub] incubator-hivemall issue #107: [HIVEMALL-132] Generalize f1score UDAF to sup...
Github user takuti commented on the issue: https://github.com/apache/incubator-hivemall/pull/107 Importantly, in case that the number of mappers is 1, fixing the bug in `merge()` does not change the output value; you might see the same result `0.42483920860540153` even if the bug exists. Alternatively, let you test the following query (6 mappers will be shown for each `select` statement), and check if its output is same as `fmeasure(truth, predicted, '-average micro')`: ```sql WITH data as ( select 1 as truth, 0 as predicted union all select 0 as truth, 1 as predicted union all select 0 as truth, 0 as predicted union all select 1 as truth, 1 as predicted union all select 0 as truth, 1 as predicted union all select 0 as truth, 0 as predicted ) select f1score(array(truth), array(predicted)) from data ; ``` If the bug has been fixed correctly, output should be same as `fmeasure(truth, predicted, '-average micro) = 0.5`, while the buggy code returns `f1score(array(truth), array(predicted)) = 1.0` --- 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. ---
[GitHub] incubator-hivemall issue #107: [HIVEMALL-132] Generalize f1score UDAF to sup...
Github user nzw0301 commented on the issue: https://github.com/apache/incubator-hivemall/pull/107 I will check whether the return value is the same tomorrow. --- 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. ---
[GitHub] incubator-hivemall issue #105: [WIP][HIVEMALL-24] Scalable field-aware facto...
Github user coveralls commented on the issue: https://github.com/apache/incubator-hivemall/pull/105 [![Coverage Status](https://coveralls.io/builds/13013516/badge)](https://coveralls.io/builds/13013516) Coverage decreased (-0.6%) to 40.215% when pulling **dba8bf64e3a29ba4da373bfbec01a37439a2cdea on myui:HIVEMALL-24-2** into **7205de1e959f0d9b96ac756e415d8a8ada7e92af on apache:master**. --- 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. ---
[GitHub] incubator-hivemall issue #107: [HIVEMALL-132] Generalize f1score UDAF to sup...
Github user nzw0301 commented on the issue: https://github.com/apache/incubator-hivemall/pull/107 @takuti Thank you for comments! I run query above, I get the same result on movielens-1M. --- 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. ---
[GitHub] incubator-hivemall pull request #107: [HIVEMALL-132] Generalize f1score UDAF...
Github user nzw0301 commented on a diff in the pull request: https://github.com/apache/incubator-hivemall/pull/107#discussion_r135471771 --- Diff: docs/gitbook/eval/binary_classification_measures.md --- @@ -0,0 +1,261 @@ + + + + +# Binary problems + +Binary classification problem is the task to predict the label of each data given two categorized dataset. + +Hivemall provides some tutorials to deal with binary classification problems as follows: + +- [Online advertisement click prediction](../binaryclass/general.html) +- [News classification](../binaryclass/news20_dataset.html) + +This page focuses on the evaluation of the results from such binary classification problems. +If your classifier outputs probability rather than 0/1 label, evaluation based on [Area Under the ROC Curve](./auc.md) would be more appropriate. + + +# Example + +For the metrics explanation, this page introduces toy example data and two metrics. + +## Data + +The following table shows the sample of binary classification's prediction. +In this case, `1` means positive label and `0` means negative label. +Left column includes supervised label data, +and center column includes predicted label by a binary classifier. + +| truth label| predicted label | | +|:---:|:---:|:---:| +| 1 | 0 |False Negative| +| 0 | 1 |False Positive| +| 0 | 0 |True Negative| +| 1 | 1 |True Positive| +| 0 | 1 |False Positive| +| 0 | 0 |True Negative| + +## Preliminary metrics + +Some evaluation metrics are calculated based on 4 values: + +- True Positive (TP): truth label is positive and predicted label is also positive +- True Negative (TN): truth label is negative and predicted label is also negative +- False Positive (FP): truth label is negative but predicted label is positive +- False Negative (FN): truth label is positive but predicted label is negative + +`TR` and `TN` represent correct classification, and `FP` and `FN` illustrate incorrect ones. + +In this example, we can obtain those values: + +- TP: 1 +- TN: 2 +- FP: 2 +- FN: 1 + +if you want to know about those metrics, Wikipedia provides [more detail information](https://en.wikipedia.org/wiki/Sensitivity_and_specificity). + +### Recall + +Recall indicates the true positive rate in truth positive labels. +The value is computed by the following equation: + +$$ +\mathrm{recall} = \frac{\mathrm{\#TP}}{\mathrm{\#TP} + \mathrm{\#FN}} +$$ + +In the previous example, $$\mathrm{precision} = \frac{1}{2}$$. + +### Precision + +Precision indicates the true positive rate in positive predictive labels. +The value is computed by the following equation: + +$$ +\mathrm{precision} = \frac{\mathrm{\#TP}}{\mathrm{\#TP} + \mathrm{\#FP}} +$$ + +In the previous example, $$\mathrm{precision} = \frac{1}{3}$$. + +# Metrics + +## F1-score + +F1-score is the harmonic mean of recall and precision. +F1-score is computed by the following equation: + +$$ +\mathrm{F}_1 = 2 \frac{\mathrm{precision} * \mathrm{recall}}{\mathrm{precision} + \mathrm{recall}} +$$ + +Hivemall's `fmeasure` function provides the option which can switch `micro`(default) or `binary` by passing `average` argument. + + +> Caution +> Hivemall also provides `f1score` function, but it is old function to obtain F1-score. The value of `f1score` is based on set operation. So, we recommend to use `fmeasure` function to get F1-score based on this article. + +You can learn more about this from the following external resource: + +- [scikit-learn's F1-score](http://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html) + + +### Micro average + +If `micro` is passed to `average`, +recall and precision are modified to consider True Negative. +So, micro f1score are calculated by those modified recall and precision. + +$$ +\mathrm{recall} = \frac{\mathrm{\#TP} + \mathrm{\#TN}}{\mathrm{\#TP} + \mathrm{\#FN} + \mathrm{\#TN}} +$$ + +$$ +\mathrm{precision} = \frac{\mathrm{\#TP} + \mathrm{\#TN}}{\mathrm{\#TP} + \mathrm{\#FP} + \mathrm{\#TN}} +$$ + +If `average` argument is omitted, `fmeasure` use default value: `'-average micro'`. + +The following query shows the example to obtain F1-score. +Each row value has the same type (`int` or `boolean`). +If row value's type is `int`, `1` is considered as the positive label, and `-1` or `0` is considered as the negative label. + + +```sql +WITH data as ( + select 1 as truth, 0 as predicted +union all + select 0 as truth, 1 as predicted +union all + select 0 as truth, 0 as pre
[GitHub] incubator-hivemall pull request #107: [HIVEMALL-132] Generalize f1score UDAF...
Github user nzw0301 commented on a diff in the pull request: https://github.com/apache/incubator-hivemall/pull/107#discussion_r135464601 --- Diff: core/src/test/java/hivemall/evaluation/FMeasureUDAFTest.java --- @@ -0,0 +1,393 @@ +/* + * 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 hivemall.evaluation; + +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator; +import org.apache.hadoop.hive.ql.udf.generic.SimpleGenericUDAFParameterInfo; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils; +import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + + +public class FMeasureUDAFTest { +FMeasureUDAF fmeasure; +GenericUDAFEvaluator evaluator; +ObjectInspector[] inputOIs; +FMeasureUDAF.FMeasureAggregationBuffer agg; + +@Before +public void setUp() throws Exception { +fmeasure = new FMeasureUDAF(); +inputOIs = new ObjectInspector[] { + ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableLongObjectInspector), + ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableLongObjectInspector), +ObjectInspectorUtils.getConstantObjectInspector( + PrimitiveObjectInspectorFactory.javaStringObjectInspector, "-beta 1.")}; + +evaluator = fmeasure.getEvaluator(new SimpleGenericUDAFParameterInfo(inputOIs, false, false)); + +agg = (FMeasureUDAF.FMeasureAggregationBuffer) evaluator.getNewAggregationBuffer(); +} + +private void setUpWithArguments(double beta, String average) throws Exception { +fmeasure = new FMeasureUDAF(); +inputOIs = new ObjectInspector[] { + ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableLongObjectInspector), + ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableLongObjectInspector), +ObjectInspectorUtils.getConstantObjectInspector( + PrimitiveObjectInspectorFactory.javaStringObjectInspector, "-beta " + beta ++ " -average " + average)}; + +evaluator = fmeasure.getEvaluator(new SimpleGenericUDAFParameterInfo(inputOIs, false, false)); +agg = (FMeasureUDAF.FMeasureAggregationBuffer) evaluator.getNewAggregationBuffer(); +} + +private void binarySetUp(Object actual, Object predicted, double beta, String average) +throws Exception { +fmeasure = new FMeasureUDAF(); +inputOIs = new ObjectInspector[3]; + +String actualClassName = actual.getClass().getName(); +if (actualClassName.equals("java.lang.Integer")) { +inputOIs[0] = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.INT); +} else if (actualClassName.equals("java.lang.Boolean")) { +inputOIs[0] = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.BOOLEAN); +} else if ((actualClassName.equals("java.lang.String"))) { +inputOIs[0] = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.STRING); +} + +String predicatedClassName = predicted.getClass().getName();
[GitHub] incubator-hivemall pull request #110: [HIVEMALL-142] Implement SingularizeUD...
Github user takuti commented on a diff in the pull request: https://github.com/apache/incubator-hivemall/pull/110#discussion_r135462565 --- Diff: core/src/main/java/hivemall/utils/lang/StringUtils.java --- @@ -172,12 +172,17 @@ public static void clear(@Nonnull final StringBuilder buf) { public static String concat(@Nonnull final List list, @Nonnull final String sep) { --- End diff -- done --- 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. ---
[GitHub] incubator-hivemall pull request #110: [HIVEMALL-142] Implement SingularizeUD...
Github user takuti commented on a diff in the pull request: https://github.com/apache/incubator-hivemall/pull/110#discussion_r135459734 --- Diff: core/src/main/java/hivemall/utils/lang/StringUtils.java --- @@ -172,12 +172,17 @@ public static void clear(@Nonnull final StringBuilder buf) { public static String concat(@Nonnull final List list, @Nonnull final String sep) { --- End diff -- okay, so I keep the update here --- 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. ---
[GitHub] incubator-hivemall pull request #110: [HIVEMALL-142] Implement SingularizeUD...
Github user myui commented on a diff in the pull request: https://github.com/apache/incubator-hivemall/pull/110#discussion_r135458769 --- Diff: core/src/main/java/hivemall/utils/lang/StringUtils.java --- @@ -172,12 +172,17 @@ public static void clear(@Nonnull final StringBuilder buf) { public static String concat(@Nonnull final List list, @Nonnull final String sep) { --- End diff -- former is expected. --- 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. ---
[GitHub] incubator-hivemall pull request #111: [WIP][HIVEMALL-17] Support SLIM
GitHub user nzw0301 opened a pull request: https://github.com/apache/incubator-hivemall/pull/111 [WIP][HIVEMALL-17] Support SLIM ## What changes were proposed in this pull request? Add new UDTF: `train_slim` ## What type of PR is it? Improvement ## What is the Jira issue? https://issues.apache.org/jira/browse/HIVEMALL-17 ## How was this patch tested? - Add `recommend/SlimUDTFTest` - manual tests on EMR ## How to use this feature? - Please see markdown file: `movielens_slim.md` in this PR. ## Checklist - [x] Did you apply source code formatter, i.e., `mvn formatter:format`, for your commit? You can merge this pull request into a Git repository by running: $ git pull https://github.com/nzw0301/incubator-hivemall HIVEMALL-17 Alternatively you can review and apply these changes as the patch at: https://github.com/apache/incubator-hivemall/pull/111.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #111 commit a9b89656ef4b54cb938e59e3bb546a2aa33a9fd3 Author: Kento NOZAWA Date: 2017-08-18T03:30:14Z Init Slim UDTF commit 5728499405400d68faec2be58cd11e788ce8c607 Author: Kento NOZAWA Date: 2017-08-25T07:15:18Z Move comment commit 7ac96304f06b41c0a72c1be885cd66d75acfd966 Author: Kento NOZAWA Date: 2017-08-25T09:58:27Z Init slim document commit 23672cf5ddeceb06cad10e9a0496d860d786d601 Author: Kento NOZAWA Date: 2017-08-28T07:22:39Z Update slim - Update docs - Remove degub print in test - Rename function for unification: `slim_train` to `train_slim` --- 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. ---
[GitHub] incubator-hivemall pull request #107: [HIVEMALL-132] Generalize f1score UDAF...
Github user takuti commented on a diff in the pull request: https://github.com/apache/incubator-hivemall/pull/107#discussion_r135453504 --- Diff: core/src/main/java/hivemall/evaluation/F1ScoreUDAF.java --- @@ -0,0 +1,134 @@ +/* + * 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 hivemall.evaluation; + +import hivemall.utils.hadoop.WritableUtils; + +import java.util.List; + +import org.apache.hadoop.hive.ql.exec.Description; +import org.apache.hadoop.hive.ql.exec.UDAF; +import org.apache.hadoop.hive.ql.exec.UDAFEvaluator; +import org.apache.hadoop.hive.serde2.io.DoubleWritable; +import org.apache.hadoop.io.IntWritable; + +@SuppressWarnings("deprecation") +@Description(name = "f1score", value = "_FUNC_(array[int], array[int]) - Return a F1 score") +public final class F1ScoreUDAF extends UDAF { + +public static class Evaluator implements UDAFEvaluator { + +public static class PartialResult { +long tp; +/** tp + fn */ +long totalActual; +/** tp + fp */ +long totalPredicted; + +PartialResult() { +this.tp = 0L; +this.totalPredicted = 0L; +this.totalActual = 0L; +} + +void updateScore(final List actual, final List predicted) { +final int numActual = actual.size(); +final int numPredicted = predicted.size(); +int countTp = 0; +for (int i = 0; i < numPredicted; i++) { +IntWritable p = predicted.get(i); +if (actual.contains(p)) { +countTp++; +} +} +this.tp += countTp; +this.totalActual += numActual; +this.totalPredicted += numPredicted; +} + +void merge(PartialResult other) { --- End diff -- Oh, there is a bug here! ð Correct: ```java void merge(PartialResult other) { this.tp += other.tp; this.totalActual += other.totalActual; this.totalPredicted += other.totalPredicted; } ``` --- 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. ---
[GitHub] incubator-hivemall pull request #107: [HIVEMALL-132] Generalize f1score UDAF...
Github user takuti commented on a diff in the pull request: https://github.com/apache/incubator-hivemall/pull/107#discussion_r135447663 --- Diff: core/src/main/java/hivemall/evaluation/FMeasureUDAF.java --- @@ -18,118 +18,387 @@ */ package hivemall.evaluation; -import hivemall.utils.hadoop.WritableUtils; +import hivemall.UDAFEvaluatorWithOptions; +import hivemall.utils.hadoop.HiveUtils; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; +import hivemall.utils.lang.Primitives; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; + import org.apache.hadoop.hive.ql.exec.Description; -import org.apache.hadoop.hive.ql.exec.UDAF; -import org.apache.hadoop.hive.ql.exec.UDAFEvaluator; +import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException; +import org.apache.hadoop.hive.ql.exec.UDFArgumentException; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.parse.SemanticException; +import org.apache.hadoop.hive.ql.udf.generic.AbstractGenericUDAFResolver; +import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator; import org.apache.hadoop.hive.serde2.io.DoubleWritable; -import org.apache.hadoop.io.IntWritable; - -@SuppressWarnings("deprecation") -@Description(name = "f1score", -value = "_FUNC_(array[int], array[int]) - Return a F-measure/F1 score") -public final class FMeasureUDAF extends UDAF { - -public static class Evaluator implements UDAFEvaluator { - -public static class PartialResult { -long tp; -/** tp + fn */ -long totalAcutal; -/** tp + fp */ -long totalPredicted; - -PartialResult() { -this.tp = 0L; -this.totalPredicted = 0L; -this.totalAcutal = 0L; -} +import org.apache.hadoop.hive.serde2.objectinspector.*; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector; +import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo; +import org.apache.hadoop.io.LongWritable; -void updateScore(final List actual, final List predicted) { -final int numActual = actual.size(); -final int numPredicted = predicted.size(); -int countTp = 0; -for (int i = 0; i < numPredicted; i++) { -IntWritable p = predicted.get(i); -if (actual.contains(p)) { -countTp++; -} +import javax.annotation.Nonnull; + +@Description( +name = "fmeasure", +value = "_FUNC_(array | int | boolean actual , array | int | boolean predicted, String) - Return a F-measure (f1score is the special with beta=1.)") +public final class FMeasureUDAF extends AbstractGenericUDAFResolver { +@Override +public GenericUDAFEvaluator getEvaluator(@Nonnull TypeInfo[] typeInfo) throws SemanticException { +if (typeInfo.length != 2 && typeInfo.length != 3) { +throw new UDFArgumentTypeException(typeInfo.length - 1, +"_FUNC_ takes two or three arguments"); +} + +boolean isArg1ListOrIntOrBoolean = HiveUtils.isListTypeInfo(typeInfo[0]) +|| HiveUtils.isIntegerTypeInfo(typeInfo[0]) +|| HiveUtils.isBooleanTypeInfo(typeInfo[0]); +if (!isArg1ListOrIntOrBoolean) { +throw new UDFArgumentTypeException(0, +"The first argument `array/int/boolean actual` is invalid form: " + typeInfo[0]); +} + +boolean isArg2ListOrIntOrBoolean = HiveUtils.isListTypeInfo(typeInfo[1]) +|| HiveUtils.isIntegerTypeInfo(typeInfo[1]) +|| HiveUtils.isBooleanTypeInfo(typeInfo[1]); +if (!isArg2ListOrIntOrBoolean) { +throw new UDFArgumentTypeException(1, +"The second argument `array/int/boolean actual` is invalid form: " + typeInfo[1]); +} + +if (typeInfo[0] != typeInfo[1]) { +throw new UDFArgumentTypeException(1, "The first argument's `actual` type is " ++ typeInfo[0] + ", but the second argument `predicated`'s type is not match: " --- End diff -- Typo: `predicated` => `predicted` --- If your project is set up for it, you can reply to this email and have your reply appe
[GitHub] incubator-hivemall pull request #107: [HIVEMALL-132] Generalize f1score UDAF...
Github user takuti commented on a diff in the pull request: https://github.com/apache/incubator-hivemall/pull/107#discussion_r135450159 --- Diff: core/src/test/java/hivemall/evaluation/FMeasureUDAFTest.java --- @@ -0,0 +1,393 @@ +/* + * 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 hivemall.evaluation; + +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator; +import org.apache.hadoop.hive.ql.udf.generic.SimpleGenericUDAFParameterInfo; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils; +import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + + +public class FMeasureUDAFTest { +FMeasureUDAF fmeasure; +GenericUDAFEvaluator evaluator; +ObjectInspector[] inputOIs; +FMeasureUDAF.FMeasureAggregationBuffer agg; + +@Before +public void setUp() throws Exception { +fmeasure = new FMeasureUDAF(); +inputOIs = new ObjectInspector[] { + ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableLongObjectInspector), + ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableLongObjectInspector), +ObjectInspectorUtils.getConstantObjectInspector( + PrimitiveObjectInspectorFactory.javaStringObjectInspector, "-beta 1.")}; + +evaluator = fmeasure.getEvaluator(new SimpleGenericUDAFParameterInfo(inputOIs, false, false)); + +agg = (FMeasureUDAF.FMeasureAggregationBuffer) evaluator.getNewAggregationBuffer(); +} + +private void setUpWithArguments(double beta, String average) throws Exception { +fmeasure = new FMeasureUDAF(); +inputOIs = new ObjectInspector[] { + ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableLongObjectInspector), + ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableLongObjectInspector), +ObjectInspectorUtils.getConstantObjectInspector( + PrimitiveObjectInspectorFactory.javaStringObjectInspector, "-beta " + beta ++ " -average " + average)}; + +evaluator = fmeasure.getEvaluator(new SimpleGenericUDAFParameterInfo(inputOIs, false, false)); +agg = (FMeasureUDAF.FMeasureAggregationBuffer) evaluator.getNewAggregationBuffer(); +} + +private void binarySetUp(Object actual, Object predicted, double beta, String average) +throws Exception { +fmeasure = new FMeasureUDAF(); +inputOIs = new ObjectInspector[3]; + +String actualClassName = actual.getClass().getName(); +if (actualClassName.equals("java.lang.Integer")) { +inputOIs[0] = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.INT); +} else if (actualClassName.equals("java.lang.Boolean")) { +inputOIs[0] = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.BOOLEAN); +} else if ((actualClassName.equals("java.lang.String"))) { +inputOIs[0] = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.STRING); +} + +String predicatedClassName = predicted.getClass().getName();
[GitHub] incubator-hivemall pull request #107: [HIVEMALL-132] Generalize f1score UDAF...
Github user takuti commented on a diff in the pull request: https://github.com/apache/incubator-hivemall/pull/107#discussion_r135450892 --- Diff: docs/gitbook/eval/binary_classification_measures.md --- @@ -0,0 +1,261 @@ + + + + +# Binary problems + +Binary classification problem is the task to predict the label of each data given two categorized dataset. + +Hivemall provides some tutorials to deal with binary classification problems as follows: + +- [Online advertisement click prediction](../binaryclass/general.html) +- [News classification](../binaryclass/news20_dataset.html) + +This page focuses on the evaluation of the results from such binary classification problems. +If your classifier outputs probability rather than 0/1 label, evaluation based on [Area Under the ROC Curve](./auc.md) would be more appropriate. + + +# Example + +For the metrics explanation, this page introduces toy example data and two metrics. + +## Data + +The following table shows the sample of binary classification's prediction. +In this case, `1` means positive label and `0` means negative label. +Left column includes supervised label data, +and center column includes predicted label by a binary classifier. + +| truth label| predicted label | | +|:---:|:---:|:---:| +| 1 | 0 |False Negative| +| 0 | 1 |False Positive| +| 0 | 0 |True Negative| +| 1 | 1 |True Positive| +| 0 | 1 |False Positive| +| 0 | 0 |True Negative| + +## Preliminary metrics + +Some evaluation metrics are calculated based on 4 values: + +- True Positive (TP): truth label is positive and predicted label is also positive +- True Negative (TN): truth label is negative and predicted label is also negative +- False Positive (FP): truth label is negative but predicted label is positive +- False Negative (FN): truth label is positive but predicted label is negative + +`TR` and `TN` represent correct classification, and `FP` and `FN` illustrate incorrect ones. + +In this example, we can obtain those values: + +- TP: 1 +- TN: 2 +- FP: 2 +- FN: 1 + +if you want to know about those metrics, Wikipedia provides [more detail information](https://en.wikipedia.org/wiki/Sensitivity_and_specificity). + +### Recall + +Recall indicates the true positive rate in truth positive labels. +The value is computed by the following equation: + +$$ +\mathrm{recall} = \frac{\mathrm{\#TP}}{\mathrm{\#TP} + \mathrm{\#FN}} +$$ + +In the previous example, $$\mathrm{precision} = \frac{1}{2}$$. + +### Precision + +Precision indicates the true positive rate in positive predictive labels. +The value is computed by the following equation: + +$$ +\mathrm{precision} = \frac{\mathrm{\#TP}}{\mathrm{\#TP} + \mathrm{\#FP}} +$$ + +In the previous example, $$\mathrm{precision} = \frac{1}{3}$$. + +# Metrics + +## F1-score + +F1-score is the harmonic mean of recall and precision. +F1-score is computed by the following equation: + +$$ +\mathrm{F}_1 = 2 \frac{\mathrm{precision} * \mathrm{recall}}{\mathrm{precision} + \mathrm{recall}} +$$ + +Hivemall's `fmeasure` function provides the option which can switch `micro`(default) or `binary` by passing `average` argument. + + +> Caution +> Hivemall also provides `f1score` function, but it is old function to obtain F1-score. The value of `f1score` is based on set operation. So, we recommend to use `fmeasure` function to get F1-score based on this article. + +You can learn more about this from the following external resource: + +- [scikit-learn's F1-score](http://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html) + + +### Micro average + +If `micro` is passed to `average`, --- End diff -- ð --- 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. ---
[GitHub] incubator-hivemall pull request #107: [HIVEMALL-132] Generalize f1score UDAF...
Github user takuti commented on a diff in the pull request: https://github.com/apache/incubator-hivemall/pull/107#discussion_r135449892 --- Diff: core/src/main/java/hivemall/evaluation/FMeasureUDAF.java --- @@ -18,118 +18,387 @@ */ package hivemall.evaluation; -import hivemall.utils.hadoop.WritableUtils; +import hivemall.UDAFEvaluatorWithOptions; +import hivemall.utils.hadoop.HiveUtils; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; +import hivemall.utils.lang.Primitives; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; + import org.apache.hadoop.hive.ql.exec.Description; -import org.apache.hadoop.hive.ql.exec.UDAF; -import org.apache.hadoop.hive.ql.exec.UDAFEvaluator; +import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException; +import org.apache.hadoop.hive.ql.exec.UDFArgumentException; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.parse.SemanticException; +import org.apache.hadoop.hive.ql.udf.generic.AbstractGenericUDAFResolver; +import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator; import org.apache.hadoop.hive.serde2.io.DoubleWritable; -import org.apache.hadoop.io.IntWritable; - -@SuppressWarnings("deprecation") -@Description(name = "f1score", -value = "_FUNC_(array[int], array[int]) - Return a F-measure/F1 score") -public final class FMeasureUDAF extends UDAF { - -public static class Evaluator implements UDAFEvaluator { - -public static class PartialResult { -long tp; -/** tp + fn */ -long totalAcutal; -/** tp + fp */ -long totalPredicted; - -PartialResult() { -this.tp = 0L; -this.totalPredicted = 0L; -this.totalAcutal = 0L; -} +import org.apache.hadoop.hive.serde2.objectinspector.*; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector; +import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo; +import org.apache.hadoop.io.LongWritable; -void updateScore(final List actual, final List predicted) { -final int numActual = actual.size(); -final int numPredicted = predicted.size(); -int countTp = 0; -for (int i = 0; i < numPredicted; i++) { -IntWritable p = predicted.get(i); -if (actual.contains(p)) { -countTp++; -} +import javax.annotation.Nonnull; + +@Description( +name = "fmeasure", +value = "_FUNC_(array | int | boolean actual , array | int | boolean predicted, String) - Return a F-measure (f1score is the special with beta=1.)") +public final class FMeasureUDAF extends AbstractGenericUDAFResolver { +@Override +public GenericUDAFEvaluator getEvaluator(@Nonnull TypeInfo[] typeInfo) throws SemanticException { +if (typeInfo.length != 2 && typeInfo.length != 3) { +throw new UDFArgumentTypeException(typeInfo.length - 1, +"_FUNC_ takes two or three arguments"); +} + +boolean isArg1ListOrIntOrBoolean = HiveUtils.isListTypeInfo(typeInfo[0]) +|| HiveUtils.isIntegerTypeInfo(typeInfo[0]) +|| HiveUtils.isBooleanTypeInfo(typeInfo[0]); +if (!isArg1ListOrIntOrBoolean) { +throw new UDFArgumentTypeException(0, +"The first argument `array/int/boolean actual` is invalid form: " + typeInfo[0]); +} + +boolean isArg2ListOrIntOrBoolean = HiveUtils.isListTypeInfo(typeInfo[1]) +|| HiveUtils.isIntegerTypeInfo(typeInfo[1]) +|| HiveUtils.isBooleanTypeInfo(typeInfo[1]); +if (!isArg2ListOrIntOrBoolean) { +throw new UDFArgumentTypeException(1, +"The second argument `array/int/boolean actual` is invalid form: " + typeInfo[1]); --- End diff -- Typo: `actual` => `predicted` --- 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. ---
[GitHub] incubator-hivemall pull request #107: [HIVEMALL-132] Generalize f1score UDAF...
Github user takuti commented on a diff in the pull request: https://github.com/apache/incubator-hivemall/pull/107#discussion_r135454567 --- Diff: docs/gitbook/eval/binary_classification_measures.md --- @@ -0,0 +1,261 @@ + + + + +# Binary problems + +Binary classification problem is the task to predict the label of each data given two categorized dataset. + +Hivemall provides some tutorials to deal with binary classification problems as follows: + +- [Online advertisement click prediction](../binaryclass/general.html) +- [News classification](../binaryclass/news20_dataset.html) + +This page focuses on the evaluation of the results from such binary classification problems. +If your classifier outputs probability rather than 0/1 label, evaluation based on [Area Under the ROC Curve](./auc.md) would be more appropriate. + + +# Example + +For the metrics explanation, this page introduces toy example data and two metrics. + +## Data + +The following table shows the sample of binary classification's prediction. +In this case, `1` means positive label and `0` means negative label. +Left column includes supervised label data, +and center column includes predicted label by a binary classifier. + +| truth label| predicted label | | +|:---:|:---:|:---:| +| 1 | 0 |False Negative| +| 0 | 1 |False Positive| +| 0 | 0 |True Negative| +| 1 | 1 |True Positive| +| 0 | 1 |False Positive| +| 0 | 0 |True Negative| + +## Preliminary metrics + +Some evaluation metrics are calculated based on 4 values: + +- True Positive (TP): truth label is positive and predicted label is also positive +- True Negative (TN): truth label is negative and predicted label is also negative +- False Positive (FP): truth label is negative but predicted label is positive +- False Negative (FN): truth label is positive but predicted label is negative + +`TR` and `TN` represent correct classification, and `FP` and `FN` illustrate incorrect ones. + +In this example, we can obtain those values: + +- TP: 1 +- TN: 2 +- FP: 2 +- FN: 1 + +if you want to know about those metrics, Wikipedia provides [more detail information](https://en.wikipedia.org/wiki/Sensitivity_and_specificity). + +### Recall + +Recall indicates the true positive rate in truth positive labels. +The value is computed by the following equation: + +$$ +\mathrm{recall} = \frac{\mathrm{\#TP}}{\mathrm{\#TP} + \mathrm{\#FN}} +$$ + +In the previous example, $$\mathrm{precision} = \frac{1}{2}$$. + +### Precision + +Precision indicates the true positive rate in positive predictive labels. +The value is computed by the following equation: + +$$ +\mathrm{precision} = \frac{\mathrm{\#TP}}{\mathrm{\#TP} + \mathrm{\#FP}} +$$ + +In the previous example, $$\mathrm{precision} = \frac{1}{3}$$. + +# Metrics + +## F1-score + +F1-score is the harmonic mean of recall and precision. +F1-score is computed by the following equation: + +$$ +\mathrm{F}_1 = 2 \frac{\mathrm{precision} * \mathrm{recall}}{\mathrm{precision} + \mathrm{recall}} +$$ + +Hivemall's `fmeasure` function provides the option which can switch `micro`(default) or `binary` by passing `average` argument. + + +> Caution +> Hivemall also provides `f1score` function, but it is old function to obtain F1-score. The value of `f1score` is based on set operation. So, we recommend to use `fmeasure` function to get F1-score based on this article. + +You can learn more about this from the following external resource: + +- [scikit-learn's F1-score](http://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html) + + +### Micro average + +If `micro` is passed to `average`, +recall and precision are modified to consider True Negative. +So, micro f1score are calculated by those modified recall and precision. + +$$ +\mathrm{recall} = \frac{\mathrm{\#TP} + \mathrm{\#TN}}{\mathrm{\#TP} + \mathrm{\#FN} + \mathrm{\#TN}} +$$ + +$$ +\mathrm{precision} = \frac{\mathrm{\#TP} + \mathrm{\#TN}}{\mathrm{\#TP} + \mathrm{\#FP} + \mathrm{\#TN}} +$$ + +If `average` argument is omitted, `fmeasure` use default value: `'-average micro'`. + +The following query shows the example to obtain F1-score. +Each row value has the same type (`int` or `boolean`). +If row value's type is `int`, `1` is considered as the positive label, and `-1` or `0` is considered as the negative label. + + +```sql +WITH data as ( + select 1 as truth, 0 as predicted +union all + select 0 as truth, 1 as predicted +union all + select 0 as truth, 0 as pred
[GitHub] incubator-hivemall pull request #107: [HIVEMALL-132] Generalize f1score UDAF...
Github user takuti commented on a diff in the pull request: https://github.com/apache/incubator-hivemall/pull/107#discussion_r135449941 --- Diff: core/src/main/java/hivemall/evaluation/FMeasureUDAF.java --- @@ -18,118 +18,387 @@ */ package hivemall.evaluation; -import hivemall.utils.hadoop.WritableUtils; +import hivemall.UDAFEvaluatorWithOptions; +import hivemall.utils.hadoop.HiveUtils; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; +import hivemall.utils.lang.Primitives; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; + import org.apache.hadoop.hive.ql.exec.Description; -import org.apache.hadoop.hive.ql.exec.UDAF; -import org.apache.hadoop.hive.ql.exec.UDAFEvaluator; +import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException; +import org.apache.hadoop.hive.ql.exec.UDFArgumentException; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.parse.SemanticException; +import org.apache.hadoop.hive.ql.udf.generic.AbstractGenericUDAFResolver; +import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator; import org.apache.hadoop.hive.serde2.io.DoubleWritable; -import org.apache.hadoop.io.IntWritable; - -@SuppressWarnings("deprecation") -@Description(name = "f1score", -value = "_FUNC_(array[int], array[int]) - Return a F-measure/F1 score") -public final class FMeasureUDAF extends UDAF { - -public static class Evaluator implements UDAFEvaluator { - -public static class PartialResult { -long tp; -/** tp + fn */ -long totalAcutal; -/** tp + fp */ -long totalPredicted; - -PartialResult() { -this.tp = 0L; -this.totalPredicted = 0L; -this.totalAcutal = 0L; -} +import org.apache.hadoop.hive.serde2.objectinspector.*; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector; +import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo; +import org.apache.hadoop.io.LongWritable; -void updateScore(final List actual, final List predicted) { -final int numActual = actual.size(); -final int numPredicted = predicted.size(); -int countTp = 0; -for (int i = 0; i < numPredicted; i++) { -IntWritable p = predicted.get(i); -if (actual.contains(p)) { -countTp++; -} +import javax.annotation.Nonnull; + +@Description( +name = "fmeasure", +value = "_FUNC_(array | int | boolean actual , array | int | boolean predicted, String) - Return a F-measure (f1score is the special with beta=1.)") +public final class FMeasureUDAF extends AbstractGenericUDAFResolver { +@Override +public GenericUDAFEvaluator getEvaluator(@Nonnull TypeInfo[] typeInfo) throws SemanticException { +if (typeInfo.length != 2 && typeInfo.length != 3) { +throw new UDFArgumentTypeException(typeInfo.length - 1, +"_FUNC_ takes two or three arguments"); +} + +boolean isArg1ListOrIntOrBoolean = HiveUtils.isListTypeInfo(typeInfo[0]) +|| HiveUtils.isIntegerTypeInfo(typeInfo[0]) +|| HiveUtils.isBooleanTypeInfo(typeInfo[0]); +if (!isArg1ListOrIntOrBoolean) { +throw new UDFArgumentTypeException(0, +"The first argument `array/int/boolean actual` is invalid form: " + typeInfo[0]); +} + +boolean isArg2ListOrIntOrBoolean = HiveUtils.isListTypeInfo(typeInfo[1]) +|| HiveUtils.isIntegerTypeInfo(typeInfo[1]) +|| HiveUtils.isBooleanTypeInfo(typeInfo[1]); +if (!isArg2ListOrIntOrBoolean) { +throw new UDFArgumentTypeException(1, +"The second argument `array/int/boolean actual` is invalid form: " + typeInfo[1]); +} + +if (typeInfo[0] != typeInfo[1]) { +throw new UDFArgumentTypeException(1, "The first argument's `actual` type is " ++ typeInfo[0] + ", but the second argument `predicated`'s type is not match: " ++ typeInfo[1]); +} + +return new Evaluator(); +} + +public static class Evaluator