Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
scikit-learn
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ian Johnson
scikit-learn
Commits
3a106fc7
Commit
3a106fc7
authored
8 years ago
by
fukatani
Committed by
Nelle Varoquaux
8 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Fix _get_importances. (#7487)
Not to use hasattr.
parent
5fcc7e5c
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
sklearn/feature_selection/from_model.py
+3
-4
3 additions, 4 deletions
sklearn/feature_selection/from_model.py
sklearn/feature_selection/rfe.py
+2
-2
2 additions, 2 deletions
sklearn/feature_selection/rfe.py
with
5 additions
and
6 deletions
sklearn/feature_selection/from_model.py
+
3
−
4
View file @
3a106fc7
...
@@ -14,17 +14,16 @@ from ..exceptions import NotFittedError
...
@@ -14,17 +14,16 @@ from ..exceptions import NotFittedError
def
_get_feature_importances
(
estimator
):
def
_get_feature_importances
(
estimator
):
"""
Retrieve or aggregate feature importances from estimator
"""
"""
Retrieve or aggregate feature importances from estimator
"""
if
hasattr
(
estimator
,
"
feature_importances_
"
):
importances
=
getattr
(
estimator
,
"
feature_importances_
"
,
None
)
importances
=
estimator
.
feature_importances_
el
if
hasattr
(
estimator
,
"
coef_
"
):
if
importances
is
None
and
hasattr
(
estimator
,
"
coef_
"
):
if
estimator
.
coef_
.
ndim
==
1
:
if
estimator
.
coef_
.
ndim
==
1
:
importances
=
np
.
abs
(
estimator
.
coef_
)
importances
=
np
.
abs
(
estimator
.
coef_
)
else
:
else
:
importances
=
np
.
sum
(
np
.
abs
(
estimator
.
coef_
),
axis
=
0
)
importances
=
np
.
sum
(
np
.
abs
(
estimator
.
coef_
),
axis
=
0
)
el
s
e
:
el
if
importances
is
Non
e
:
raise
ValueError
(
raise
ValueError
(
"
The underlying estimator %s has no `coef_` or
"
"
The underlying estimator %s has no `coef_` or
"
"
`feature_importances_` attribute. Either pass a fitted estimator
"
"
`feature_importances_` attribute. Either pass a fitted estimator
"
...
...
This diff is collapsed.
Click to expand it.
sklearn/feature_selection/rfe.py
+
2
−
2
View file @
3a106fc7
...
@@ -171,9 +171,9 @@ class RFE(BaseEstimator, MetaEstimatorMixin, SelectorMixin):
...
@@ -171,9 +171,9 @@ class RFE(BaseEstimator, MetaEstimatorMixin, SelectorMixin):
# Get coefs
# Get coefs
if
hasattr
(
estimator
,
'
coef_
'
):
if
hasattr
(
estimator
,
'
coef_
'
):
coefs
=
estimator
.
coef_
coefs
=
estimator
.
coef_
elif
hasattr
(
estimator
,
'
feature_importances_
'
):
coefs
=
estimator
.
feature_importances_
else
:
else
:
coefs
=
getattr
(
estimator
,
'
feature_importances_
'
,
None
)
if
coefs
is
None
:
raise
RuntimeError
(
'
The classifier does not expose
'
raise
RuntimeError
(
'
The classifier does not expose
'
'"
coef_
"
or
"
feature_importances_
"
'
'"
coef_
"
or
"
feature_importances_
"
'
'
attributes
'
)
'
attributes
'
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment