hipdf.Series.sample#
3 min read time
- Series.sample(n=None, frac=None, replace=False, weights=None, random_state=None, axis=None, ignore_index=False)#
Return a random sample of items from an axis of object.
If reproducible results are required, a random number generator may be provided via the random_state parameter. This function will always produce the same sample given an identical random_state.
Parameters#
- nint, optional
Number of items from axis to return. Cannot be used with frac. Default = 1 if frac = None.
- fracfloat, optional
Fraction of axis items to return. Cannot be used with n.
- replacebool, default False
Allow or disallow sampling of the same row more than once. replace == True is not supported for axis = 1/”columns”. replace == False is not supported for axis = 0/”index” given random_state is None or a cupy random state, and weights is specified.
- weightsndarray-like, optional
Default None for uniform probability distribution over rows to sample from. If ndarray is passed, the length of weights should equal to the number of rows to sample from, and will be normalized to have a sum of 1. Unlike pandas, index alignment is not currently not performed.
- random_stateint, numpy/cupy RandomState, or None, default None
If None, default cupy random state is chosen. If int, the seed for the default cupy random state. If RandomState, rows-to-sample are generated from the RandomState.
- axis{0 or index, 1 or columns, None}, default None
Axis to sample. Accepts axis number or name. Default is stat axis for given data type (0 for Series and DataFrames). Series doesn’t support axis=1.
- ignore_indexbool, default False
If True, the resulting index will be labeled 0, 1, …, n - 1.
Returns#
- Series or DataFrame
A new object of same type as caller containing n items randomly sampled from the caller object.
Examples#
>>> import cudf >>> df = cudf.DataFrame({"a":{1, 2, 3, 4, 5}}) >>> df.sample(3, random_state=0) a 0 1 1 2 4 5
>>> sr = cudf.Series([1, 2, 3, 4, 5]) >>> sr.sample(10, replace=True, random_state=0) 1 2 4 5 1 2 2 3 4 5 1 2 4 5 2 3 4 5 1 2 dtype: int64
>>> df = cudf.DataFrame( ... {"a": [1, 2], "b": [2, 3], "c": [3, 4], "d": [4, 5]} ... ) >>> df.sample(2, axis=1, random_state=0) c d 0 3 4 1 4 5
Note
The two
random_state=0examples above that sample alongaxis=0are excluded from doctest verification (# doctest: +SKIP) because their output is not portable across GPU vendors. When samplingaxis=0with an integer seed, the rows to draw are generated withcupy.random.RandomState, which is backed by the platform GPU RNG library (cuRAND on NVIDIA, rocRAND on AMD). These libraries emit different pseudo-random sequences for the same seed, so the specific sampled rows differ by platform even though the sampling itself is correct and fully reproducible on any single platform. (Theaxis=1example is verified because column sampling uses NumPy on the host, which is deterministic and matches pandas.)