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
1090a93b
Unverified
Commit
1090a93b
authored
Dec 14, 2024
by
José Morales
Committed by
GitHub
Dec 14, 2024
Browse files
[python-package] do not copy column-major numpy arrays when predicting (#6751)
parent
b33a12ea
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
5 deletions
+17
-5
python-package/lightgbm/basic.py
python-package/lightgbm/basic.py
+2
-5
tests/python_package_test/test_engine.py
tests/python_package_test/test_engine.py
+15
-0
No files found.
python-package/lightgbm/basic.py
View file @
1090a93b
...
...
@@ -1291,10 +1291,7 @@ class _InnerPredictor:
predict_type
:
int
,
preds
:
Optional
[
np
.
ndarray
],
)
->
Tuple
[
np
.
ndarray
,
int
]:
if
mat
.
dtype
==
np
.
float32
or
mat
.
dtype
==
np
.
float64
:
data
=
np
.
asarray
(
mat
.
reshape
(
mat
.
size
),
dtype
=
mat
.
dtype
)
else
:
# change non-float data to float data, need to copy
data
=
np
.
array
(
mat
.
reshape
(
mat
.
size
),
dtype
=
np
.
float32
)
data
,
layout
=
_np2d_to_np1d
(
mat
)
ptr_data
,
type_ptr_data
,
_
=
_c_float_array
(
data
)
n_preds
=
self
.
__get_num_preds
(
start_iteration
=
start_iteration
,
...
...
@@ -1314,7 +1311,7 @@ class _InnerPredictor:
ctypes
.
c_int
(
type_ptr_data
),
ctypes
.
c_int32
(
mat
.
shape
[
0
]),
ctypes
.
c_int32
(
mat
.
shape
[
1
]),
ctypes
.
c_int
(
_C_API_IS_ROW_MAJOR
),
ctypes
.
c_int
(
layout
),
ctypes
.
c_int
(
predict_type
),
ctypes
.
c_int
(
start_iteration
),
ctypes
.
c_int
(
num_iteration
),
...
...
tests/python_package_test/test_engine.py
View file @
1090a93b
...
...
@@ -4611,3 +4611,18 @@ def test_bagging_by_query_in_lambdarank():
ndcg_score_no_bagging_by_query
=
gbm_no_bagging_by_query
.
best_score
[
"valid_0"
][
"ndcg@5"
]
assert
ndcg_score_bagging_by_query
>=
ndcg_score
-
0.1
assert
ndcg_score_no_bagging_by_query
>=
ndcg_score
-
0.1
def
test_equal_predict_from_row_major_and_col_major_data
():
X_row
,
y
=
make_synthetic_regression
()
assert
X_row
.
flags
[
"C_CONTIGUOUS"
]
and
not
X_row
.
flags
[
"F_CONTIGUOUS"
]
ds
=
lgb
.
Dataset
(
X_row
,
y
)
params
=
{
"num_leaves"
:
8
,
"verbose"
:
-
1
}
bst
=
lgb
.
train
(
params
,
ds
,
num_boost_round
=
5
)
preds_row
=
bst
.
predict
(
X_row
)
X_col
=
np
.
asfortranarray
(
X_row
)
assert
X_col
.
flags
[
"F_CONTIGUOUS"
]
and
not
X_col
.
flags
[
"C_CONTIGUOUS"
]
preds_col
=
bst
.
predict
(
X_col
)
np
.
testing
.
assert_allclose
(
preds_row
,
preds_col
)
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