代码:
from sklearn.neighbors import KNeighborsClassifier
from sklearn.feature_selection import SelectFromModel
from sklearn import datasets
X,y = datasets.load_wine(True)
model = SelectFromModel(estimator= KNeighborsClassifier(),threshold='median')
model.fit(X,y)
SelectFromModel(estimator=KNeighborsClassifier(algorithm='auto', leaf_size=30,
metric='minkowski',
metric_params=None, n_jobs=None,
n_neighbors=5, p=2,
weights='uniform'),
max_features=None, norm_order=1, prefit=False,
threshold='median')
model.transform(X)
-------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-6-5431fe5f687b> in <module>
----> 1 model.transform(X)
d:\python3.7.4\lib\site-packages\sklearn\feature_selection\_base.py in transform(self, X)
75 X = check_array(X, dtype=None, accept_sparse='csr',
76 force_all_finite=not tags.get('allow_nan', True))
---> 77 mask = self.get_support()
78 if not mask.any():
79 warn("No features were selected: either the data is"
d:\python3.7.4\lib\site-packages\sklearn\feature_selection\_base.py in get_support(self, indices)
44 values are indices into the input feature vector.
45 """
---> 46 mask = self._get_support_mask()
47 return mask if not indices else np.where(mask)[0]
48
d:\python3.7.4\lib\site-packages\sklearn\feature_selection\_from_model.py in _get_support_mask(self)
176 ' "prefit=True" while passing the fitted'
177 ' estimator to the constructor.')
--> 178 scores = _get_feature_importances(estimator, self.norm_order)
179 threshold = _calculate_threshold(estimator, scores, self.threshold)
180 if self.max_features is not None:
d:\python3.7.4\lib\site-packages\sklearn\feature_selection\_from_model.py in _get_feature_importances(estimator, norm_order)
30 "`feature_importances_` attribute. Either pass a fitted estimator"
31 " to SelectFromModel or call fit before calling transform."
---> 32 % estimator.__class__.__name__)
33
34 return importances
ValueError: The underlying estimator KNeighborsClassifier has no `coef_` or `feature_importances_` attribute. Either pass a fitted estimator to SelectFromModel or call fit before calling transform.
网友评论