Understanding the Python sample() function

Python Sample() Function

Hello, readers! In this article, we will be focusing on the Python sample() function and its importance in the domain of data science.

So, let us get started!


What is the Python sample() method?

Let us first understand the existence of sample() method in the industry of Data science.

While solving problems with respect to the prediction of data, we often come across situations wherein we need to test the algorithm on a handful of data to estimate the accuracy of the algorithm applied.

This is when Python sample() method comes into picture.

The sample() method lets us pick a random sample from the available data for operations. Though, there are lot of techniques to sample the data, sample() method is considered as one of the easiest of its kind.

Python sample() method works will all the types of iterables such as list, tuple, sets, dataframe, etc. It randomly selects data from the iterable through the user defined number of data values.

Let us now understand the structure of the same in the below section.


Syntax of sample() method

Have a look at the below syntax!

Syntax:

sample(iterable, sample_amt)

We need to provide the function with the sample amount that we want the function to randomly pick from the provided iterable or data structure.


1. Python sample() with list

In this section, we have implemented sample() function alongside a Python list and have selected 4 samples out of the data randomly using the function.

Example:

from random import sample 

lst = [10,20,40,30,50,46,89] 
res = sample(lst, 4)
print(res) 

Output:

[20, 89, 40, 46]

2. Python sample() with set

Here, we have created a Python set using alphabets as well as numeric values. Further, we have applied sample() function on the set and selected 4 values at random.

Example:

from random import sample 

set_sample = {1,2,3,4,5,"s","a","f","a"}
res = sample(set_sample, 4)
print(res) 

Output:

['f', 1, 5, 2]

Error and Exceptions with sample() function

While dealing with the sample() function, we can come across a ValueError exception. If we provide the sample_amt as a value that is greater than the total count of data values present in the iterable, this exception is invoked.

Example:

from random import sample 

set_sample = {1,2,3,4,5,"s","a","f","a"}
res = sample(set_sample, 15)
print(res) 

Output:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-284724c4b7db> in <module>
      2 
      3 set_sample = {1,2,3,4,5,"s","a","f","a"}
----> 4 res = sample(set_sample, 15)
      5 print(res)

c:\users\hp\appdata\local\programs\python\python36\lib\random.py in sample(self, population, k)
    316         n = len(population)
    317         if not 0 <= k <= n:
--> 318             raise ValueError("Sample larger than population or is negative")
    319         result = [None] * k
    320         setsize = 21        # size of a small set minus size of an empty list

ValueError: Sample larger than population or is negative

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to Python, Stay tuned and till then, Happy Learning! 🙂