Return-Path: X-Original-To: apmail-flink-issues-archive@minotaur.apache.org Delivered-To: apmail-flink-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9E3B91844A for ; Mon, 20 Jul 2015 07:59:01 +0000 (UTC) Received: (qmail 83259 invoked by uid 500); 20 Jul 2015 07:58:27 -0000 Delivered-To: apmail-flink-issues-archive@flink.apache.org Received: (qmail 83212 invoked by uid 500); 20 Jul 2015 07:58:27 -0000 Mailing-List: contact issues-help@flink.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@flink.apache.org Delivered-To: mailing list issues@flink.apache.org Received: (qmail 83203 invoked by uid 99); 20 Jul 2015 07:58:27 -0000 Received: from Unknown (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 20 Jul 2015 07:58:27 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id 90D59C09A5 for ; Mon, 20 Jul 2015 07:58:26 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.971 X-Spam-Level: X-Spam-Status: No, score=0.971 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, T_RP_MATCHES_RCVD=-0.01, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-us-west.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id QJdJtTzffudP for ; Mon, 20 Jul 2015 07:58:20 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-us-west.apache.org (ASF Mail Server at mx1-us-west.apache.org) with SMTP id ACDF32312D for ; Mon, 20 Jul 2015 07:58:20 +0000 (UTC) Received: (qmail 83197 invoked by uid 99); 20 Jul 2015 07:58:20 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 20 Jul 2015 07:58:20 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 32083E0414; Mon, 20 Jul 2015 07:58:20 +0000 (UTC) From: sachingoel0101 To: issues@flink.incubator.apache.org Reply-To: issues@flink.incubator.apache.org References: In-Reply-To: Subject: [GitHub] flink pull request: [FLINK-1723] [ml] [WIP] Add cross validation f... Content-Type: text/plain Message-Id: <20150720075820.32083E0414@git1-us-west.apache.org> Date: Mon, 20 Jul 2015 07:58:20 +0000 (UTC) Github user sachingoel0101 commented on a diff in the pull request: https://github.com/apache/flink/pull/891#discussion_r34973783 --- Diff: flink-staging/flink-ml/src/main/scala/org/apache/flink/ml/evaluation/CrossValidation.scala --- @@ -0,0 +1,97 @@ +/* + * 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.evaluation + +import org.apache.flink.api.scala._ +import org.apache.flink.ml.RichDataSet +import java.util.Random + +import org.apache.flink.ml.pipeline.{EvaluateDataSetOperation, FitOperation, Predictor} + +object CrossValidation { + def crossValScore[P <: Predictor[P], T]( + predictor: P, + data: DataSet[T], + scorerOption: Option[Scorer] = None, + cv: FoldGenerator = KFold(), + seed: Long = new Random().nextLong())(implicit fitOperation: FitOperation[P, T], + evaluateDataSetOperation: EvaluateDataSetOperation[P, T, Double]): Array[DataSet[Double]] = { + val folds = cv.folds(data, 1) + + val scores = folds.map { + case (training: DataSet[T], testing: DataSet[T]) => + predictor.fit(training) + if (scorerOption.isEmpty) { + predictor.score(testing) + } else { + val s = scorerOption.get + s.evaluate(testing, predictor) + } + } + // TODO: Undecided on the return type: Array[DS[Double]] or DS[Double] i.e. reduce->union? + // Or: Return mean and std? + scores//.reduce((right: DataSet[Double], left: DataSet[Double]) => left.union(right)).mean() + } +} + +abstract class FoldGenerator { + + /** Takes a DataSet as input and creates splits (folds) of the data into + * (training, testing) pairs. + * + * @param input The DataSet that will be split into folds + * @param seed Seed for replicable splitting of the data + * @tparam T The type of the DataSet + * @return An Array containing K (training, testing) tuples, where training and testing are + * DataSets + */ + def folds[T]( + input: DataSet[T], + seed: Long = new Random().nextLong()): Array[(DataSet[T], DataSet[T])] +} + +class KFold(numFolds: Int) extends FoldGenerator{ + + /** Takes a DataSet as input and creates K splits (folds) of the data into non-overlapping + * (training, testing) pairs. + * + * Code based on Apache Spark implementation + * @param input The DataSet that will be split into folds + * @param seed Seed for replicable splitting of the data + * @tparam T The type of the DataSet + * @return An Array containing K (training, testing) tuples, where training and testing are + * DataSets + */ + override def folds[T]( + input: DataSet[T], + seed: Long = new Random().nextLong()): Array[(DataSet[T], DataSet[T])] = { + val numFoldsF = numFolds.toFloat + (1 to numFolds).map { fold => + val lb = (fold - 1) / numFoldsF + val ub = fold / numFoldsF + val validation = input.sampleBounded(lb, ub, complement = false, seed = seed) + val training = input.sampleBounded(lb, ub, complement = true, seed = seed) + (training, validation) --- End diff -- Ah yes. I see that now. --- 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 infrastructure@apache.org or file a JIRA ticket with INFRA. ---