zhipeng93 commented on a change in pull request #32: URL: https://github.com/apache/flink-ml/pull/32#discussion_r750250851
########## File path: flink-ml-lib/src/main/java/org/apache/flink/ml/classification/naivebayes/NaiveBayes.java ########## @@ -0,0 +1,333 @@ +/* + * 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.classification.naivebayes; + +import org.apache.flink.api.common.functions.AggregateFunction; +import org.apache.flink.api.common.functions.FlatMapFunction; +import org.apache.flink.api.common.functions.ReduceFunction; +import org.apache.flink.api.java.functions.KeySelector; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.api.java.tuple.Tuple3; +import org.apache.flink.api.java.tuple.Tuple4; +import org.apache.flink.ml.api.core.Estimator; +import org.apache.flink.ml.common.datastream.EndOfStreamWindows; +import org.apache.flink.ml.param.Param; +import org.apache.flink.ml.util.ReadWriteUtils; +import org.apache.flink.streaming.api.datastream.DataStream; +import org.apache.flink.streaming.api.functions.windowing.AllWindowFunction; +import org.apache.flink.streaming.api.windowing.windows.TimeWindow; +import org.apache.flink.table.api.Table; +import org.apache.flink.table.api.bridge.java.StreamTableEnvironment; +import org.apache.flink.table.api.internal.TableImpl; +import org.apache.flink.types.Row; +import org.apache.flink.util.Collector; +import org.apache.flink.util.Preconditions; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; + +/** + * Naive Bayes classifier is a simple probability classification algorithm using + * Bayes theorem based on independent assumption. It is an independent feature model. + * The input feature can be continual or categorical. + */ +public class NaiveBayes implements Estimator<NaiveBayes, NaiveBayesModel>, + NaiveBayesParams<NaiveBayes> { + private final Map<Param<?>, Object> paramMap = new HashMap<>(); + + @Override + public NaiveBayesModel fit(Table... inputs) { + String[] featureColNames = getFeatureCols(); + String labelColName = getLabelCol(); + String predictionCol = getPredictionCol(); + double smoothing = getSmoothing(); + + Preconditions.checkNotNull(inputs, "input table list should not be null"); + Preconditions.checkArgument(inputs.length == 1, "input table list should contain only one argument"); + Preconditions.checkArgument( + new HashSet<>(Arrays.asList(featureColNames)).size() == featureColNames.length, + "feature columns should not duplicate"); + Preconditions.checkNotNull(labelColName, "label column should be set"); + + StreamTableEnvironment tEnv = (StreamTableEnvironment) ((TableImpl) inputs[0]).getTableEnvironment(); + DataStream<Row> input = tEnv.toDataStream(inputs[0]); + + DataStream<NaiveBayesModelData> naiveBayesModel = input + .flatMap(new FlattenFunction( + featureColNames, + labelColName + )) + .keyBy((KeySelector<Tuple4<Object, Integer, Object, Double>, Object>) value -> new Tuple3<>(value.f0, value.f1, value.f2)) + .window(EndOfStreamWindows.get()) + .reduce((ReduceFunction<Tuple4<Object, Integer, Object, Double>>) (t0, t1) -> {t0.f3 += t1.f3; return t0; }) + .keyBy((KeySelector<Tuple4<Object, Integer, Object, Double>, Object>) value -> new Tuple2<>(value.f0, value.f1)) + .window(EndOfStreamWindows.get()) + .aggregate(new ValueMapFunction()) + .keyBy((KeySelector<Tuple4<Object, Integer, Map<Object, Double>, Double>, Object>) value -> value.f0) + .window(EndOfStreamWindows.get()) + .aggregate(new MapArrayFunction(featureColNames.length)) + .windowAll(EndOfStreamWindows.get()) Review comment: Is this helpful in this case? https://github.com/apache/flink-ml/pull/30 -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
