k-nearest neighbor (kNN) is a simple supervised learning method for text classification. Documents are classified by a majority vote of nearest neighbors (cosine distance) in the training data.
(self, k=10, distance=COSINE, train=[], baseline=FREQUENCY)
| 2050 | class KNN(Classifier): |
| 2051 | |
| 2052 | def __init__(self, k=10, distance=COSINE, train=[], baseline=FREQUENCY): |
| 2053 | """ k-nearest neighbor (kNN) is a simple supervised learning method for text classification. |
| 2054 | Documents are classified by a majority vote of nearest neighbors (cosine distance) |
| 2055 | in the training data. |
| 2056 | """ |
| 2057 | self.k = k # Number of nearest neighbors to observe. |
| 2058 | self.distance = distance # COSINE, EUCLIDEAN, ... |
| 2059 | Classifier.__init__(self, train, baseline) |
| 2060 | |
| 2061 | def train(self, document, type=None): |
| 2062 | """ Trains the classifier with the given document of the given type (i.e., class). |