{"id":26205,"date":"2021-12-17T08:43:14","date_gmt":"2021-12-17T03:13:14","guid":{"rendered":"https:\/\/python-programs.com\/?p=26205"},"modified":"2021-12-17T08:43:14","modified_gmt":"2021-12-17T03:13:14","slug":"python-program-for-sample-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-sample-function-with-examples\/","title":{"rendered":"Python Program for sample() Function with Examples"},"content":{"rendered":"
When handling problems involving data prediction, we frequently encounter scenarios in which we must test the algorithm on a small set of data to evaluate the method’s accuracy.<\/p>\n
This is where the Python sample() function comes into play.<\/p>\n
For operations, we may use the sample() method to select a random sample from the available data. Though there are other strategies for sampling data, the sample() method is widely regarded as one of the most simple.<\/p>\n
Python’s sample() method works with all sorts of iterables, including list, tuple, sets, dataframes, and so on. It selects data from the iterable at random from the user-specified number of data values.<\/p>\n
sample() Function:<\/strong><\/p>\n sample() is a built-in function of Python’s random module that returns a specific length list of items taken from a sequence, such as a list, tuple, string, or set. Used for random sampling Non-replacement.<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n sequence:<\/strong> It may be a list, tuple, string, or set, etc.<\/p>\n k:<\/strong> It is an Integer. This is the length of a sample.<\/p>\n Return Value:<\/strong><\/p>\n Returns a new list of k elements selected from the sequence.<\/p>\n Examples:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n When working with the sample() function, we may see a ValueError exception. This exception is thrown if the sample length\u00a0is greater than the length of the iterable.<\/p>\n For Example:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" When handling problems involving data prediction, we frequently encounter scenarios in which we must test the algorithm on a small set of data to evaluate the method’s accuracy. This is where the Python sample() function comes into play. For operations, we may use the sample() method to select a random sample from the available data. …<\/p>\nrandom.sample(sequence, k)<\/pre>\n
1)For Lists<\/strong><\/h5>\n
\n
# Import sample() function from the random module using the import keyword.\r\nfrom random import sample\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [24, 4, 5, 9, 2, 1]\r\n# Give the length of the sample as static input and store it in another variable.\r\nlen_sampl = 3\r\n# Pass the given list, sample length as the arguments to the sample() method\r\n# to get given length(len_sampl) of random sample items from the list\r\n# Store it in another variable.\r\nrslt = sample(gvn_lst, len_sampl)\r\n# Print the above result.\r\nprint(\"The\", len_sampl, \"random items from the given list are:\")\r\nprint(rslt)\r\n<\/pre>\n
The 3 random items from the given list are:\r\n[9, 24, 4]<\/pre>\n
2)For Sets<\/strong><\/h5>\n
\n
# Import sample() function from random module using the import keyword.\r\nfrom random import sample\r\n# Give the set as static input and store it in a variable.\r\ngvn_set = {2, 4, 7, 1, 3, 1, 4, 2}\r\n# Give the length of sample as static input and store it in another variable.\r\nlen_sampl = 4\r\n# Pass the given set, sample length as the arguments to the sample() method\r\n# to get given length(len_sampl) of random sample items from the set\r\n# Store it in another variable.\r\nrslt = sample(gvn_set, len_sampl)\r\n# Print the above result.\r\nprint(\"The\", len_sampl, \"random items from the given set are:\")\r\nprint(rslt)\r\n<\/pre>\n
The 4 random items from the given set are:\r\n[1, 4, 7, 2]<\/pre>\n
Exceptions & Errors While using the sample() Function<\/strong><\/h5>\n
# Import sample() function from the random module using the import keyword.\r\nfrom random import sample\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [24, 4, 5, 9, 2, 1]\r\n# Give the length of the sample as static input and store it in another variable.\r\nlen_sampl = 10\r\n# Pass the given list, sample length as the arguments to the sample() method\r\n# to get given length(len_sampl) of random sample items from the list\r\n# Store it in another variable.\r\nrslt = sample(gvn_lst, len_sampl)\r\n# Print the above result.\r\nprint(\"The\", len_sampl, \"random items from the given list are:\")\r\nprint(rslt)\r\n<\/pre>\n
Traceback (most recent call last):\r\n File \"\/home\/f39024cad259bfac4456fa81ec86fb82.py\", line 10, in <module>\r\n rslt = sample(gvn_lst, len_sampl)\r\n File \"\/usr\/lib\/python3.6\/random.py\", line 320, in sample\r\n raise ValueError(\"Sample larger than population or is negative\")\r\nValueError: Sample larger than population or is negative<\/pre>\n