blob: 22e185eb044d55564593f791899520e7cf2d1af1 [file] [log] [blame]
/* ----------------------------------------------------------------------- *//**
*
* 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.
*
*//* ----------------------------------------------------------------------- */
CREATE TABLE kmeans_2d(
id SERIAL,
x DOUBLE PRECISION,
y DOUBLE PRECISION,
position DOUBLE PRECISION[]
);
INSERT INTO kmeans_2d(x, y, position)
SELECT
x, y,
ARRAY[
x + random() * 15.0,
y + random() * 15.0
]::DOUBLE PRECISION[] AS position
FROM (
SELECT
random() * 100.0 AS x,
random() * 100.0 AS y
FROM generate_series(1,10)
) AS centroids, generate_series(1,10) i;
CREATE TABLE centroids AS
SELECT x,y,position
FROM kmeans_2d
ORDER BY random()
LIMIT 10;
SELECT * FROM kmeanspp('kmeans_2d', 'position', 2, 'MADLIB_SCHEMA.squared_dist_norm2', 'MADLIB_SCHEMA.avg', 2);
SELECT * FROM kmeans_random('kmeans_2d', 'position', 2, 'MADLIB_SCHEMA.squared_dist_norm2', 'MADLIB_SCHEMA.avg', 2);
SELECT * FROM kmeans('kmeans_2d', 'position', 'centroids', 'position');