neighbors¶
ai.neighbors
¶
ai.neighbors provides functionality for unsupervised and supervised
neighbors-based learning methods. Unsupervised nearest neighbors is the
foundation of many other learning methods, notably manifold learning and
spectral clustering. Supervised neighbors-based learning comes in two flavors:
classification for data with discrete labels, and regression for data with
continuous labels.
The principle behind nearest neighbor methods is to find a predefined number of
training samples closest in distance to the new point, and predict the label
from these. The number of samples can be a user-defined constant
(k-nearest neighbor learning), or vary based on the local density of points
(radius-based neighbor learning). The distance can, in general, be any metric
measure: standard Euclidean distance is the most common choice.
Neighbors-based methods are known as non-generalizing machine learning methods,
since they simply “remember” all of its training data (possibly transformed into
a fast indexing structure such as a Ball Tree or KD Tree).
Despite its simplicity, nearest neighbors has been successful in a large number of classification and regression problems, including handwritten digits and satellite image scenes. Being a non-parametric method, it is often successful in classification situations where the decision boundary is very irregular.
ai.neighbors implements the following nearest-neighbors algorithms:
ai.neighbors.knn.KNeighborsClassifier
KNeighborsClassifier
¶
Bases: DistanceMetric
Classifier implementing the k-nearest neighbors vote.
The k-nearest neighbors algorithm is a non-parametric, supervised learning classifier, which uses proximity to make classifications or predictions about the grouping of an individual data point. k-nearest neighbors algorithm is typically used as a classification algorithm, working off the assumption that similar points can be found near one another.
For classification problems, a class label is assigned on the basis of a majority vote - i.e., the label that is most frequently represented around a give data point is used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neighbors
|
int
|
Number of neighbors to use. By default |
3
|
p
|
int
|
Power parameter for the Minkowski metric. When |
2
|
metric
|
str
|
Metric to use for distance computation. Default is |
'euclidean'
|
Source code in ai/neighbors/knn.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |
__init__(*, n_neighbors=3, p=2, metric='euclidean')
¶
Initializes model's hyperparameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neighbors
|
int
|
Number of neighbors to use. By default |
3
|
p
|
int
|
Power parameter for the Minkowski metric. When |
2
|
metric
|
str
|
Metric to use for distance computation. Default is |
'euclidean'
|
Source code in ai/neighbors/knn.py
fit(X, y)
¶
Fit the k-nearest neighbors classifier from the training dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Sample vector. |
required |
y
|
ndarray
|
Target vector. |
required |
Returns:
| Type | Description |
|---|---|
None
|
The fitted |
Source code in ai/neighbors/knn.py
predict(X)
¶
Predict the class labels for the provided data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Test samples. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Class label for each data sample. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If predict method is called before fit. |
Source code in ai/neighbors/knn.py
knn
¶
DistanceMetric
¶
Distance metrices for computing k-nearest neighbors.
These distance metrices can be used for computing the distances between two
np.ndarray and not just for kNeighborsClassifier. This DistanceMetric
class supports Euclidean, Minkowski, Manhattan, and Hamming distance
metrices to compute the distance between two data points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric
|
str
|
The metric to use for computing distance. Default |
'minkowski'
|
minkowski_p
|
int
|
Power parameter for the Minkowski metric. |
2
|
Source code in ai/neighbors/knn.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
__init__(metric='minkowski', minkowski_p=2)
¶
Initializes metric parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric
|
str
|
The metric to use for computing distance. Default |
'minkowski'
|
minkowski_p
|
int
|
Power parameter for the Minkowski metric. |
2
|
Source code in ai/neighbors/knn.py
distance(x1, x2)
¶
Distance function that uses one of euclidean, minkowski, manhattan,
or hamming distance metric to measure distance between the given points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x1
|
Union[float32, ndarray]
|
Query point vector. |
required |
x2
|
Union[float32, ndarray]
|
Other point vector. |
required |
Returns:
| Type | Description |
|---|---|
Union[float32, ndarray]
|
Measured distance point vector. |
Source code in ai/neighbors/knn.py
euclidean(x1, x2)
¶
Euclidean distance.
This is the most commonly used distance measure, and it is limited to real-valued vectors. Using the below formula, it measures a straight line between the query point and the other point being measured.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x1
|
Union[float32, ndarray]
|
Query point vector. |
required |
x2
|
Union[float32, ndarray]
|
Other point vector. |
required |
Returns:
| Type | Description |
|---|---|
Union[float32, ndarray]
|
Measured distance point vector. |
Source code in ai/neighbors/knn.py
hamming(x1, x2)
¶
Hamming distance.
This technique is typically used with Boolean or string vectors, identifying the points where the vectors do not match. As a result, it has also been referred to as the overlap metric. This can be represented with the following formula:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x1
|
Union[float32, ndarray]
|
Query point vector. |
required |
x2
|
Union[float32, ndarray]
|
Other point vector. |
required |
Returns:
| Type | Description |
|---|---|
Union[float32, ndarray]
|
Measured distance point vector. |
Source code in ai/neighbors/knn.py
manhattan(x1, x2)
¶
Manhattan distance.
This is also another popular distance metric, which measures the absolute value between two points. It is also referred to as taxicab distance or city block distance as it is commonly visualized with a grid, illustrating how one might navigate from one address to another via city streets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x1
|
Union[float32, ndarray]
|
Query point vector. |
required |
x2
|
Union[float32, ndarray]
|
Other point vector. |
required |
Returns:
| Type | Description |
|---|---|
Union[float32, ndarray]
|
Measured distance point vector. |
Source code in ai/neighbors/knn.py
minkowski(x1, x2)
¶
Minkowski distance.
This distance measure is the generalized form of Euclidean and Manhattan distance metrics. The parameter, p, in the formula below, allows for the creation of other distance metrics. Euclidean distance is represented by this formula when p is equal to two, and Manhattan distance is denoted with p equal to one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x1
|
Union[float32, ndarray]
|
Query point vector. |
required |
x2
|
Union[float32, ndarray]
|
Other point vector. |
required |
Returns:
| Type | Description |
|---|---|
Union[float32, ndarray]
|
Measured distance point vector. |
Source code in ai/neighbors/knn.py
KNeighborsClassifier
¶
Bases: DistanceMetric
Classifier implementing the k-nearest neighbors vote.
The k-nearest neighbors algorithm is a non-parametric, supervised learning classifier, which uses proximity to make classifications or predictions about the grouping of an individual data point. k-nearest neighbors algorithm is typically used as a classification algorithm, working off the assumption that similar points can be found near one another.
For classification problems, a class label is assigned on the basis of a majority vote - i.e., the label that is most frequently represented around a give data point is used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neighbors
|
int
|
Number of neighbors to use. By default |
3
|
p
|
int
|
Power parameter for the Minkowski metric. When |
2
|
metric
|
str
|
Metric to use for distance computation. Default is |
'euclidean'
|
Source code in ai/neighbors/knn.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |
__init__(*, n_neighbors=3, p=2, metric='euclidean')
¶
Initializes model's hyperparameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neighbors
|
int
|
Number of neighbors to use. By default |
3
|
p
|
int
|
Power parameter for the Minkowski metric. When |
2
|
metric
|
str
|
Metric to use for distance computation. Default is |
'euclidean'
|
Source code in ai/neighbors/knn.py
fit(X, y)
¶
Fit the k-nearest neighbors classifier from the training dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Sample vector. |
required |
y
|
ndarray
|
Target vector. |
required |
Returns:
| Type | Description |
|---|---|
None
|
The fitted |
Source code in ai/neighbors/knn.py
predict(X)
¶
Predict the class labels for the provided data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Test samples. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Class label for each data sample. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If predict method is called before fit. |