Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
tianlh
LightGBM-DCU
Commits
b0f7aa50
Commit
b0f7aa50
authored
Jan 12, 2017
by
ClimbsRocks
Browse files
sklearn compatibility update- renames .feature_importance_ to .feature_importances_
parent
6c248d37
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
46 additions
and
46 deletions
+46
-46
docs/Python-API.md
docs/Python-API.md
+39
-39
examples/python-guide/sklearn_example.py
examples/python-guide/sklearn_example.py
+1
-1
python-package/lightgbm/sklearn.py
python-package/lightgbm/sklearn.py
+3
-3
tests/python_package_test/test_sklearn.py
tests/python_package_test/test_sklearn.py
+3
-3
No files found.
docs/Python-API.md
View file @
b0f7aa50
...
@@ -22,7 +22,7 @@
...
@@ -22,7 +22,7 @@
+
[
print_evaluation
](
Python-API.md#print_evaluationperiod1-show_stdvtrue
)
+
[
print_evaluation
](
Python-API.md#print_evaluationperiod1-show_stdvtrue
)
+
[
record_evaluation
](
Python-API.md#record_evaluationeval_result
)
+
[
record_evaluation
](
Python-API.md#record_evaluationeval_result
)
+
[
early_stopping
](
Python-API.md#early_stoppingstopping_rounds-verbosetrue
)
+
[
early_stopping
](
Python-API.md#early_stoppingstopping_rounds-verbosetrue
)
The methods of each Class is in alphabetical order.
The methods of each Class is in alphabetical order.
----
----
...
@@ -62,7 +62,7 @@ The methods of each Class is in alphabetical order.
...
@@ -62,7 +62,7 @@ The methods of each Class is in alphabetical order.
Other parameters
Other parameters
free_raw_data : Bool
free_raw_data : Bool
True if need to free raw data after construct inner dataset
True if need to free raw data after construct inner dataset
####create_valid(data, label=None, weight=None, group=None, silent=False, params=None)
####create_valid(data, label=None, weight=None, group=None, silent=False, params=None)
...
@@ -83,7 +83,7 @@ The methods of each Class is in alphabetical order.
...
@@ -83,7 +83,7 @@ The methods of each Class is in alphabetical order.
Whether print messages during construction
Whether print messages during construction
params : dict, optional
params : dict, optional
Other parameters
Other parameters
####get_group()
####get_group()
...
@@ -92,7 +92,7 @@ The methods of each Class is in alphabetical order.
...
@@ -92,7 +92,7 @@ The methods of each Class is in alphabetical order.
Returns
Returns
-------
-------
init_score : array
init_score : array
####get_init_score()
####get_init_score()
...
@@ -101,7 +101,7 @@ The methods of each Class is in alphabetical order.
...
@@ -101,7 +101,7 @@ The methods of each Class is in alphabetical order.
Returns
Returns
-------
-------
init_score : array
init_score : array
####get_label()
####get_label()
...
@@ -110,7 +110,7 @@ The methods of each Class is in alphabetical order.
...
@@ -110,7 +110,7 @@ The methods of each Class is in alphabetical order.
Returns
Returns
-------
-------
label : array
label : array
####get_weight()
####get_weight()
...
@@ -119,7 +119,7 @@ The methods of each Class is in alphabetical order.
...
@@ -119,7 +119,7 @@ The methods of each Class is in alphabetical order.
Returns
Returns
-------
-------
weight : array
weight : array
####num_data()
####num_data()
...
@@ -128,7 +128,7 @@ The methods of each Class is in alphabetical order.
...
@@ -128,7 +128,7 @@ The methods of each Class is in alphabetical order.
Returns
Returns
-------
-------
number of rows : int
number of rows : int
####num_feature()
####num_feature()
...
@@ -137,7 +137,7 @@ The methods of each Class is in alphabetical order.
...
@@ -137,7 +137,7 @@ The methods of each Class is in alphabetical order.
Returns
Returns
-------
-------
number of columns : int
number of columns : int
####save_binary(filename)
####save_binary(filename)
...
@@ -147,7 +147,7 @@ The methods of each Class is in alphabetical order.
...
@@ -147,7 +147,7 @@ The methods of each Class is in alphabetical order.
----------
----------
filename : str
filename : str
Name of the output file.
Name of the output file.
####set_categorical_feature(categorical_feature)
####set_categorical_feature(categorical_feature)
...
@@ -158,7 +158,7 @@ The methods of each Class is in alphabetical order.
...
@@ -158,7 +158,7 @@ The methods of each Class is in alphabetical order.
categorical_feature : list of str or list of int
categorical_feature : list of str or list of int
Name (str) or index (int) of categorical features
Name (str) or index (int) of categorical features
####set_feature_name(feature_name)
####set_feature_name(feature_name)
...
@@ -168,7 +168,7 @@ The methods of each Class is in alphabetical order.
...
@@ -168,7 +168,7 @@ The methods of each Class is in alphabetical order.
----------
----------
feature_name : list of str
feature_name : list of str
Feature names
Feature names
####set_group(group)
####set_group(group)
...
@@ -178,7 +178,7 @@ The methods of each Class is in alphabetical order.
...
@@ -178,7 +178,7 @@ The methods of each Class is in alphabetical order.
----------
----------
group : numpy array or list or None
group : numpy array or list or None
Group size of each group
Group size of each group
####set_init_score(init_score)
####set_init_score(init_score)
...
@@ -188,7 +188,7 @@ The methods of each Class is in alphabetical order.
...
@@ -188,7 +188,7 @@ The methods of each Class is in alphabetical order.
----------
----------
init_score : numpy array or list or None
init_score : numpy array or list or None
Init score for booster
Init score for booster
####set_label(label)
####set_label(label)
...
@@ -198,7 +198,7 @@ The methods of each Class is in alphabetical order.
...
@@ -198,7 +198,7 @@ The methods of each Class is in alphabetical order.
----------
----------
label : numpy array or list or None
label : numpy array or list or None
The label information to be set into Dataset
The label information to be set into Dataset
####set_reference(reference)
####set_reference(reference)
...
@@ -208,7 +208,7 @@ The methods of each Class is in alphabetical order.
...
@@ -208,7 +208,7 @@ The methods of each Class is in alphabetical order.
----------
----------
reference : Dataset
reference : Dataset
Will use reference as template to consturct current dataset
Will use reference as template to consturct current dataset
####set_weight(weight)
####set_weight(weight)
...
@@ -218,7 +218,7 @@ The methods of each Class is in alphabetical order.
...
@@ -218,7 +218,7 @@ The methods of each Class is in alphabetical order.
----------
----------
weight : numpy array or list or None
weight : numpy array or list or None
Weight for each data point
Weight for each data point
####subset(used_indices, params=None)
####subset(used_indices, params=None)
...
@@ -230,7 +230,7 @@ The methods of each Class is in alphabetical order.
...
@@ -230,7 +230,7 @@ The methods of each Class is in alphabetical order.
Used indices of this subset
Used indices of this subset
params : dict
params : dict
Other parameters
Other parameters
###Booster
###Booster
...
@@ -248,7 +248,7 @@ The methods of each Class is in alphabetical order.
...
@@ -248,7 +248,7 @@ The methods of each Class is in alphabetical order.
Path to the model file.
Path to the model file.
silent : boolean, optional
silent : boolean, optional
Whether print messages during construction
Whether print messages during construction
####add_valid(data, name)
####add_valid(data, name)
...
@@ -260,7 +260,7 @@ The methods of each Class is in alphabetical order.
...
@@ -260,7 +260,7 @@ The methods of each Class is in alphabetical order.
Validation data
Validation data
name : str
name : str
Name of validation data
Name of validation data
####attr(key)
####attr(key)
...
@@ -275,7 +275,7 @@ The methods of each Class is in alphabetical order.
...
@@ -275,7 +275,7 @@ The methods of each Class is in alphabetical order.
-------
-------
value : str
value : str
The attribute value of the key, returns None if attribute do not exist.
The attribute value of the key, returns None if attribute do not exist.
####current_iteration()
####current_iteration()
...
@@ -294,7 +294,7 @@ The methods of each Class is in alphabetical order.
...
@@ -294,7 +294,7 @@ The methods of each Class is in alphabetical order.
-------
-------
result : dict or list
result : dict or list
Json format of model
Json format of model
####eval(data, name, feval=None)
####eval(data, name, feval=None)
...
@@ -311,7 +311,7 @@ The methods of each Class is in alphabetical order.
...
@@ -311,7 +311,7 @@ The methods of each Class is in alphabetical order.
-------
-------
result : list
result : list
Evaluation result list.
Evaluation result list.
####eval_train(feval=None)
####eval_train(feval=None)
...
@@ -326,7 +326,7 @@ The methods of each Class is in alphabetical order.
...
@@ -326,7 +326,7 @@ The methods of each Class is in alphabetical order.
-------
-------
result: str
result: str
Evaluation result list.
Evaluation result list.
####eval_valid(feval=None)
####eval_valid(feval=None)
...
@@ -341,7 +341,7 @@ The methods of each Class is in alphabetical order.
...
@@ -341,7 +341,7 @@ The methods of each Class is in alphabetical order.
-------
-------
result : str
result : str
Evaluation result list.
Evaluation result list.
####feature_importance(importance_type="split")
####feature_importance(importance_type="split")
...
@@ -351,7 +351,7 @@ The methods of each Class is in alphabetical order.
...
@@ -351,7 +351,7 @@ The methods of each Class is in alphabetical order.
-------
-------
result : array
result : array
Array of feature importances
Array of feature importances
####predict(data, num_iteration=-1, raw_score=False, pred_leaf=False, data_has_header=False, is_reshape=True)
####predict(data, num_iteration=-1, raw_score=False, pred_leaf=False, data_has_header=False, is_reshape=True)
...
@@ -376,7 +376,7 @@ The methods of each Class is in alphabetical order.
...
@@ -376,7 +376,7 @@ The methods of each Class is in alphabetical order.
Returns
Returns
-------
-------
Prediction result
Prediction result
####reset_parameter(params)
####reset_parameter(params)
...
@@ -388,12 +388,12 @@ The methods of each Class is in alphabetical order.
...
@@ -388,12 +388,12 @@ The methods of each Class is in alphabetical order.
New parameters for boosters
New parameters for boosters
silent : boolean, optional
silent : boolean, optional
Whether print messages during construction
Whether print messages during construction
####rollback_one_iter()
####rollback_one_iter()
Rollback one iteration.
Rollback one iteration.
####save_model(filename, num_iteration=-1)
####save_model(filename, num_iteration=-1)
...
@@ -405,7 +405,7 @@ The methods of each Class is in alphabetical order.
...
@@ -405,7 +405,7 @@ The methods of each Class is in alphabetical order.
Filename to save
Filename to save
num_iteration : int
num_iteration : int
Number of iteration that want to save. < 0 means save all
Number of iteration that want to save. < 0 means save all
####set_attr(**kwargs)
####set_attr(**kwargs)
...
@@ -415,7 +415,7 @@ The methods of each Class is in alphabetical order.
...
@@ -415,7 +415,7 @@ The methods of each Class is in alphabetical order.
----------
----------
**kwargs
**kwargs
The attributes to set. Setting a value to None deletes an attribute.
The attributes to set. Setting a value to None deletes an attribute.
####set_train_data_name(name)
####set_train_data_name(name)
...
@@ -443,7 +443,7 @@ The methods of each Class is in alphabetical order.
...
@@ -443,7 +443,7 @@ The methods of each Class is in alphabetical order.
Returns
Returns
-------
-------
is_finished, bool
is_finished, bool
##Training API
##Training API
...
@@ -515,7 +515,7 @@ The methods of each Class is in alphabetical order.
...
@@ -515,7 +515,7 @@ The methods of each Class is in alphabetical order.
Returns
Returns
-------
-------
booster : a trained booster model
booster : a trained booster model
####cv(params, train_set, num_boost_round=10, nfold=5, stratified=False, shuffle=True, metrics=None, fobj=None, feval=None, init_model=None, feature_name='auto', categorical_feature='auto', early_stopping_rounds=None, fpreproc=None, verbose_eval=None, show_stdv=True, seed=0, callbacks=None)
####cv(params, train_set, num_boost_round=10, nfold=5, stratified=False, shuffle=True, metrics=None, fobj=None, feval=None, init_model=None, feature_name='auto', categorical_feature='auto', early_stopping_rounds=None, fpreproc=None, verbose_eval=None, show_stdv=True, seed=0, callbacks=None)
...
@@ -577,7 +577,7 @@ The methods of each Class is in alphabetical order.
...
@@ -577,7 +577,7 @@ The methods of each Class is in alphabetical order.
Returns
Returns
-------
-------
evaluation history : list of str
evaluation history : list of str
##Scikit-learn API
##Scikit-learn API
...
@@ -664,7 +664,7 @@ The methods of each Class is in alphabetical order.
...
@@ -664,7 +664,7 @@ The methods of each Class is in alphabetical order.
----
----
A custom objective function can be provided for the ``objective``
A custom objective function can be provided for the ``objective``
parameter. In this case, it should have the signature
parameter. In this case, it should have the signature
``objective(y_true, y_pred) -> grad, hess``
``objective(y_true, y_pred) -> grad, hess``
or ``objective(y_true, y_pred, group) -> grad, hess``:
or ``objective(y_true, y_pred, group) -> grad, hess``:
y_true: array_like of shape [n_samples]
y_true: array_like of shape [n_samples]
...
@@ -681,7 +681,7 @@ The methods of each Class is in alphabetical order.
...
@@ -681,7 +681,7 @@ The methods of each Class is in alphabetical order.
for multi-class task, the y_pred is group by class_id first, then group by row_id
for multi-class task, the y_pred is group by class_id first, then group by row_id
if you want to get i-th row y_pred in j-th class, the access way is y_pred[j*num_data+i]
if you want to get i-th row y_pred in j-th class, the access way is y_pred[j*num_data+i]
and you should group grad and hess in this way as well
and you should group grad and hess in this way as well
####apply(X, num_iteration=0)
####apply(X, num_iteration=0)
...
@@ -698,7 +698,7 @@ The methods of each Class is in alphabetical order.
...
@@ -698,7 +698,7 @@ The methods of each Class is in alphabetical order.
Returns
Returns
-------
-------
X_leaves : array_like, shape=[n_samples, n_trees]
X_leaves : array_like, shape=[n_samples, n_trees]
####fit(X, y, sample_weight=None, init_score=None, group=None, eval_set=None, eval_sample_weight=None, eval_init_score=None, eval_group=None, eval_metric=None, early_stopping_rounds=None, verbose=True, feature_name='auto', categorical_feature='auto', callbacks=None)
####fit(X, y, sample_weight=None, init_score=None, group=None, eval_set=None, eval_sample_weight=None, eval_init_score=None, eval_group=None, eval_metric=None, early_stopping_rounds=None, verbose=True, feature_name='auto', categorical_feature='auto', callbacks=None)
...
@@ -796,7 +796,7 @@ The methods of each Class is in alphabetical order.
...
@@ -796,7 +796,7 @@ The methods of each Class is in alphabetical order.
Get the evaluation results.
Get the evaluation results.
####feature_importance_
####feature_importance
s
_
Get normailized feature importances.
Get normailized feature importances.
...
@@ -826,7 +826,7 @@ The methods of each Class is in alphabetical order.
...
@@ -826,7 +826,7 @@ The methods of each Class is in alphabetical order.
####n_classes_
####n_classes_
Get number of classes.
Get number of classes.
###LGBMRegressor
###LGBMRegressor
...
...
examples/python-guide/sklearn_example.py
View file @
b0f7aa50
...
@@ -34,7 +34,7 @@ print('The rmse of prediction is:', mean_squared_error(y_test, y_pred) ** 0.5)
...
@@ -34,7 +34,7 @@ print('The rmse of prediction is:', mean_squared_error(y_test, y_pred) ** 0.5)
print
(
'Calculate feature importances...'
)
print
(
'Calculate feature importances...'
)
# feature importances
# feature importances
print
(
'Feature importances:'
,
list
(
gbm
.
feature_importance_
))
print
(
'Feature importances:'
,
list
(
gbm
.
feature_importance
s
_
))
# other scikit-learn modules
# other scikit-learn modules
estimator
=
lgb
.
LGBMRegressor
(
num_leaves
=
31
)
estimator
=
lgb
.
LGBMRegressor
(
num_leaves
=
31
)
...
...
python-package/lightgbm/sklearn.py
View file @
b0f7aa50
...
@@ -463,7 +463,7 @@ class LGBMModel(LGBMModelBase):
...
@@ -463,7 +463,7 @@ class LGBMModel(LGBMModelBase):
return
self
.
evals_result
return
self
.
evals_result
@
property
@
property
def
feature_importance_
(
self
):
def
feature_importance
s
_
(
self
):
"""Get normailized feature importances."""
"""Get normailized feature importances."""
importace_array
=
self
.
booster_
.
feature_importance
().
astype
(
np
.
float32
)
importace_array
=
self
.
booster_
.
feature_importance
().
astype
(
np
.
float32
)
return
importace_array
/
importace_array
.
sum
()
return
importace_array
/
importace_array
.
sum
()
...
@@ -472,9 +472,9 @@ class LGBMModel(LGBMModelBase):
...
@@ -472,9 +472,9 @@ class LGBMModel(LGBMModelBase):
def
booster
(
self
):
def
booster
(
self
):
return
self
.
booster_
return
self
.
booster_
@
LGBMDeprecated
(
'Use attribute feature_importance_ instead.'
)
@
LGBMDeprecated
(
'Use attribute feature_importance
s
_ instead.'
)
def
feature_importance
(
self
):
def
feature_importance
(
self
):
return
self
.
feature_importance_
return
self
.
feature_importance
s
_
class
LGBMRegressor
(
LGBMModel
,
LGBMRegressorBase
):
class
LGBMRegressor
(
LGBMModel
,
LGBMRegressorBase
):
...
...
tests/python_package_test/test_sklearn.py
View file @
b0f7aa50
...
@@ -105,12 +105,12 @@ class TestSklearn(unittest.TestCase):
...
@@ -105,12 +105,12 @@ class TestSklearn(unittest.TestCase):
gbm
=
template
.
test_template
(
return_model
=
True
)
gbm
=
template
.
test_template
(
return_model
=
True
)
gbm_clone
=
clone
(
gbm
)
gbm_clone
=
clone
(
gbm
)
self
.
assertIsInstance
(
gbm
.
booster_
,
lgb
.
Booster
)
self
.
assertIsInstance
(
gbm
.
booster_
,
lgb
.
Booster
)
self
.
assertIsInstance
(
gbm
.
feature_importance_
,
np
.
ndarray
)
self
.
assertIsInstance
(
gbm
.
feature_importance
s
_
,
np
.
ndarray
)
clf
=
template
.
test_template
(
load_digits
(
2
,
True
),
model
=
lgb
.
LGBMClassifier
,
return_model
=
True
)
clf
=
template
.
test_template
(
load_digits
(
2
,
True
),
model
=
lgb
.
LGBMClassifier
,
return_model
=
True
)
self
.
assertListEqual
(
sorted
(
clf
.
classes_
),
[
0
,
1
])
self
.
assertListEqual
(
sorted
(
clf
.
classes_
),
[
0
,
1
])
self
.
assertEqual
(
clf
.
n_classes_
,
2
)
self
.
assertEqual
(
clf
.
n_classes_
,
2
)
self
.
assertIsInstance
(
clf
.
booster_
,
lgb
.
Booster
)
self
.
assertIsInstance
(
clf
.
booster_
,
lgb
.
Booster
)
self
.
assertIsInstance
(
clf
.
feature_importance_
,
np
.
ndarray
)
self
.
assertIsInstance
(
clf
.
feature_importance
s
_
,
np
.
ndarray
)
def
test_joblib
(
self
):
def
test_joblib
(
self
):
gbm
=
template
.
test_template
(
num_round
=
10
,
return_model
=
True
)
gbm
=
template
.
test_template
(
num_round
=
10
,
return_model
=
True
)
...
@@ -118,7 +118,7 @@ class TestSklearn(unittest.TestCase):
...
@@ -118,7 +118,7 @@ class TestSklearn(unittest.TestCase):
gbm_pickle
=
joblib
.
load
(
'lgb.pkl'
)
gbm_pickle
=
joblib
.
load
(
'lgb.pkl'
)
self
.
assertIsInstance
(
gbm_pickle
.
booster_
,
lgb
.
Booster
)
self
.
assertIsInstance
(
gbm_pickle
.
booster_
,
lgb
.
Booster
)
self
.
assertDictEqual
(
gbm
.
get_params
(),
gbm_pickle
.
get_params
())
self
.
assertDictEqual
(
gbm
.
get_params
(),
gbm_pickle
.
get_params
())
self
.
assertListEqual
(
list
(
gbm
.
feature_importance_
),
list
(
gbm_pickle
.
feature_importance_
))
self
.
assertListEqual
(
list
(
gbm
.
feature_importance
s
_
),
list
(
gbm_pickle
.
feature_importance
s
_
))
X_train
,
X_test
,
y_train
,
y_test
=
template
.
test_template
(
return_data
=
True
)
X_train
,
X_test
,
y_train
,
y_test
=
template
.
test_template
(
return_data
=
True
)
gbm
.
fit
(
X_train
,
y_train
,
eval_set
=
[(
X_test
,
y_test
)],
verbose
=
False
)
gbm
.
fit
(
X_train
,
y_train
,
eval_set
=
[(
X_test
,
y_test
)],
verbose
=
False
)
gbm_pickle
.
fit
(
X_train
,
y_train
,
eval_set
=
[(
X_test
,
y_test
)],
verbose
=
False
)
gbm_pickle
.
fit
(
X_train
,
y_train
,
eval_set
=
[(
X_test
,
y_test
)],
verbose
=
False
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment