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
c861be93
Commit
c861be93
authored
Nov 29, 2016
by
Guolin Ke
Browse files
some bugs fix
parent
6b288215
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
13 deletions
+56
-13
python-package/lightgbm/basic.py
python-package/lightgbm/basic.py
+35
-1
python-package/lightgbm/callback.py
python-package/lightgbm/callback.py
+1
-0
python-package/lightgbm/engine.py
python-package/lightgbm/engine.py
+20
-12
No files found.
python-package/lightgbm/basic.py
View file @
c861be93
...
...
@@ -519,7 +519,7 @@ class Dataset(object):
params_str
=
param_dict_to_str
(
params
)
_safe_call
(
_LIB
.
LGBM_DatasetGetSubset
(
ctypes
.
byref
(
self
.
handle
),
used_indices
.
data_as
(
ctypes
.
POINTER
(
ctypes
.
c_int32
)),
used_indices
.
ctypes
.
data_as
(
ctypes
.
POINTER
(
ctypes
.
c_int32
)),
used_indices
.
shape
[
0
],
c_str
(
params_str
),
ctypes
.
byref
(
ret
.
handle
)))
...
...
@@ -808,6 +808,7 @@ class Booster(object):
self
.
__need_reload_eval_info
=
True
self
.
__is_manage_handle
=
True
self
.
__train_data_name
=
"training"
self
.
__attr
=
{}
params
=
{}
if
params
is
None
else
params
if
silent
:
params
[
"verbose"
]
=
0
...
...
@@ -1205,3 +1206,36 @@ class Booster(object):
self
.
__higher_better_inner_eval
.
append
(
True
)
else
:
self
.
__higher_better_inner_eval
.
append
(
False
)
def
attr
(
self
,
key
):
"""Get attribute string from the Booster.
Parameters
----------
key : str
The key to get attribute from.
Returns
-------
value : str
The attribute value of the key, returns None if attribute do not exist.
"""
if
key
in
self
.
__attr
:
return
self
.
__attr
[
key
]
else
:
return
None
def
set_attr
(
self
,
**
kwargs
):
"""Set the attribute of the Booster.
Parameters
----------
**kwargs
The attributes to set. Setting a value to None deletes an attribute.
"""
for
key
,
value
in
kwargs
.
items
():
if
value
is
not
None
:
if
not
isinstance
(
value
,
STRING_TYPES
):
raise
ValueError
(
"Set Attr only accepts string values"
)
self
.
__attr
[
key
]
=
value
else
:
self
.
__attr
.
pop
(
key
,
None
)
python-package/lightgbm/callback.py
View file @
c861be93
from
__future__
import
absolute_import
import
collections
class
EarlyStopException
(
Exception
):
"""Exception of early stopping.
...
...
python-package/lightgbm/engine.py
View file @
c861be93
...
...
@@ -16,7 +16,7 @@ def _construct_dataset(data, reference=None,
group
=
None
init_score
=
None
if
other_fields
is
not
None
:
if
not
is
isinstance
(
other_fields
,
dict
):
if
not
isinstance
(
other_fields
,
dict
):
raise
TypeError
(
"other filed data should be dict type"
)
weight
=
None
if
'weight'
not
in
other_fields
else
other_fields
[
'weight'
]
group
=
None
if
'group'
not
in
other_fields
else
other_fields
[
'group'
]
...
...
@@ -127,7 +127,8 @@ def train(params, train_data, num_boost_round=100,
"""reduce cost for prediction training data"""
if
valid_datas
[
i
]
is
train_data
:
is_valid_contain_train
=
True
train_data_name
=
valid_names
[
i
]
if
valid_names
is
not
None
:
train_data_name
=
valid_names
[
i
]
continue
valid_set
=
_construct_dataset
(
valid_datas
[
i
],
...
...
@@ -136,7 +137,10 @@ def train(params, train_data, num_boost_round=100,
other_fields
,
predictor
)
valid_sets
.
append
(
valid_set
)
name_valid_sets
.
append
(
valid_names
[
i
])
if
valid_names
is
not
None
:
name_valid_sets
.
append
(
valid_names
[
i
])
else
:
name_valid_sets
.
append
(
'valid_'
+
str
(
i
))
"""process callbacks"""
callbacks
=
[]
if
callbacks
is
None
else
callbacks
...
...
@@ -153,8 +157,8 @@ def train(params, train_data, num_boost_round=100,
if
learning_rates
is
not
None
:
callbacks
.
append
(
callback
.
reset_learning_rate
(
learning_rates
))
if
eval
s
_result
is
not
None
:
callbacks
.
append
(
callback
.
record_evaluation
(
eval
s
_result
))
if
out_
eval_result
is
not
None
:
callbacks
.
append
(
callback
.
record_evaluation
(
out_
eval_result
))
callbacks_before_iter
=
[
cb
for
cb
in
callbacks
if
cb
.
__dict__
.
get
(
'before_iteration'
,
False
)]
...
...
@@ -203,7 +207,7 @@ def train(params, train_data, num_boost_round=100,
class
CVBooster
(
object
):
""""Auxiliary datastruct to hold one fold of CV."""
def
__init__
(
self
,
train_set
,
valid_test
,
param
):
def
__init__
(
self
,
train_set
,
valid_test
,
param
s
):
""""Initialize the CVBooster"""
self
.
train_set
=
train_set
self
.
valid_test
=
valid_test
...
...
@@ -268,12 +272,12 @@ def _agg_cv_result(raw_results):
metric_type
[
key
]
=
one_line
[
3
]
if
key
not
in
cvmap
:
cvmap
[
key
]
=
[]
cvmap
[
key
].
append
(
one_
result
[
2
])
cvmap
[
key
].
append
(
one_
line
[
2
])
results
=
[]
for
k
,
v
in
cvmap
.
items
():
v
=
np
.
array
(
v
)
mean
,
std
=
np
.
mean
(
v
),
np
.
std
(
v
)
results
.
ext
end
(
[
'cv_agg'
,
k
,
mean
,
metric_type
[
k
],
std
]
)
results
.
app
end
(
(
'cv_agg'
,
k
,
mean
,
metric_type
[
k
],
std
)
)
return
results
def
cv
(
params
,
train_data
,
num_boost_round
=
10
,
nfold
=
5
,
stratified
=
False
,
...
...
@@ -339,9 +343,14 @@ def cv(params, train_data, num_boost_round=10, nfold=5, stratified=False,
if
not
'metric'
in
params
:
params
[
'metric'
]
=
[]
else
:
if
is_str
(
params
[
'metric'
]):
params
[
'metric'
]
=
params
[
'metric'
].
split
(
','
)
else
:
params
[
'metric'
]
=
list
(
params
[
'metric'
])
if
len
(
metric
)
>
0
:
params
[
'metric'
].
extend
(
metric
)
if
metrics
is
not
None
and
len
(
metric
s
)
>
0
:
params
[
'metric'
].
extend
(
metric
s
)
train_set
=
_construct_dataset
(
train_data
,
None
,
params
,
train_fields
)
...
...
@@ -374,8 +383,7 @@ def cv(params, train_data, num_boost_round=10, nfold=5, stratified=False,
evaluation_result_list
=
None
))
for
fold
in
cvfolds
:
fold
.
update
(
fobj
)
res
=
aggcv
([
f
.
eval
(
feval
)
for
f
in
cvfolds
])
res
=
_agg_cv_result
([
f
.
eval
(
feval
)
for
f
in
cvfolds
])
for
_
,
key
,
mean
,
_
,
std
in
res
:
if
key
+
'-mean'
not
in
results
:
results
[
key
+
'-mean'
]
=
[]
...
...
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