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
ef778069
Commit
ef778069
authored
Mar 01, 2017
by
Guolin Ke
Browse files
Add categorical feature support back.
parent
d93eb338
Changes
41
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
0 deletions
+43
-0
tests/python_package_test/test_engine.py
tests/python_package_test/test_engine.py
+43
-0
No files found.
tests/python_package_test/test_engine.py
View file @
ef778069
...
...
@@ -153,6 +153,49 @@ class TestEngine(unittest.TestCase):
for
ret
in
other_ret
:
self
.
assertAlmostEqual
(
ret_origin
,
ret
,
places
=
5
)
@
unittest
.
skipIf
(
not
IS_PANDAS_INSTALLED
,
'pandas not installed'
)
def
test_pandas_categorical
(
self
):
X
=
pd
.
DataFrame
({
"A"
:
np
.
random
.
permutation
([
'a'
,
'b'
,
'c'
,
'd'
]
*
75
),
# str
"B"
:
np
.
random
.
permutation
([
1
,
2
,
3
]
*
100
),
# int
"C"
:
np
.
random
.
permutation
([
0.1
,
0.2
,
-
0.1
,
-
0.1
,
0.2
]
*
60
),
# float
"D"
:
np
.
random
.
permutation
([
True
,
False
]
*
150
)})
# bool
y
=
np
.
random
.
permutation
([
0
,
1
]
*
150
)
X_test
=
pd
.
DataFrame
({
"A"
:
np
.
random
.
permutation
([
'a'
,
'b'
,
'e'
]
*
20
),
"B"
:
np
.
random
.
permutation
([
1
,
3
]
*
30
),
"C"
:
np
.
random
.
permutation
([
0.1
,
-
0.1
,
0.2
,
0.2
]
*
15
),
"D"
:
np
.
random
.
permutation
([
True
,
False
]
*
30
)})
for
col
in
[
"A"
,
"B"
,
"C"
,
"D"
]:
X
[
col
]
=
X
[
col
].
astype
(
'category'
)
X_test
[
col
]
=
X_test
[
col
].
astype
(
'category'
)
params
=
{
'objective'
:
'binary'
,
'metric'
:
'binary_logloss'
,
'verbose'
:
-
1
}
lgb_train
=
lgb
.
Dataset
(
X
,
y
)
gbm0
=
lgb
.
train
(
params
,
lgb_train
,
num_boost_round
=
10
,
verbose_eval
=
False
)
pred0
=
list
(
gbm0
.
predict
(
X_test
))
lgb_train
=
lgb
.
Dataset
(
X
,
y
)
gbm1
=
lgb
.
train
(
params
,
lgb_train
,
num_boost_round
=
10
,
verbose_eval
=
False
,
categorical_feature
=
[
0
])
pred1
=
list
(
gbm1
.
predict
(
X_test
))
lgb_train
=
lgb
.
Dataset
(
X
,
y
)
gbm2
=
lgb
.
train
(
params
,
lgb_train
,
num_boost_round
=
10
,
verbose_eval
=
False
,
categorical_feature
=
[
'A'
])
pred2
=
list
(
gbm2
.
predict
(
X_test
))
lgb_train
=
lgb
.
Dataset
(
X
,
y
)
gbm3
=
lgb
.
train
(
params
,
lgb_train
,
num_boost_round
=
10
,
verbose_eval
=
False
,
categorical_feature
=
[
'A'
,
'B'
,
'C'
,
'D'
])
pred3
=
list
(
gbm3
.
predict
(
X_test
))
lgb_train
=
lgb
.
Dataset
(
X
,
y
)
gbm3
.
save_model
(
'categorical.model'
)
gbm4
=
lgb
.
Booster
(
model_file
=
'categorical.model'
)
pred4
=
list
(
gbm4
.
predict
(
X_test
))
self
.
assertListEqual
(
pred0
,
pred1
)
self
.
assertListEqual
(
pred0
,
pred2
)
self
.
assertListEqual
(
pred0
,
pred3
)
self
.
assertListEqual
(
pred0
,
pred4
)
print
(
"----------------------------------------------------------------------"
)
print
(
"running test_engine.py"
)
...
...
Prev
1
2
3
Next
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