@@ -275,29 +275,18 @@ function deserialize(::Type{Booster}, buf::AbstractVector{UInt8}, data=DMatrix[]
275
275
deserialize! (b, buf)
276
276
end
277
277
278
- # sadly this is type unstable because we might return a transpose
279
278
"""
280
- predict (b::Booster, data; margin=false, training=false, ntree_limit=0 )
279
+ predict_nocopy (b::Booster, data; kw... )
281
280
282
- Use the model `b` to run predictions on `data`. This will return a `Vector{Float32}` which can be compared
283
- to training or test target data.
284
-
285
- If `ntree_limit > 0` only the first `ntree_limit` trees will be used in prediction.
286
-
287
- ## Examples
288
- ```julia
289
- (X, y) = (randn(100,3), randn(100))
290
- b = xgboost((X, y), 10)
291
-
292
- ŷ = predict(b, X)
293
- ```
281
+ Same as [`predict`](@ref), but the output array is not copied. Data in the array output
282
+ by this function may be overwritten by future calls to `predict_nocopy` or `predict`.
294
283
"""
295
- function predict (b:: Booster , Xy:: DMatrix ;
296
- margin:: Bool = false , # whether to output margin
297
- training:: Bool = false ,
298
- ntree_lower_limit:: Integer = 0 ,
299
- ntree_limit:: Integer = 0 , # 0 corresponds to no limit
300
- )
284
+ function predict_nocopy (b:: Booster , Xy:: DMatrix ;
285
+ margin:: Bool = false , # whether to output margin
286
+ training:: Bool = false ,
287
+ ntree_lower_limit:: Integer = 0 ,
288
+ ntree_limit:: Integer = 0 , # 0 corresponds to no limit
289
+ )
301
290
opts = Dict (" type" => (margin ? 1 : 0 ),
302
291
" iteration_begin" => ntree_lower_limit,
303
292
" iteration_end" => ntree_limit,
@@ -309,9 +298,31 @@ function predict(b::Booster, Xy::DMatrix;
309
298
o = Ref {Ptr{Cfloat}} ()
310
299
xgbcall (XGBoosterPredictFromDMatrix, b. handle, Xy. handle, opts, oshape, odim, o)
311
300
dims = reverse (unsafe_wrap (Array, oshape[], odim[]))
301
+ # this `copy` is needed because libxgboost re-uses the pointer
312
302
o = unsafe_wrap (Array, o[], tuple (dims... ))
313
- length (dims) > 1 ? transpose (o) : o
303
+ length (dims) > 1 ? permutedims (o) : o
314
304
end
305
+
306
+ predict_nocopy (b:: Booster , Xy; kw... ) = predict_nocopy (b, DMatrix (Xy); kw... )
307
+
308
+ """
309
+ predict(b::Booster, data; margin=false, training=false, ntree_limit=0)
310
+
311
+ Use the model `b` to run predictions on `data`. This will return a `Vector{Float32}` which can be compared
312
+ to training or test target data.
313
+
314
+ If `ntree_limit > 0` only the first `ntree_limit` trees will be used in prediction.
315
+
316
+ ## Examples
317
+ ```julia
318
+ (X, y) = (randn(100,3), randn(100))
319
+ b = xgboost((X, y), 10)
320
+
321
+ ŷ = predict(b, X)
322
+ ```
323
+ """
324
+ predict (b:: Booster , Xy:: DMatrix ; kw... ) = copy (predict_nocopy (b, Xy; kw... ))
325
+
315
326
predict (b:: Booster , Xy; kw... ) = predict (b, DMatrix (Xy); kw... )
316
327
317
328
function evaliter (b:: Booster , watch, n:: Integer = 1 )
0 commit comments