You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@knox.apache.org by GitBox <gi...@apache.org> on 2019/10/07 09:47:03 UTC

[GitHub] [knox] smolnar82 commented on a change in pull request #163: KNOX-2052 - KnoxShellTable mean, median, and mode methods

smolnar82 commented on a change in pull request #163: KNOX-2052 - KnoxShellTable mean, median, and mode methods
URL: https://github.com/apache/knox/pull/163#discussion_r331928191
 
 

 ##########
 File path: gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/KnoxShellTable.java
 ##########
 @@ -76,6 +80,98 @@ public KnoxShellTable value(Comparable<? extends Object> value) {
     return col;
   }
 
+  public double[] toDoubleArray(String colName) throws IllegalArgumentException {
+    List<Comparable<? extends Object>> col = values(colName);
+    double[] colArray = new double[col.size()];
+    int conversionMethod = 0;
+    for (int i = 0; i < col.size(); i++) {
+      if (i == 0) {
+        if (col.get(i) instanceof Integer) {
+          conversionMethod = INTEGER_CONVERSION_METHOD;
+        }
+        else if (col.get(i) instanceof Double) {
+          conversionMethod = DOUBLE_CONVERSION_METHOD;
+        }
+        else if (col.get(i) instanceof Float) {
+          conversionMethod = FLOAT_CONVERSION_METHOD;
+        }
+        else {
+           throw new IllegalArgumentException();
+        }
+      }
+      if (conversionMethod == INTEGER_CONVERSION_METHOD) {
+        colArray[i] = (double) ((Integer) col.get(i)).intValue();
+      }
+      if (conversionMethod == DOUBLE_CONVERSION_METHOD) {
+        colArray[i] = (double) ((Double) col.get(i));
+      }
+      if (conversionMethod == FLOAT_CONVERSION_METHOD) {
+        colArray[i] = (double) ((Float) col.get(i)).floatValue();
+      }
+    }
+    return colArray;
+  }
+
+  public double mean(String colName) {
+    double[] colArray = toDoubleArray(colName);
+    double arrayMean = StatUtils.mean(colArray);
+    return arrayMean;
+  }
+
+  public double mean(int colIndex) {
+    return mean(headers.get(colIndex));
+  }
+
+  public double median(String colName) {
+    double[] colArray = toDoubleArray(colName);
+    double arrayMedian = StatUtils.percentile(colArray, 50);
+    return arrayMedian;
 
 Review comment:
   `return StatUtils. percentile(toDoubleArray(colName), 50)` directly?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services