K-nearest neighbors is a method for finding the k closest points to a given data point in terms of a given metric. Its input consists of data points as features from testing examples, and it looks for k closest points in the training set for each of the data points in test set. The output of KNN depends on the type of task. For classification, the output is the majority vote of the classes of the k nearest data points. That is, the testing example gets assigned the most popular class from the nearest neighbors. For regression, the output is the average of the values of k nearest neighbors of the given test point.
knn( point_source,
point_column_name,
label_column_name,
test_source,
test_column_name,
id_column_name,
output_table,
operation,
k
)
Arguments
TEXT. Name of the table containing the training data points.
Training data points are expected to be stored row-wise in a column of type DOUBLE PRECISION[].
TEXT. Name of the column with training data points.
TEXT. Name of the column with labels/values of training data points.
TEXT. Name of the table containing the test data points.
Testing data points are expected to be stored row-wise in a column of type DOUBLE PRECISION[].
TEXT. Name of the column with testing data points.
TEXT. Name of the column having ids of data points in test data table.
TEXT. Name of the table to store final results.
TEXT. Type of task: 'r' for regression and 'c' for classification.
INTEGER. default: 1. Number of nearest neighbors to consider. For classification, should be an odd number to break ties.
The output of the KNN module is a table with the following columns:
| id | INTEGER. The ids of test data points. |
|---|---|
| test_column_name | DOUBLE PRECISION[]. The test data points. |
| prediction | INTEGER. Label in case of classification, average value in case of regression. |
DROP TABLE IF EXISTS knn_train_data;
CREATE TABLE knn_train_data (
id integer,
data integer[],
label float
);
INSERT INTO knn_train_data VALUES
(1, '{1,1}', 1.0),
(2, '{2,2}', 1.0),
(3, '{3,3}', 1.0),
(4, '{4,4}', 1.0),
(5, '{4,5}', 1.0),
(6, '{20,50}', 0.0),
(7, '{10,31}', 0.0),
(8, '{81,13}', 0.0),
(9, '{1,111}', 0.0);
DROP TABLE IF EXISTS knn_test_data;
CREATE TABLE knn_test_data (
id integer,
data integer[]
);
INSERT INTO knn_test_data VALUES
(1, '{2,1}'),
(2, '{2,6}'),
(3, '{15,40}'),
(4, '{12,1}'),
(5, '{2,90}'),
(6, '{50,45}');
DROP TABLE IF EXISTS madlib_knn_result_classification;
SELECT * FROM madlib.knn(
'knn_train_data', -- Table of training data
'data', -- Col name of training data
'label', -- Training labels
'knn_test_data', -- Table of test data
'data', -- Col name of test data
'id', -- Col name of id in test data
'madlib_knn_result_classification', -- Output table
'c', -- Classification
3 -- Number of nearest neighbours
);
SELECT * from madlib_knn_result_classification ORDER BY id;
Result:
id | data | prediction
----+---------+------------
1 | {2,1} | 1
2 | {2,6} | 1
3 | {15,40} | 0
4 | {12,1} | 1
5 | {2,90} | 0
6 | {50,45} | 0
(6 rows)
DROP TABLE IF EXISTS madlib_knn_result_regression;
SELECT * FROM madlib.knn(
'knn_train_data', -- Table of training data
'data', -- Col name of training data
'label', -- Training labels
'knn_test_data', -- Table of test data
'data', -- Col name of test data
'id', -- Col name of id in test data
'madlib_knn_result_regression', -- Output table
'r', -- Regressions
3 -- Number of nearest neighbours
);
SELECT * from madlib_knn_result_regression ORDER BY id;
Result:
id | data | prediction
----+---------+-------------------
1 | {2,1} | 1
2 | {2,6} | 1
3 | {15,40} | 0.333333333333333
4 | {12,1} | 1
5 | {2,90} | 0
6 | {50,45} | 0
(6 rows)
The training data points are vectors in a multidimensional feature space, each with a class label. The training phase of the algorithm consists only of storing the feature vectors and class labels of the training points.
In the classification phase, k is a user-defined constant, and an unlabeled vector (a test point) is classified by assigning the label which is most frequent among the k training samples nearest to that test point. In case of regression, average of the values of these k training samples is assigned to the test point. The only distance metric supported in this version is MADlib's squared_dist_norm2. Other distance metrics will be added in a future release of this module.
[1] Wikipedia, k-nearest neighbors algorithm, https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm
[2] N. S. Altman: An Introduction to Kernel and Nearest-Neighbor Nonparametric Regression http://www.stat.washington.edu/courses/stat527/s13/readings/Altman_AmStat_1992.pdf
[3] Gongde Guo1, Hui Wang, David Bell, Yaxin Bi, Kieran Greer: KNN Model-Based Approach in Classification, https://ai2-s2-pdfs.s3.amazonaws.com/a7e2/814ec5db800d2f8c4313fd436e9cf8273821.pdf