<jupyter_start><jupyter_text>Initial analysis on the reason for absence<jupyter_code># get the number of entries for each reason for absence
plt.figure(figsize=(10, 5))
ax = sns.countplot(data=preprocessed_data, x="Reason for absence")
ax.set_ylabel("Number of entries per reason of absence")
plt.savefig('figs/absence_reasons_distribution.png', format='png', dpi=300)<jupyter_output><empty_output><jupyter_text>Social drinkers and smokers analysis<jupyter_code># plot reasons for absence against being a social drinker/smoker
plt.figure(figsize=(8, 6))
sns.countplot(data=preprocessed_data, x="Reason for absence", 
              hue="Social drinker", hue_order=["Yes", "No"])
plt.savefig('figs/absence_reasons_drinkers.png', format='png', dpi=300)

plt.figure(figsize=(8, 6))
sns.countplot(data=preprocessed_data, x="Reason for absence", 
              hue="Social smoker", hue_order=["Yes", "No"])
plt.savefig('figs/absence_reasons_smokers.png', format='png', dpi=300)
print(preprocessed_data["Social drinker"].value_counts(normalize=True))
print(preprocessed_data["Social smoker"].value_counts(normalize=True))
# computation of conditional probability
sample_space = set(["BB", "BG", "GB", "GG"])
event_a = set(["BB"])
event_b = set(["BB", "BG", "GB"])

cond_prob = (0.25*len(event_a.intersection(event_b))) / (0.25*len(event_b))
print(round(cond_prob, 4))
# compute probabilities of being a drinker and smoker
drinker_prob = preprocessed_data["Social drinker"]\
    .value_counts(normalize=True)["Yes"]
smoker_prob = preprocessed_data["Social smoker"]\
    .value_counts(normalize=True)["Yes"]
print(f"P(social drinker) = {drinker_prob:.3f} | P(social smoker) = {smoker_prob:.3f}")

# create mask for social drinkers/smokers
drinker_mask = preprocessed_data["Social drinker"] == "Yes"
smoker_mask = preprocessed_data["Social smoker"] == "Yes"

# compute probabilities of absence reasons and being a social drinker/smoker
total_entries = preprocessed_data.shape[0]
absence_drinker_prob = preprocessed_data["Reason for absence"]\
    [drinker_mask].value_counts()/total_entries
absence_smoker_prob = preprocessed_data["Reason for absence"]\
    [smoker_mask].value_counts()/total_entries

# compute conditional probabilities
cond_prob = pd.DataFrame(index=range(0,29))
cond_prob["P(Absence | social drinker)"] = absence_drinker_prob/drinker_prob
cond_prob["P(Absence | social smoker)"] = absence_smoker_prob/smoker_prob

# plot probabilities
plt.figure()
ax = cond_prob.plot.bar(figsize=(10,6))
ax.set_ylabel("Conditional probability")
plt.savefig('figs/conditional_probabilities.png', format='png', dpi=300)<jupyter_output>P(social drinker) = 0.568 | P(social smoker) = 0.073<jupyter_text>**Exercise 2.03: Identifying probability of being drinker/smoker, conditioned to absence reason**<jupyter_code># compute reason for absence probabilities
absence_prob = preprocessed_data["Reason for absence"].value_counts(normalize=True)
# compute conditional probabilities for drinker/smoker
cond_prob_drinker_smoker = pd.DataFrame(index=range(0,29))
cond_prob_drinker_smoker["P(social drinker | Absence)"] = \
    cond_prob["P(Absence | social drinker)"]*drinker_prob/absence_prob
cond_prob_drinker_smoker["P(social smoker | Absence)"] = \
    cond_prob["P(Absence | social smoker)"]*smoker_prob/absence_prob

plt.figure()
ax = cond_prob_drinker_smoker.plot.bar(figsize=(10,6))
ax.set_ylabel("Conditional probability")
plt.savefig('figs/conditional_probabilities_drinker_smoker.png', format='png', dpi=300)
# create violin plots of the absenteeism time in hours
plt.figure(figsize=(8,6))
sns.violinplot(x="Social drinker", y="Absenteeism time in hours", \
                   data=preprocessed_data, order=["No", "Yes"])
plt.savefig('figs/drinkers_hour_distribution.png', format='png', dpi=300)

plt.figure(figsize=(8,6))
sns.violinplot(x="Social smoker", y="Absenteeism time in hours", \
               data=preprocessed_data, order=["No", "Yes"])
plt.savefig('figs/smokers_hour_distribution.png', format='png', dpi=300)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13992583436341163, 'fraction_numerical': 0.016069221260815822, 'mean_word_length': 7.342268041237113, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '252151', 'license': 'MIT', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1370, 'n_words': 263, 'path': 'Chapter02/Exercise2.03/Exercise2.03.ipynb', 'repo_name': 'ivalderrama/packt_data_analysis_workshop', 'size': '4045'}