You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2019/09/04 19:59:20 UTC

[GitHub] [nifi] markap14 commented on a change in pull request #3681: NIFI-6510 - Analytics framework

markap14 commented on a change in pull request #3681: NIFI-6510 - Analytics framework
URL: https://github.com/apache/nifi/pull/3681#discussion_r320939083
 
 

 ##########
 File path: nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/status/analytics/models/OrdinaryLeastSquares.java
 ##########
 @@ -0,0 +1,108 @@
+/*
+ * 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.nifi.controller.status.analytics.models;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.apache.commons.lang3.ArrayUtils;
+import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression;
+import org.apache.nifi.controller.status.analytics.StatusAnalyticsModel;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * <p>
+ * An implementation of the {@link StatusAnalyticsModel} that uses Ordinary Least Squares computation for regression.
+ * This model support multiple regression
+ * </p>
+ */
+public class OrdinaryLeastSquares implements StatusAnalyticsModel {
+
+    private static final Logger LOG = LoggerFactory.getLogger(OrdinaryLeastSquares.class);
+    private OLSMultipleLinearRegression olsModel;
+    private double[] coefficients;
+
+    public OrdinaryLeastSquares() {
+        this.olsModel = new OLSMultipleLinearRegression();
+    }
+
+    @Override
+    public void learn(Stream<Double[]> features, Stream<Double> labels) {
+        double[] labelArray = ArrayUtils.toPrimitive(labels.toArray(Double[]::new));
+        double[][] featuresMatrix = features.map(feature -> ArrayUtils.toPrimitive(feature)).toArray(double[][]::new);
+        this.olsModel.newSampleData(labelArray, featuresMatrix);
+        this.coefficients = olsModel.estimateRegressionParameters();
+    }
+
+    @Override
+    public Double predict(Double[] feature) {
+        if (coefficients != null) {
 
 Review comment:
   Code tends to read more easily using the idiom of `if (coefficients == null) { return null; }` rather than embedding the majority of the function's logic in the `if` condition...

----------------------------------------------------------------
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