<jupyter_start><jupyter_text>To start working on this notebook, or any other notebook that we will use in the Moringa Data Science Course, we will need to save our own copy of it. We can do this by clicking File > Save a Copy in Drive. We will then be able to make edits to our own copy of this notebook.  Python Programming:  Simple Random Sampling  Importing our Libraries<jupyter_code># importing numpy
import numpy as np

# importing pandas
import pandas as pd

# importing random module
# we will use random.sample() function to return a k length 
# list of unique elements chosen from our population
import random<jupyter_output><empty_output><jupyter_text>1.0 Selecting More than One item from a List<jupyter_code># Example 1
# ---
# Selecting more than one item from a list 
# ---
#

list = [20, 40, 80, 100, 120]
print ("Choosing 3 random items from a list using sample function", random.sample(list, k=3))

list = [20, 40, 20, 20, 20]
print ("Choosing 3 random items from a list using sample function",random.sample(list, k=3))

# As you can see we pass k=3 to choose 3 random elements from a list. 
# In return, we got a list of 3 random items.<jupyter_output><empty_output><jupyter_text>1.0 Challenges<jupyter_code># Challenge 1
# ---
# Question: Create a list containing 10 counties in Kenya 
# then select 2 random counties from the list.
# ---
# 
OUR CODE GOES HERE<jupyter_output><empty_output><jupyter_text>2.0  Selecting the Random Sample from Dictionary<jupyter_code># Example 1
# ---
# We can select a random key-value pair from the dictionary as shown 
# ---
#
marks_dict = {
  "Kelly": 55,
  "jhon": 70,
  "Donald": 60,
  "Lennin" :50
}

print ("Choosing 2 random items from a dictionary using sample method ", random.sample(marks_dict.items(), k=2))<jupyter_output><empty_output><jupyter_text>2.0 Challenges<jupyter_code># Challenge 1
# ---
# A caterer offers three different food packages; 
# bronze package: starch:rice stew:chicken curry vegetable:cabbages desert:melon
# silver package: starch:rice and chappati  stew:beef curry vegetable:spinach desert:melon and bananas
# gold package: starch:rice, chappati and mukimo stew: beef curry vegetable:spinach and managu desert: tart
# Create a dictionary that stores the above information and select a sample of size 2 
# ---
#
OUR CODE GOES HERE<jupyter_output><empty_output><jupyter_text>3.0 Selecting a Random Sample with Replacement to including repetition<jupyter_code># Example 1
# ---
# We can select a random sample with replacement to include repetition as shown below
# ---
# 
names = ["Roger", "Nadal", "Novac", "Andre", "Sarena", "Mariya", "Martina"]

print("random sample with replacement to including repetition ", random.choices(names, k=3))<jupyter_output><empty_output><jupyter_text>3.0 Challenges<jupyter_code># Challenge 1
# ---
# Question: Select a random sample of 3 with replacement to include replacement from the 
# following list of months
# ---
#

months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
          'August', 'September', 'October', 'November', 'December']

OUR CODE GOES HERE<jupyter_output><empty_output><jupyter_text>4.0 Selecting a Random Sample from a Range of Integers<jupyter_code># Example 1
# ---
# We can select a random sample from a range of integers as shown below
# ---
# 

weight_set = {40, 50, 55, 65, 75, 80}

print ("Choosing 4 random items from a set ", random.sample(weight_set, k=4))<jupyter_output><empty_output><jupyter_text>4.1 Challenges<jupyter_code># Challenge 1
# ---
# Question: Select a random sample from a range of integers as shown below
# --- 
# 
weight_set = {4, 5, 6, 7, 8, 9, 10}

OUR CODE GOES HERE<jupyter_output><empty_output><jupyter_text>5.0 Selecting a Sample Array from a Large multidimensional Array<jupyter_code># Example 1
# ---
# We can select a sample array from a large multidimensional array as shown
# ---
# 

array = np.array([[2 ,4, 6], [5, 10, 15], [6, 12, 18], [7, 14, 21], [8, 16, 24]]) 
print("Printing 2D Array")
print(array)

# selecting multiple random rows from the 2D as shown
print("Selecting multiple random rows from the 2D array")
randomRows = np.random.randint(5, size=2)
for i in randomRows:
  print(array[i,:])<jupyter_output><empty_output><jupyter_text>5.0 Challenges<jupyter_code># Challenge 1
# ---
# Question: Select two arrays from the given multidimensional array
# ---
# 

a = [[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20], [1 ,2 , 4, 5, 6], [5, 10, 15, 20, 25], [6, 12, 18, 24, 30], [7, 14, 21, 28, 35]]  

OUR CODE GOES HERE<jupyter_output><empty_output><jupyter_text>6.0 Selecting Random Samples from a Dataset<jupyter_code># Example 1
# ---
# Selecting a random sample from the following medical appointments dataset
# ---
# Dataset url = http://bit.ly/MedicalAppointmentsDataset
# ---
# 

# Loading the dataset
medical_df = pd.read_csv("http://bit.ly/MedicalAppointmentsDataset")

# Determining the size
print(medical_df.shape)


# Previewing the dataset
print(medical_df.head())

# Selecting the sample 
medical_sample = medical_df.sample(n = 10, replace="False")
print(medical_sample)<jupyter_output><empty_output><jupyter_text>6.0 Challenges<jupyter_code># Challenge 1
# ---
# Select a random sample of 10 from the given dataset below 
# ---
# Dataset url = http://bit.ly/DSRussianInvestigation
# ---
#
OUR CODE GOES HERE
# Challenge 2
# ---
# A medical company would like to follow up with 50 patients who never showed up for appointments 
# but received an sms. Select a random sample from such patients given the medical dataset below 
# ---
# Dataset url = http://bit.ly/MedicalAppointmentsDataset
# ---
#
OUR CODE GOES HERE<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11411992263056092, 'fraction_numerical': 0.033585370142430106, 'mean_word_length': 4.587426326129666, 'pattern_counts': {'":': 3, '<': 53, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '233450', 'license': 'MIT', 'n_tokens_mistral': 2068, 'n_tokens_neox': 1836, 'n_words': 683, 'path': '20191209_Python_Programming_Simple_Random_Sampling.ipynb', 'repo_name': 'Dogechi/Core_exersices', 'size': '5687'}