1
1
import re
2
- import six
3
2
from typing import (
4
- Any , Iterable , Iterator , Tuple , Sized , List , Optional , Dict ,
5
- Union , Callable , Pattern
6
- )
3
+ Any , Iterable , Iterator , Sized , Optional , Union , Callable , Pattern )
7
4
8
5
import numpy as np
9
6
import scipy .sparse as sp
@@ -14,15 +11,14 @@ class FeatureNames(Sized, Iterable):
14
11
A list-like object with feature names. It allows
15
12
feature names for unknown features to be generated using
16
13
a provided template, and to avoid making copies of large objects
17
- in get_feature_names .
14
+ in get_feature_names_out .
18
15
"""
19
16
def __init__ (self ,
20
17
feature_names = None ,
21
- bias_name = None , # type: str
22
- unkn_template = None , # type: str
23
- n_features = None , # type: int
18
+ bias_name : Optional [ str ] = None ,
19
+ unkn_template : Optional [ str ] = None ,
20
+ n_features : Optional [ int ] = None ,
24
21
):
25
- # type: (...) -> None
26
22
if not (feature_names is not None or
27
23
(unkn_template is not None and n_features )):
28
24
raise ValueError (
@@ -39,20 +35,17 @@ def __init__(self,
39
35
'unkn_template should be set for sparse features' )
40
36
self .feature_names = feature_names
41
37
self .unkn_template = unkn_template
42
- self .n_features = n_features or len (feature_names ) # type: int
38
+ self .n_features : int = n_features or len (feature_names )
43
39
self .bias_name = bias_name
44
40
45
- def __repr__ (self ):
46
- # type: () -> str
41
+ def __repr__ (self ) -> str :
47
42
return '<FeatureNames: {} features {} bias>' .format (
48
43
self .n_features , 'with' if self .has_bias else 'without' )
49
44
50
- def __len__ (self ):
51
- # type: () -> int
45
+ def __len__ (self ) -> int :
52
46
return self .n_features + int (self .has_bias )
53
47
54
- def __iter__ (self ):
55
- # type: () -> Iterator[str]
48
+ def __iter__ (self ) -> Iterator [str ]:
56
49
return (self [i ] for i in range (len (self )))
57
50
58
51
def __getitem__ (self , idx ):
@@ -69,10 +62,10 @@ def __getitem__(self, idx):
69
62
return self .unkn_template % idx
70
63
raise IndexError ('Feature index out of range' )
71
64
72
- def _slice (self , aslice ):
73
- # type: (slice) -> Any
65
+ def _slice (self , aslice : slice ):
74
66
if isinstance (self .feature_names , (list , np .ndarray )):
75
67
# Fast path without going through __getitem__
68
+ lst : Union [list , np .ndarray ]
76
69
if self .has_bias :
77
70
lst = list (self .feature_names )
78
71
lst .append (self .bias_name )
@@ -84,29 +77,26 @@ def _slice(self, aslice):
84
77
return [self [idx ] for idx in indices ]
85
78
86
79
@property
87
- def has_bias (self ):
88
- # type: () -> bool
80
+ def has_bias (self ) -> bool :
89
81
return self .bias_name is not None
90
82
91
83
@property
92
- def bias_idx (self ):
93
- # type: () -> Optional[int]
84
+ def bias_idx (self ) -> Optional [int ]:
94
85
if self .has_bias :
95
86
return self .n_features
96
87
return None
97
88
98
- def filtered (self , feature_filter , x = None ):
99
- # type: (Callable, Any) -> Tuple[FeatureNames, List[int]]
89
+ def filtered (self , feature_filter : Callable , x = None ) -> tuple ['FeatureNames' , list [int ]]:
100
90
""" Return feature names filtered by a regular expression
101
91
``feature_re``, and indices of filtered elements.
102
92
"""
103
93
indices = []
104
94
filtered_feature_names = []
105
- indexed_names = None # type : Optional[Iterable[Tuple [int, Any]]]
95
+ indexed_names : Optional [Iterable [tuple [int , Any ]]] = None
106
96
if isinstance (self .feature_names , (np .ndarray , list )):
107
97
indexed_names = enumerate (self .feature_names )
108
98
elif isinstance (self .feature_names , dict ):
109
- indexed_names = six . iteritems ( self .feature_names )
99
+ indexed_names = self .feature_names . items ( )
110
100
elif self .feature_names is None :
111
101
indexed_names = []
112
102
assert indexed_names is not None
@@ -116,8 +106,7 @@ def filtered(self, feature_filter, x=None):
116
106
assert x .shape [0 ] == 1
117
107
flt = lambda nm , i : feature_filter (nm , x [0 , i ])
118
108
else :
119
- # FIXME: mypy warns about x[i] because it thinks x can be None
120
- flt = lambda nm , i : feature_filter (nm , x [i ]) # type: ignore
109
+ flt = lambda nm , i : feature_filter (nm , x [i ])
121
110
else :
122
111
flt = lambda nm , i : feature_filter (nm )
123
112
@@ -141,10 +130,9 @@ def filtered(self, feature_filter, x=None):
141
130
142
131
def handle_filter (self ,
143
132
feature_filter ,
144
- feature_re , # type: Pattern[str]
145
- x = None , # type: Any
146
- ):
147
- # type: (...) -> Tuple[FeatureNames, Union[List[int], None]]
133
+ feature_re : Pattern [str ],
134
+ x = None ,
135
+ ) -> tuple ['FeatureNames' , Union [list [int ], None ]]:
148
136
if feature_re is not None and feature_filter :
149
137
raise ValueError ('pass either feature_filter or feature_re' )
150
138
if feature_re is not None :
@@ -158,8 +146,7 @@ def handle_filter(self,
158
146
else :
159
147
return self , None
160
148
161
- def add_feature (self , feature ):
162
- # type: (Any) -> int
149
+ def add_feature (self , feature ) -> int :
163
150
""" Add a new feature name, return it's index.
164
151
"""
165
152
# A copy of self.feature_names is always made, because it might be
@@ -179,8 +166,7 @@ def add_feature(self, feature):
179
166
return idx
180
167
181
168
182
- def _all_feature_names (name ):
183
- # type: (Union[str, bytes, List[Dict]]) -> List[str]
169
+ def _all_feature_names (name : Union [str , bytes , list [dict ]]) -> list [str ]:
184
170
""" All feature names for a feature: usually just the feature itself,
185
171
but can be several features for unhashed features with collisions.
186
172
"""
0 commit comments