Sometimes, while working with Machine Learning Algorithm, we can have problem in which we need to differentiate the training and testing data randomly. This is very common problem and solution to it is desirable for Machine Learning domains. This article discusses approach to solve this without using external libraries.
Method : Using keys() + random.randint() + computations
This problem can be solved by using combination of above functions. In this, we perform the task of extraction of random keys using randint(), from the keys extracted using keys(). The logical computations are performed for getting the separated test and training data.
# Python3 code to demonstrate working of # Random Sample Training and Test Data # Using keys() + randint() + computations import random # initializing dictionary test_dict = {'gfg' : 4, 'is' : 12, 'best' : 6, 'for' : 7, 'geeks' : 10} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # initializing ratio test = 40training = 60 # Random Sample Training and Test Data # Using keys() + randint() + computations key_list = list(test_dict.keys()) test_key_count = int((len(key_list) / 100) * test) test_keys = [random.choice(key_list) for ele in range(test_key_count)] train_keys = [ele for ele in key_list if ele not in test_keys] testing_dict = dict((key, test_dict[key]) for key in test_keys if key in test_dict) training_dict = dict((key, test_dict[key]) for key in train_keys if key in test_dict) # printing result print("The testing dictionary is : " + str(testing_dict)) print("The training dictionary is : " + str(training_dict)) |
The original dictionary is : {‘is’: 12, ‘gfg’: 4, ‘best’: 6, ‘for’: 7, ‘geeks’: 10}
The testing dictionary is : {‘is’: 12, ‘for’: 7}
The training dictionary is : {‘gfg’: 4, ‘best’: 6, ‘geeks’: 10}
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Implementing Artificial Neural Network training process in Python
- Python | Pokémon Training Game
- NLP | Training a tokenizer and filtering stopwords in a sentence
- NLP | Training Unigram Tagger
- NLP | Training Tagger Based Chunker | Set 1
- NLP | Training Tagger Based Chunker | Set 2
- NLP | Named Entity Chunker Training
- ML | Training Image Classifier using Tensorflow Object Detection API
- Visualizing training with TensorBoard
- Python | random.sample() function
- Random sampling in numpy | sample() function
- random.random() function in Python
- Random sampling in numpy | random() function
- Python | Get random dictionary pair
- 3D Plotting sample Data from MongoDB Atlas Using Python
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert string dictionary to dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

