gen_even_slices#

sklearn.utils.gen_even_slices(n,n_packs,*,n_samples=None)[source]#

Generator to createn_packs evenly spaced slices going up ton.

Ifn_packs does not dividen, except for the firstn%n_packsslices, remaining slices may contain fewer elements.

Parameters:
nint

Size of the sequence.

n_packsint

Number of slices to generate.

n_samplesint, default=None

Number of samples. Passn_samples when the slices are to be used forsparse matrix indexing; slicing off-the-end raises an exception, whileit works for NumPy arrays.

Yields:
slice representing a set of indices from 0 to n.

See also

gen_batches

Generator to create slices containing batch_size elements from 0 to n.

Examples

>>>fromsklearn.utilsimportgen_even_slices>>>list(gen_even_slices(10,1))[slice(0, 10, None)]>>>list(gen_even_slices(10,10))[slice(0, 1, None), slice(1, 2, None), ..., slice(9, 10, None)]>>>list(gen_even_slices(10,5))[slice(0, 2, None), slice(2, 4, None), ..., slice(8, 10, None)]>>>list(gen_even_slices(10,3))[slice(0, 4, None), slice(4, 7, None), slice(7, 10, None)]

Gallery examples#

Poisson regression and non-normal loss

Poisson regression and non-normal loss