Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id BA1F4200C62 for ; Wed, 12 Apr 2017 02:13:52 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id B8E35160B9E; Wed, 12 Apr 2017 00:13:52 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id D7FEE160B9B for ; Wed, 12 Apr 2017 02:13:51 +0200 (CEST) Received: (qmail 91650 invoked by uid 500); 12 Apr 2017 00:13:46 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 91630 invoked by uid 99); 12 Apr 2017 00:13:46 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 12 Apr 2017 00:13:46 +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 90E45C14F1 for ; Wed, 12 Apr 2017 00:13:45 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -99.202 X-Spam-Level: X-Spam-Status: No, score=-99.202 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, RP_MATCHES_RCVD=-0.001, SPF_PASS=-0.001, USER_IN_WHITELIST=-100] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id tCzYOncZWLwo for ; Wed, 12 Apr 2017 00:13:44 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTP id 8A6D361F81 for ; Wed, 12 Apr 2017 00:13:43 +0000 (UTC) Received: from jira-lw-us.apache.org (unknown [207.244.88.139]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id A9529E0D67 for ; Wed, 12 Apr 2017 00:13:42 +0000 (UTC) Received: from jira-lw-us.apache.org (localhost [127.0.0.1]) by jira-lw-us.apache.org (ASF Mail Server at jira-lw-us.apache.org) with ESMTP id DC6E52407D for ; Wed, 12 Apr 2017 00:13:41 +0000 (UTC) Date: Wed, 12 Apr 2017 00:13:41 +0000 (UTC) From: "Rob Tompkins (JIRA)" To: issues@commons.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Updated] (MATH-1378) KMeansPlusPlusClusterer optimize seeding procedure, by computing sum of squared distances outside the loop. MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 archived-at: Wed, 12 Apr 2017 00:13:52 -0000 [ https://issues.apache.org/jira/browse/MATH-1378?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] Rob Tompkins updated MATH-1378: ------------------------------- Fix Version/s: 4.0 > KMeansPlusPlusClusterer optimize seeding procedure, by computing sum of squared distances outside the loop. > ----------------------------------------------------------------------------------------------------------- > > Key: MATH-1378 > URL: https://issues.apache.org/jira/browse/MATH-1378 > Project: Commons Math > Issue Type: Improvement > Reporter: Artem Barger > Assignee: Artem Barger > Fix For: 4.0 > > Attachments: MATH-1378.patch > > > Currently in KMeansPlusPlusClusterer class, function which implements initial clusters seeding *chooseInitialCenters*, has following computation executed inside the while loop cycle: > {code} > while (resultSet.size() < k) { > // Sum up the squared distances for the points in pointList not > // already taken. > double distSqSum = 0.0; > for (int i = 0; i < numPoints; i++) { > if (!taken[i]) { > distSqSum += minDistSquared[i]; > } > } > // Rest skipped for simplicity > {code} > While computation of this sum could be produced once outside the loop and latter adjusted according to the values of minimum distances to the centers set. E.g.: > {code} > // Sum up the squared distances for the points in pointList not > // already taken. > double distSqSum = 0.0; > // There is no need to compute sum of squared distances within the "while" loop > // we can compute initial value ones and maintain deltas in the loop. > for (int i = 0; i < numPoints; i++) { > if (!taken[i]) { > distSqSum += minDistSquared[i]; > } > } > while (resultSet.size() < k) { > // Add one new data point as a center. Each point x is chosen with > // probability proportional to D(x)2 > final double r = random.nextDouble() * distSqSum; > // The index of the next point to be added to the resultSet. > int nextPointIndex = -1; > // Sum through the squared min distances again, stopping when > // sum >= r. > double sum = 0.0; > for (int i = 0; i < numPoints; i++) { > if (!taken[i]) { > sum += minDistSquared[i]; > if (sum >= r) { > nextPointIndex = i; > break; > } > } > } > // If it's not set to >= 0, the point wasn't found in the previous > // for loop, probably because distances are extremely small. Just pick > // the last available point. > if (nextPointIndex == -1) { > for (int i = numPoints - 1; i >= 0; i--) { > if (!taken[i]) { > nextPointIndex = i; > break; > } > } > } > // We found one. > if (nextPointIndex >= 0) { > final T p = pointList.get(nextPointIndex); > resultSet.add(new CentroidCluster (p)); > // Mark it as taken. > taken[nextPointIndex] = true; > if (resultSet.size() < k) { > // Now update elements of minDistSquared. We only have to compute > // the distance to the new center to do this. > for (int j = 0; j < numPoints; j++) { > // Only have to worry about the points still not taken. > if (!taken[j]) { > double d = distance(p, pointList.get(j)); > // Subtracting the old value. > distSqSum -= minDistSquared[j]; > // Update minimum distance. > minDistSquared[j] = FastMath.min(d*d, minDistSquared[j]); > // Adjust the overall sum of squared distances. > distSqSum += minDistSquared[j]; > } > } > } > } else { > // None found -- > // Break from the while loop to prevent > // an infinite loop. > break; > } > } > {code} -- This message was sent by Atlassian JIRA (v6.3.15#6346)