Built with Tutify.io

    AIStudyLabs

    Polars Series sample() Function with Examples

    Polars Series sample() Function with Examples

    In Polars, the sample() method on a Series is used to randomly select a subset of elements from that Series. It functions much like sampling methods in other data libraries, such as Pandas, but is optimized for speed and efficiency. With sample(), you can specify either an exact number of items or a fraction of the Series to select at random. Additionally, you have the option to enable sampling with replacement, which permits the same element to be selected multiple times.

    In this article, I will explain the Polars Series sample() method, covering its syntax, parameters, and usage, and explain how it returns a new Series containing randomly selected elements from the original one.

    Key Points –

    <a id="h-polars-series-sample-introduction"></a>

    Polars Series sample() Introduction

    Let’s know the syntax of the series sample() method.

    
    # Syntax of sample()
    Series.sample(
        n: int | None = None,
        *,
        fraction: float | None = None,
        with_replacement: bool = False,
        shuffle: bool = False,
        seed: int | None = None,
    ) → Series
    

    <a id="h-parameters-of-the-series-sample"></a>

    Parameters of the Series sample()

    Following are the parameters of the series sample() method.

    <a id="h-return-value"></a>

    Return Value

    This function returns a new Polars Series containing the randomly sampled elements from the original Series.

    <a id="h-usage-of-polars-series-sample-function"></a>

    Usage of Polars Series sample() Function

    The sample() function in a Polars Series is used to randomly extract a subset of elements, either by specifying a fixed number or a fraction of the total. While similar to sampling features in other libraries like Pandas, this method is optimized specifically for Polars.

    First, let’s create a Polars Series.

    
    import polars as pl
    
    ser = pl.Series("numbers", [10, 20, 30, 40, 50, 60])
    print("Original Series:\n", ser)
    

    Yields below output.

    To randomly select one element from a Polars Series using the default behavior of sample(), simply call the method without specifying n. By default, sample() returns a single random element when n is not provided.

    
    # Sample 1 random element (default)
    ser2 = ser.sample()
    print("Sampled 1 random element:\n", ser2)
    

    Here,

    <a id="h-sample-3-random-elements-without-replacement"></a>

    Sample 3 Random Elements (Without Replacement)

    To randomly select 3 elements without replacement from your Polars Series, set n=3 in the sample() function. This will return 3 unique random elements from the Series.

    
    # Sample 3 random elements without replacement (default behavior)
    ser2 = ser.sample(n=3)
    print("Sampled 3 random elements:\n", ser2)
    
    # Output:
    # Sampled 3 random elements:
    # shape: (3,)
    # Series: 'numbers' [i64]
    #[
    #	10
    #	60
    #	50
    #]
    

    Here,

    <a id="h-sample-60-of-the-series-using-fraction"></a>

    Sample 60% of the Series using Fraction

    To sample 60% of a Polars Series using a fraction, you can use the fraction parameter in the sample() method.

    
    # Sample 60% of the Series
    ser2 = ser.sample(fraction=0.6)
    print("Sampled 60% of the Series:\n", ser2)
    
    # Output:
    # Sampled 60% of the Series:
    # shape: (3,)
    # Series: 'numbers' [i64]
    # [
    #	20
    #	60
    #	30
    # ]
    

    Here,

    <a id="h-sample-2-elements-with-a-fixed-seed-for-reproducibility"></a>

    Sample 2 Elements with a Fixed Seed for Reproducibility

    To sample 2 elements with a fixed seed for reproducibility, use the seed parameter in the sample() function.

    
    # Sample 2 elements with a fixed seed
    ser2 = ser.sample(n=2, seed=42)
    print("Sampled 2 elements with seed=42:\n", ser2)
    
    # Output:
    # Sampled 2 elements with seed=42:
    # shape: (2,)
    # Series: 'numbers' [i64]
    # [
    #	20
    #	50
    # ]
    

    Here,

    <a id="h-sample-4-elements-and-shuffle-the-result"></a>

    Sample 4 Elements and Shuffle the Result

    To sample 4 elements and shuffle the results in a Polars Series, use the sample() method with the shuffle=True parameter. Since sample() already returns elements in random order, sampling 4 elements naturally produces a shuffled subset.

    
    # Sample 4 elements and shuffle the result
    ser2 = ser.sample(n=4, shuffle=True)
    print("Sampled 4 elements and shuffled:\n", ser2)
    
    # Output:
    # Sampled 4 elements and shuffled:
    # shape: (4,)
    # Series: 'numbers' [i64]
    # [
    #	40
    #	20
    #	60
    #	30
    # ]
    

    Here,

    <a id="h-sample-80-of-the-series-with-replacement-and-shuffled"></a>

    Sample 80% of the Series, with Replacement, and Shuffled

    To sample 80% of the Series with replacement and shuffle the result, you can use the fraction, with_replacement=True, and shuffle=True parameters in the sample() method.

    
    # Sample 80% with replacement and shuffle
    ser2 = ser.sample(fraction=0.8, with_replacement=True, shuffle=True)
    print("Sampled 80% with replacement and shuffled:\n", ser2)
    
    # Output:
    # Sampled 80% with replacement and shuffled:
    # shape: (4,)
    # Series: 'numbers' [i64]
    # [
    #	10
    #	30
    #	30
    #	40
    # ]
    

    Here,

    <a id="h-conclusion"></a>

    Conclusion

    In conclusion, the sample() method in Polars Series is a powerful and flexible tool for randomly selecting data. Whether you need a fixed number of elements, a percentage of the Series, or want to sample with replacement and shuffle the output, sample() makes it easy and efficient.

    Happy Learning!!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-reference"></a>

    Reference