<jupyter_start><jupyter_text>Sampling<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

file_name = "https://raw.githubusercontent.com/rajeevratan84/datascienceforbusiness/master/winequalityN.csv"
df = pd.read_csv(file_name)
df.head()
df.shape
import pandas as pd

sample_means = []
population_mean = df['alcohol'].mean()
sample_size = 100

for i in range(sample_size):
    # we use the sample function to take n samples, random state is a seed parameter to generate pseudo randomn numbers
    sample = df['alcohol'].sample(sample_size, random_state=i)
    sample_means.append(sample.mean())

plt.scatter(range(1,sample_size+1), sample_means)

# Plots our actual mean
plt.axhline(population_mean, color='red')

# Plots the mean of our sample size
sample_size_mean = np.array(sample_means).mean()
plt.axhline(sample_size_mean)

print("Actual Mean: %2f" % (population_mean))
print("Sample Mean: %2f" % (sample_size_mean))
print("Sample Error: %2f" % (population_mean-sample_size_mean))<jupyter_output>Actual Mean: 10.491801
Sample Mean: 10.500091
Sample Error: -0.008291<jupyter_text>Stratifying SamplingTaking the average alcohol content of wines based on our sample can be misleading because our dataset contains both red and white wines, and when looking at the proportion, it contains far more white wines.<jupyter_code>df['type'].value_counts()
# We need to stratify the data

stratum_white = df[df.type == 'white']
stratum_red = df[df.type == 'red']
len(stratum_white)
alcohol_per_wine_type = {}

for stratum, wine_type in [(stratum_white, 'white'), (stratum_red, 'red')]:
  sample = stratum['alcohol'].sample(250, random_state = 0) # random sampling on each stratum
  alcohol_per_wine_type[wine_type] = sample.mean()

print(alcohol_per_wine_type)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10826985854189336, 'fraction_numerical': 0.02013057671381937, 'mean_word_length': 6.356, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '86463', 'license': 'MIT', 'n_tokens_mistral': 664, 'n_tokens_neox': 613, 'n_words': 166, 'path': 'notebooks/5.1 Statistics 2 - Sampling.ipynb', 'repo_name': 'sparsh-ai/Data-Science-Analytics-AI-for-Business-the-Real-World-', 'size': '1838'}