|
10 | 10 | import numpy as np |
11 | 11 | import scipy.sparse |
12 | 12 | from scipy.sparse.csgraph import connected_components |
13 | | -from sklearn import preprocessing |
14 | 13 | from collections import defaultdict |
15 | 14 |
|
| 15 | + |
16 | 16 | # from .util import full, WSP2W resolve import cycle by |
17 | 17 | # forcing these into methods |
18 | 18 | from . import adjtools |
|
21 | 21 | __all__ = ["W", "WSP"] |
22 | 22 |
|
23 | 23 |
|
| 24 | +class _LabelEncoder(object): |
| 25 | + """Encode labels with values between 0 and n_classes-1. |
| 26 | +
|
| 27 | + Attributes |
| 28 | + ---------- |
| 29 | + classes_: array of shape [n_classes] |
| 30 | + Class labels for each index. |
| 31 | +
|
| 32 | + Examples |
| 33 | + -------- |
| 34 | + >>> le = _LabelEncoder() |
| 35 | + >>> le.fit(["NY", "CA", "NY", "CA", "TX", "TX"]) |
| 36 | + >>> le.classes_ |
| 37 | + array(['CA', 'NY', 'TX']) |
| 38 | + >>> le.transform(["NY", "CA", "NY", "CA", "TX", "TX"]) |
| 39 | + array([1, 0, 1, 0, 2, 2]) |
| 40 | + """ |
| 41 | + |
| 42 | + def fit(self, y): |
| 43 | + """Fit label encoder. |
| 44 | +
|
| 45 | + Parameters |
| 46 | + ---------- |
| 47 | + y : list |
| 48 | + list of labels |
| 49 | +
|
| 50 | + Returns |
| 51 | + ------- |
| 52 | + self : instance of self. |
| 53 | + Fitted label encoder. |
| 54 | + """ |
| 55 | + self.classes_ = np.unique(y) |
| 56 | + return self |
| 57 | + |
| 58 | + def transform(self, y): |
| 59 | + """Transform labels to normalized encoding. |
| 60 | +
|
| 61 | + Parameters |
| 62 | + ---------- |
| 63 | + y : list |
| 64 | + list of labels |
| 65 | +
|
| 66 | + Returns |
| 67 | + ------- |
| 68 | + y : array |
| 69 | + array of normalized labels. |
| 70 | + """ |
| 71 | + return np.searchsorted(self.classes_, y) |
| 72 | + |
| 73 | + |
24 | 74 | class W(object): |
25 | 75 | """ |
26 | 76 | Spatial weights class. Class attributes are described by their |
@@ -505,7 +555,7 @@ def to_sparse(self, fmt="coo"): |
505 | 555 | data = adj_list.weight |
506 | 556 | row = adj_list.focal |
507 | 557 | col = adj_list.neighbor |
508 | | - le = preprocessing.LabelEncoder() |
| 558 | + le = _LabelEncoder() |
509 | 559 | le.fit(row) |
510 | 560 | row = le.transform(row) |
511 | 561 | col = le.transform(col) |
|
0 commit comments