<jupyter_start><jupyter_text>Load Dataset Food servers’ tips in restaurants may be influenced by many factors, including the nature of the restaurant, size of the party, and table locations in the restaurant. Restaurant managers need to know which factors matter when they assign tables to food servers. For the sake of staff morale, they usually want to avoid either the substance or the appearance of unfair treatment of the servers, for whom tips (at least in restaurants in the United States) are a major component of pay. In one restaurant, a food server recorded the following data on all customers they served during an interval of two and a half months in early 1990. The restaurant, located in a suburban shopping mall, was part of a national chain and served a varied menu. In observance of local law, the restaurant offered to seat in a non-smoking section to patrons who requested it. Each record includes a day and time, and taken together, they show the server’s work schedule.<jupyter_code>df = pd.read_csv(r'..\tips.csv') 
df.info()
df.head()<jupyter_output><empty_output><jupyter_text>Question: Do customers give more tips during lunch/dinner time?<jupyter_code>sns.distplot(df.tip)
def plot_prob_density(df_lunch, df_dinner, field, x_start, x_end):
    plt.figure(figsize = (10, 7))

    unit = 1.5
    x = np.linspace(df_lunch.min() - unit, df_lunch.max() + unit, 1000)[:, np.newaxis]

    # Plot the data using a normalized histogram
    plt.hist(df_lunch, bins=10, density=True, label='Lunch Time', color='orange', alpha=0.2)
    plt.hist(df_dinner, bins=10, density=True, label='Dinner Time', color='navy', alpha=0.2)

    # Do kernel density estimation
    kd_lunch = KernelDensity(kernel='gaussian', bandwidth=0.5).fit(df_lunch)
    kd_dinner = KernelDensity(kernel='gaussian', bandwidth=0.5).fit(df_dinner)

    # Plot the estimated densty
    kd_vals_lunch = np.exp(kd_lunch.score_samples(x))
    kd_vals_dinner = np.exp(kd_dinner.score_samples(x))

    plt.plot(x, kd_vals_lunch, color='orange')
    plt.plot(x, kd_vals_dinner, color='navy')
    
    plt.axvline(x=x_start,color='red',linestyle='dashed')
    plt.axvline(x=x_end,color='red',linestyle='dashed')

    # Show the plots
    plt.xlabel(field, fontsize=15)
    plt.ylabel('Probability Density', fontsize=15)
    plt.legend(fontsize=15)
    plt.show()
    gc.collect()
    return kd_lunch, kd_dinner
def get_probability(start_value, end_value, eval_points, kd):
    
    # Number of evaluation points 
    N = eval_points                                      
    step = (end_value - start_value) / (N - 1)  # Step size

    x = np.linspace(start_value, end_value, N)[:, np.newaxis]  # Generate values in the range
    kd_vals = np.exp(kd.score_samples(x))  # Get PDF values for each x
    probability = np.sum(kd_vals * step)  # Approximate the integral of the PDF
    return probability.round(4)
plt.figure(figsize = (10, 7))
sns.distplot(df.tip[df.time == 'Dinner'], label='Dinner Time')
sns.distplot(df.tip[df.time == 'Lunch'], label='Lunch Time')
plt.xlabel('tip', fontsize=15)
plt.ylabel('Probability Density', fontsize=15)
plt.legend(fontsize=15)
plt.show()
df_lunch = df[df.time == 'Lunch']
df_dinner = df[df.time == 'Dinner']
field = 'tip'
data_lunch = np.array(df_lunch['%s'%field]).reshape(-1, 1)
data_dinner = np.array(df_dinner['%s'%field]).reshape(-1, 1)
kd_lunch, kd_dinner = plot_prob_density(data_lunch, data_dinner, field, x_start=1, x_end=3)
print('Probability of customers giving 1-3 USD tips during LUNCH time: {}\n'
      .format(get_probability(start_value = 1, 
                              end_value = 3, 
                              eval_points = 100, 
                              kd = kd_lunch)))
print('Probability of customers giving 1-3 USD tips during DINNER time: {}'
      .format(get_probability(start_value = 1, 
                              end_value = 3, 
                              eval_points = 100, 
                              kd = kd_dinner)))
kd_lunch, kd_dinner = plot_prob_density(data_lunch, data_dinner, field, x_start=3, x_end=5)
print('Probability of customers giving 3-5 USD tips during LUNCH time: {}\n'
      .format(get_probability(start_value = 3, 
              end_value = 5, 
              eval_points = 100, 
              kd = kd_lunch)))
print('Probability of customers giving 3-5 USD tips during DINNER time: {}'
      .format(get_probability(start_value = 3, 
              end_value = 5, 
              eval_points = 100, 
              kd = kd_dinner)))<jupyter_output>Probability of customers giving 3-5 USD tips during LUNCH time: 0.2653

Probability of customers giving 3-5 USD tips during DINNER time: 0.3792
{'fraction_non_alphanumeric': 0.10676080445014977, 'fraction_numerical': 0.019683354728284124, 'mean_word_length': 3.365079365079365, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '610233', 'license': 'MIT', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1516, 'n_words': 443, 'path': 'cal_probability.ipynb', 'repo_name': 'admond1994/calculate-probability-from-probability-density-plots', 'size': '4674'}