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
452b41f0
Commit
452b41f0
authored
Nov 30, 2016
by
Guolin Ke
Browse files
add more tests
parent
44fcf16c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
114 additions
and
7 deletions
+114
-7
python-package/lightgbm/engine.py
python-package/lightgbm/engine.py
+1
-1
python-package/lightgbm/sklearn.py
python-package/lightgbm/sklearn.py
+10
-6
tests/python_package_test/test_sklearn.py
tests/python_package_test/test_sklearn.py
+103
-0
No files found.
python-package/lightgbm/engine.py
View file @
452b41f0
...
@@ -208,7 +208,7 @@ def train(params, train_data, num_boost_round=100,
...
@@ -208,7 +208,7 @@ def train(params, train_data, num_boost_round=100,
booster
.
best_iteration
=
int
(
booster
.
attr
(
'best_iteration'
))
booster
.
best_iteration
=
int
(
booster
.
attr
(
'best_iteration'
))
else
:
else
:
booster
.
best_iteration
=
num_boost_round
-
1
booster
.
best_iteration
=
num_boost_round
-
1
return
num_
boost
_round
return
boost
er
class
CVBooster
(
object
):
class
CVBooster
(
object
):
...
...
python-package/lightgbm/sklearn.py
View file @
452b41f0
...
@@ -207,6 +207,9 @@ class LGBMModel(LGBMModelBase):
...
@@ -207,6 +207,9 @@ class LGBMModel(LGBMModelBase):
evals_result
=
{}
evals_result
=
{}
params
=
self
.
get_params
()
params
=
self
.
get_params
()
if
other_params
is
not
None
:
params
.
update
(
other_params
)
if
callable
(
self
.
objective
):
if
callable
(
self
.
objective
):
fobj
=
_objective_decorator
(
self
.
objective
)
fobj
=
_objective_decorator
(
self
.
objective
)
params
[
"objective"
]
=
"None"
params
[
"objective"
]
=
"None"
...
@@ -215,14 +218,13 @@ class LGBMModel(LGBMModelBase):
...
@@ -215,14 +218,13 @@ class LGBMModel(LGBMModelBase):
fobj
=
None
fobj
=
None
if
callable
(
eval_metric
):
if
callable
(
eval_metric
):
feval
=
eval_metric
feval
=
eval_metric
el
se
:
el
if
is_str
(
eval_metric
)
or
isinstance
(
eval_metric
,
list
)
:
feval
=
None
feval
=
None
params
.
update
({
'metric'
:
eval_metric
})
params
.
update
({
'metric'
:
eval_metric
})
else
:
feval
=
None
feval
=
eval_metric
if
callable
(
eval_metric
)
else
None
feval
=
eval_metric
if
callable
(
eval_metric
)
else
None
if
other_params
is
not
None
:
params
.
update
(
other_params
)
self
.
_Booster
=
train
(
params
,
(
X
,
y
),
self
.
_Booster
=
train
(
params
,
(
X
,
y
),
self
.
n_estimators
,
valid_datas
=
eval_set
,
self
.
n_estimators
,
valid_datas
=
eval_set
,
early_stopping_rounds
=
early_stopping_rounds
,
early_stopping_rounds
=
early_stopping_rounds
,
...
@@ -296,10 +298,12 @@ class LGBMClassifier(LGBMModel, LGBMClassifierBase):
...
@@ -296,10 +298,12 @@ class LGBMClassifier(LGBMModel, LGBMClassifierBase):
other_params
=
{}
other_params
=
{}
if
self
.
n_classes_
>
2
:
if
self
.
n_classes_
>
2
:
# Switch to using a multiclass objective in the underlying LGBM instance
# Switch to using a multiclass objective in the underlying LGBM instance
other_params
[
"objective"
]
=
"multiclass"
if
not
callable
(
self
.
objective
):
self
.
objective
=
"multiclass"
other_params
[
'num_class'
]
=
self
.
n_classes_
other_params
[
'num_class'
]
=
self
.
n_classes_
else
:
else
:
other_params
[
"objective"
]
=
"binary"
if
not
callable
(
self
.
objective
):
self
.
objective
=
"binary"
self
.
_le
=
LGBMLabelEncoder
().
fit
(
y
)
self
.
_le
=
LGBMLabelEncoder
().
fit
(
y
)
training_labels
=
self
.
_le
.
transform
(
y
)
training_labels
=
self
.
_le
.
transform
(
y
)
...
...
tests/python_package_test/test_sklearn.py
0 → 100644
View file @
452b41f0
import
numpy
as
np
import
random
import
lightgbm
as
lgb
rng
=
np
.
random
.
RandomState
(
2016
)
def
test_binary_classification
():
from
sklearn
import
datasets
,
metrics
,
model_selection
X
,
y
=
datasets
.
make_classification
(
n_samples
=
10000
,
n_features
=
100
)
x_train
,
x_test
,
y_train
,
y_test
=
model_selection
.
train_test_split
(
X
,
y
,
test_size
=
0.1
)
lgb_model
=
lgb
.
LGBMClassifier
().
fit
(
x_train
,
y_train
,
eval_set
=
[[
x_train
,
y_train
],(
x_test
,
y_test
)],
eval_metric
=
'binary_logloss'
)
from
sklearn.datasets
import
load_digits
digits
=
load_digits
(
2
)
y
=
digits
[
'target'
]
X
=
digits
[
'data'
]
x_train
,
x_test
,
y_train
,
y_test
=
model_selection
.
train_test_split
(
X
,
y
,
test_size
=
0.2
)
lgb_model
=
lgb
.
LGBMClassifier
().
fit
(
x_train
,
y_train
,
eval_set
=
[[
x_train
,
y_train
],(
x_test
,
y_test
)],
eval_metric
=
'binary_logloss'
)
preds
=
lgb_model
.
predict
(
x_test
)
err
=
sum
(
1
for
i
in
range
(
len
(
preds
))
if
int
(
preds
[
i
]
>
0.5
)
!=
y_test
[
i
])
/
float
(
len
(
preds
))
assert
err
<
0.1
def
test_multiclass_classification
():
from
sklearn.datasets
import
load_iris
from
sklearn
import
datasets
,
metrics
,
model_selection
def
check_pred
(
preds
,
labels
):
err
=
sum
(
1
for
i
in
range
(
len
(
preds
))
if
int
(
preds
[
i
]
>
0.5
)
!=
labels
[
i
])
/
float
(
len
(
preds
))
assert
err
<
0.7
X
,
y
=
datasets
.
make_classification
(
n_samples
=
10000
,
n_features
=
100
,
n_classes
=
4
,
n_informative
=
3
)
x_train
,
x_test
,
y_train
,
y_test
=
model_selection
.
train_test_split
(
X
,
y
,
test_size
=
0.1
)
lgb_model
=
lgb
.
LGBMClassifier
().
fit
(
x_train
,
y_train
,
eval_set
=
[[
x_train
,
y_train
],(
x_test
,
y_test
)],
eval_metric
=
'multi_logloss'
)
preds
=
lgb_model
.
predict
(
x_test
)
check_pred
(
preds
,
y_test
)
def
test_regression
():
from
sklearn.metrics
import
mean_squared_error
from
sklearn.datasets
import
load_boston
from
sklearn.cross_validation
import
KFold
from
sklearn
import
datasets
,
metrics
,
model_selection
boston
=
load_boston
()
y
=
boston
[
'target'
]
X
=
boston
[
'data'
]
x_train
,
x_test
,
y_train
,
y_test
=
model_selection
.
train_test_split
(
X
,
y
,
test_size
=
0.1
)
lgb_model
=
lgb
.
LGBMRegressor
().
fit
(
x_train
,
y_train
,
eval_set
=
[[
x_train
,
y_train
],(
x_test
,
y_test
)],
eval_metric
=
'l2'
)
preds
=
lgb_model
.
predict
(
x_test
)
assert
mean_squared_error
(
preds
,
y_test
)
<
30
def
test_regression_with_custom_objective
():
from
sklearn.metrics
import
mean_squared_error
from
sklearn.datasets
import
load_boston
from
sklearn.cross_validation
import
KFold
from
sklearn
import
datasets
,
metrics
,
model_selection
def
objective_ls
(
y_true
,
y_pred
):
grad
=
(
y_pred
-
y_true
)
hess
=
np
.
ones
(
len
(
y_true
))
return
grad
,
hess
boston
=
load_boston
()
y
=
boston
[
'target'
]
X
=
boston
[
'data'
]
x_train
,
x_test
,
y_train
,
y_test
=
model_selection
.
train_test_split
(
X
,
y
,
test_size
=
0.1
)
lgb_model
=
lgb
.
LGBMRegressor
(
objective
=
objective_ls
).
fit
(
x_train
,
y_train
,
eval_set
=
[[
x_train
,
y_train
],(
x_test
,
y_test
)],
eval_metric
=
'l2'
)
preds
=
lgb_model
.
predict
(
x_test
)
assert
mean_squared_error
(
preds
,
y_test
)
<
30
def
test_binary_classification_with_custom_objective
():
from
sklearn
import
datasets
,
metrics
,
model_selection
def
logregobj
(
y_true
,
y_pred
):
y_pred
=
1.0
/
(
1.0
+
np
.
exp
(
-
y_pred
))
grad
=
y_pred
-
y_true
hess
=
y_pred
*
(
1.0
-
y_pred
)
return
grad
,
hess
X
,
y
=
datasets
.
make_classification
(
n_samples
=
10000
,
n_features
=
100
)
x_train
,
x_test
,
y_train
,
y_test
=
model_selection
.
train_test_split
(
X
,
y
,
test_size
=
0.1
)
lgb_model
=
lgb
.
LGBMClassifier
(
objective
=
logregobj
).
fit
(
x_train
,
y_train
,
eval_set
=
[[
x_train
,
y_train
],(
x_test
,
y_test
)],
eval_metric
=
'binary_logloss'
)
from
sklearn.datasets
import
load_digits
digits
=
load_digits
(
2
)
y
=
digits
[
'target'
]
X
=
digits
[
'data'
]
x_train
,
x_test
,
y_train
,
y_test
=
model_selection
.
train_test_split
(
X
,
y
,
test_size
=
0.2
)
lgb_model
=
lgb
.
LGBMClassifier
(
objective
=
logregobj
).
fit
(
x_train
,
y_train
,
eval_set
=
[[
x_train
,
y_train
],(
x_test
,
y_test
)],
eval_metric
=
'binary_logloss'
)
preds
=
lgb_model
.
predict
(
x_test
)
err
=
sum
(
1
for
i
in
range
(
len
(
preds
))
if
int
(
preds
[
i
]
>
0.5
)
!=
y_test
[
i
])
/
float
(
len
(
preds
))
assert
err
<
0.1
test_binary_classification
()
test_multiclass_classification
()
test_regression
()
test_regression_with_custom_objective
()
test_binary_classification_with_custom_objective
()
\ No newline at end of file
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