@@ -47,6 +47,11 @@ class NearestCentroid(ClassifierMixin, BaseEstimator):
47
47
If the `"manhattan"` metric is provided, this centroid is the median
48
48
and for all other metrics, the centroid is now set to be the mean.
49
49
50
+ .. deprecated:: 1.3
51
+ Support for metrics other than `euclidean` and `manhattan` and for
52
+ callables was deprecated in version 1.3 and will be removed in
53
+ version 1.5.
54
+
50
55
.. versionchanged:: 0.19
51
56
`metric='precomputed'` was deprecated and now raises an error
52
57
@@ -101,10 +106,12 @@ class NearestCentroid(ClassifierMixin, BaseEstimator):
101
106
[1]
102
107
"""
103
108
109
+ _valid_metrics = set (_VALID_METRICS ) - {"mahalanobis" , "seuclidean" , "wminkowski" }
110
+
104
111
_parameter_constraints : dict = {
105
112
"metric" : [
106
113
StrOptions (
107
- set ( _VALID_METRICS ) - {"mahalanobis " , "seuclidean" , "wminkowski " }
114
+ _valid_metrics , deprecated = _valid_metrics - {"manhattan " , "euclidean " }
108
115
),
109
116
callable ,
110
117
],
@@ -134,6 +141,20 @@ def fit(self, X, y):
134
141
Fitted estimator.
135
142
"""
136
143
self ._validate_params ()
144
+
145
+ if isinstance (self .metric , str ) and self .metric not in (
146
+ "manhattan" ,
147
+ "euclidean" ,
148
+ ):
149
+ warnings .warn (
150
+ (
151
+ "Support for distance metrics other than euclidean and "
152
+ "manhattan and for callables was deprecated in version "
153
+ "1.3 and will be removed in version 1.5."
154
+ ),
155
+ FutureWarning ,
156
+ )
157
+
137
158
# If X is sparse and the metric is "manhattan", store it in a csc
138
159
# format is easier to calculate the median.
139
160
if self .metric == "manhattan" :
@@ -167,14 +188,14 @@ def fit(self, X, y):
167
188
if is_X_sparse :
168
189
center_mask = np .where (center_mask )[0 ]
169
190
170
- # XXX: Update other averaging methods according to the metrics.
171
191
if self .metric == "manhattan" :
172
192
# NumPy does not calculate median of sparse matrices.
173
193
if not is_X_sparse :
174
194
self .centroids_ [cur_class ] = np .median (X [center_mask ], axis = 0 )
175
195
else :
176
196
self .centroids_ [cur_class ] = csc_median_axis_0 (X [center_mask ])
177
197
else :
198
+ # TODO(1.5) remove warning when metric is only manhattan or euclidean
178
199
if self .metric != "euclidean" :
179
200
warnings .warn (
180
201
"Averaging for metrics other than "
@@ -209,6 +230,7 @@ def fit(self, X, y):
209
230
self .centroids_ = dataset_centroid_ [np .newaxis , :] + msd
210
231
return self
211
232
233
+ # TODO(1.5) remove note about precomputed metric
212
234
def predict (self , X ):
213
235
"""Perform classification on an array of test vectors `X`.
214
236
0 commit comments