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
ab4ed725
Commit
ab4ed725
authored
Feb 02, 2017
by
wxchan
Committed by
Guolin Ke
Feb 02, 2017
Browse files
add feature name (#280)
parent
cba87c37
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
102 additions
and
8 deletions
+102
-8
docs/Python-API.md
docs/Python-API.md
+12
-2
examples/python-guide/simple_example.py
examples/python-guide/simple_example.py
+2
-1
include/LightGBM/boosting.h
include/LightGBM/boosting.h
+6
-0
include/LightGBM/c_api.h
include/LightGBM/c_api.h
+16
-1
python-package/lightgbm/basic.py
python-package/lightgbm/basic.py
+31
-4
src/boosting/gbdt.h
src/boosting/gbdt.h
+6
-0
src/c_api.cpp
src/c_api.cpp
+23
-0
tests/python_package_test/test_engine.py
tests/python_package_test/test_engine.py
+6
-0
No files found.
docs/Python-API.md
View file @
ab4ed725
...
...
@@ -345,9 +345,19 @@ The methods of each Class is in alphabetical order.
Evaluation result list.
####feature_name()
Get feature names.
Returns
-------
result : array
Array of feature names.
####feature_importance(importance_type="split")
F
eature importances.
Get f
eature importances.
Parameters
----------
...
...
@@ -359,7 +369,7 @@ The methods of each Class is in alphabetical order.
Returns
-------
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)
...
...
examples/python-guide/simple_example.py
View file @
ab4ed725
...
...
@@ -58,7 +58,8 @@ model_json = gbm.dump_model()
with
open
(
'model.json'
,
'w+'
)
as
f
:
json
.
dump
(
model_json
,
f
,
indent
=
4
)
print
(
'Feature names:'
,
gbm
.
feature_name
())
print
(
'Calculate feature importances...'
)
# feature importances
print
(
'Feature importances:'
,
list
(
gbm
.
feature_importance
()))
# print('Feature importances:', list(gbm.feature_importance("gain")))
include/LightGBM/boosting.h
View file @
ab4ed725
...
...
@@ -167,6 +167,12 @@ public:
*/
virtual
int
MaxFeatureIdx
()
const
=
0
;
/*!
* \brief Get feature names of this model
* \return Feature names of this model
*/
virtual
std
::
vector
<
std
::
string
>
FeatureNames
()
const
=
0
;
/*!
* \brief Get index of label column
* \return index of label column
...
...
include/LightGBM/c_api.h
View file @
ab4ed725
...
...
@@ -365,13 +365,28 @@ LIGHTGBM_C_EXPORT int LGBM_BoosterGetCurrentIteration(BoosterHandle handle, int*
LIGHTGBM_C_EXPORT
int
LGBM_BoosterGetEvalCounts
(
BoosterHandle
handle
,
int
*
out_len
);
/*!
* \brief Get
N
ame of eval
* \brief Get
n
ame of eval
* \param out_len total number of eval results
* \param out_strs names of eval result, need to pre-allocate memory before call this
* \return 0 when succeed, -1 when failure happens
*/
LIGHTGBM_C_EXPORT
int
LGBM_BoosterGetEvalNames
(
BoosterHandle
handle
,
int
*
out_len
,
char
**
out_strs
);
/*!
* \brief Get name of features
* \param out_len total number of features
* \param out_strs names of features, need to pre-allocate memory before call this
* \return 0 when succeed, -1 when failure happens
*/
LIGHTGBM_C_EXPORT
int
LGBM_BoosterGetFeatureNames
(
BoosterHandle
handle
,
int
*
out_len
,
char
**
out_strs
);
/*!
* \brief Get number of features
* \param out_len total number of features
* \return 0 when succeed, -1 when failure happens
*/
LIGHTGBM_C_EXPORT
int
LGBM_BoosterGetNumFeature
(
BoosterHandle
handle
,
int
*
out_len
);
/*!
* \brief get evaluation for training data and validation data
Note: 1. you should call LGBM_BoosterGetEvalNames first to get the name of evaluation results
...
...
python-package/lightgbm/basic.py
View file @
ab4ed725
...
...
@@ -1582,15 +1582,41 @@ class Booster(object):
return
predictor
.
predict
(
data
,
num_iteration
,
raw_score
,
pred_leaf
,
data_has_header
,
is_reshape
)
def
_to_predictor
(
self
):
"""Convert to predictor
"""
"""Convert to predictor"""
predictor
=
_InnerPredictor
(
booster_handle
=
self
.
handle
)
predictor
.
pandas_categorical
=
self
.
pandas_categorical
return
predictor
def
feature_name
(
self
):
"""
Get feature names.
Returns
-------
result : array
Array of feature names.
"""
out_num_feature
=
ctypes
.
c_int
(
0
)
"""Get num of features"""
_safe_call
(
_LIB
.
LGBM_BoosterGetNumFeature
(
self
.
handle
,
ctypes
.
byref
(
out_num_feature
)))
num_feature
=
out_num_feature
.
value
"""Get name of features"""
tmp_out_len
=
ctypes
.
c_int
(
0
)
string_buffers
=
[
ctypes
.
create_string_buffer
(
255
)
for
i
in
range_
(
num_feature
)]
ptr_string_buffers
=
(
ctypes
.
c_char_p
*
num_feature
)(
*
map
(
ctypes
.
addressof
,
string_buffers
))
_safe_call
(
_LIB
.
LGBM_BoosterGetFeatureNames
(
self
.
handle
,
ctypes
.
byref
(
tmp_out_len
),
ptr_string_buffers
))
if
num_feature
!=
tmp_out_len
.
value
:
raise
ValueError
(
"Length of feature names doesn't equal with num_feature"
)
return
[
string_buffers
[
i
].
value
.
decode
()
for
i
in
range_
(
num_feature
)]
def
feature_importance
(
self
,
importance_type
=
'split'
):
"""
F
eature importances
Get f
eature importances
Parameters
----------
...
...
@@ -1601,7 +1627,8 @@ class Booster(object):
Returns
-------
Array of feature importances
result : array
Array of feature importances.
"""
if
importance_type
not
in
[
"split"
,
"gain"
]:
raise
KeyError
(
"importance_type must be split or gain"
)
...
...
src/boosting/gbdt.h
View file @
ab4ed725
...
...
@@ -176,6 +176,12 @@ public:
*/
inline
int
MaxFeatureIdx
()
const
override
{
return
max_feature_idx_
;
}
/*!
* \brief Get feature names of this model
* \return Feature names of this model
*/
inline
std
::
vector
<
std
::
string
>
FeatureNames
()
const
override
{
return
feature_names_
;
}
/*!
* \brief Get index of label column
* \return index of label column
...
...
src/c_api.cpp
View file @
ab4ed725
...
...
@@ -225,6 +225,15 @@ public:
return
idx
;
}
int
GetFeatureNames
(
char
**
out_strs
)
const
{
int
idx
=
0
;
for
(
const
auto
&
name
:
boosting_
->
FeatureNames
())
{
std
::
strcpy
(
out_strs
[
idx
],
name
.
c_str
());
++
idx
;
}
return
idx
;
}
const
Boosting
*
GetBoosting
()
const
{
return
boosting_
.
get
();
}
private:
...
...
@@ -724,6 +733,20 @@ LIGHTGBM_C_EXPORT int LGBM_BoosterGetEvalNames(BoosterHandle handle, int* out_le
API_END
();
}
LIGHTGBM_C_EXPORT
int
LGBM_BoosterGetFeatureNames
(
BoosterHandle
handle
,
int
*
out_len
,
char
**
out_strs
)
{
API_BEGIN
();
Booster
*
ref_booster
=
reinterpret_cast
<
Booster
*>
(
handle
);
*
out_len
=
ref_booster
->
GetFeatureNames
(
out_strs
);
API_END
();
}
LIGHTGBM_C_EXPORT
int
LGBM_BoosterGetNumFeature
(
BoosterHandle
handle
,
int
*
out_len
)
{
API_BEGIN
();
Booster
*
ref_booster
=
reinterpret_cast
<
Booster
*>
(
handle
);
*
out_len
=
ref_booster
->
GetBoosting
()
->
MaxFeatureIdx
()
+
1
;
API_END
();
}
LIGHTGBM_C_EXPORT
int
LGBM_BoosterGetEval
(
BoosterHandle
handle
,
int
data_idx
,
int
*
out_len
,
...
...
tests/python_package_test/test_engine.py
View file @
ab4ed725
...
...
@@ -124,6 +124,12 @@ class TestEngine(unittest.TestCase):
metrics
=
'l1'
,
verbose_eval
=
False
,
callbacks
=
[
lgb
.
reset_parameter
(
learning_rate
=
lambda
i
:
0.1
-
0.001
*
i
)])
def
test_feature_name
(
self
):
lgb_train
,
_
=
template
.
test_template
(
return_data
=
True
)
feature_names
=
[
'f'
+
str
(
i
)
for
i
in
range
(
13
)]
gbm
=
lgb
.
train
({
'verbose'
:
-
1
},
lgb_train
,
num_boost_round
=
10
,
feature_name
=
feature_names
)
self
.
assertListEqual
(
feature_names
,
gbm
.
feature_name
())
def
test_save_load_copy_pickle
(
self
):
gbm
=
template
.
test_template
(
num_round
=
20
,
return_model
=
True
)
_
,
ret_origin
=
template
.
test_template
(
init_model
=
gbm
)
...
...
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