Menu iconMenu iconData Analysis Foundations with Python
Data Analysis Foundations with Python

Chapter 12: Hypothesis Testing

12.4 Practical Exercises Chapter 12: Hypothesis Testing

Exercise 1: Conducting a t-test

Question: You have been given the exam scores of a small class of 8 students before and after a coaching program. Conduct a paired t-test to find out if the coaching program made a significant impact on the scores.

Before Coaching: [55, 45, 67, 78, 49, 59, 61, 64]

After Coaching: [67, 53, 71, 85, 61, 66, 70, 80]

Solution:

from scipy import stats

before_coaching = [55, 45, 67, 78, 49, 59, 61, 64]
after_coaching = [67, 53, 71, 85, 61, 66, 70, 80]

# Conducting paired t-test
t_stat, p_value = stats.ttest_rel(before_coaching, after_coaching)
print(f't-statistic: {t_stat}, p-value: {p_value}')

Exercise 2: Performing One-Way ANOVA

Question: Three algorithms have been tested for their accuracy in classifying images, yielding the following results. Conduct a one-way ANOVA test to find out if the algorithms have different accuracies.

Algorithm A: [0.92, 0.88, 0.91, 0.87, 0.95]

Algorithm B: [0.77, 0.80, 0.76, 0.85, 0.81]

Algorithm C: [0.85, 0.89, 0.84, 0.88, 0.90]

Solution:

a = [0.92, 0.88, 0.91, 0.87, 0.95]
b = [0.77, 0.80, 0.76, 0.85, 0.81]
c = [0.85, 0.89, 0.84, 0.88, 0.90]

# Performing one-way ANOVA
f_stat, p_value = stats.f_oneway(a, b, c)
print(f'F-statistic: {f_stat}, p-value: {p_value}')

Exercise 3: Post-Hoc Analysis

Question: After conducting ANOVA as per Exercise 2, perform a post-hoc analysis to find out which pairs of algorithms are significantly different from each other.

Solution:

from statsmodels.stats.multicomp import pairwise_tukeyhsd

data = a + b + c
labels = ['A'] * len(a) + ['B'] * len(b) + ['C'] * len(c)

# Post-hoc comparison
tukey_result = pairwise_tukeyhsd(data, labels, 0.05)
print(tukey_result)

These exercises should give you a strong foundation for performing hypothesis tests and understanding their practical significance. Feel free to run the code, tweak the variables, and see how the results change. Happy learning!

12.4 Practical Exercises Chapter 12: Hypothesis Testing

Exercise 1: Conducting a t-test

Question: You have been given the exam scores of a small class of 8 students before and after a coaching program. Conduct a paired t-test to find out if the coaching program made a significant impact on the scores.

Before Coaching: [55, 45, 67, 78, 49, 59, 61, 64]

After Coaching: [67, 53, 71, 85, 61, 66, 70, 80]

Solution:

from scipy import stats

before_coaching = [55, 45, 67, 78, 49, 59, 61, 64]
after_coaching = [67, 53, 71, 85, 61, 66, 70, 80]

# Conducting paired t-test
t_stat, p_value = stats.ttest_rel(before_coaching, after_coaching)
print(f't-statistic: {t_stat}, p-value: {p_value}')

Exercise 2: Performing One-Way ANOVA

Question: Three algorithms have been tested for their accuracy in classifying images, yielding the following results. Conduct a one-way ANOVA test to find out if the algorithms have different accuracies.

Algorithm A: [0.92, 0.88, 0.91, 0.87, 0.95]

Algorithm B: [0.77, 0.80, 0.76, 0.85, 0.81]

Algorithm C: [0.85, 0.89, 0.84, 0.88, 0.90]

Solution:

a = [0.92, 0.88, 0.91, 0.87, 0.95]
b = [0.77, 0.80, 0.76, 0.85, 0.81]
c = [0.85, 0.89, 0.84, 0.88, 0.90]

# Performing one-way ANOVA
f_stat, p_value = stats.f_oneway(a, b, c)
print(f'F-statistic: {f_stat}, p-value: {p_value}')

Exercise 3: Post-Hoc Analysis

Question: After conducting ANOVA as per Exercise 2, perform a post-hoc analysis to find out which pairs of algorithms are significantly different from each other.

Solution:

from statsmodels.stats.multicomp import pairwise_tukeyhsd

data = a + b + c
labels = ['A'] * len(a) + ['B'] * len(b) + ['C'] * len(c)

# Post-hoc comparison
tukey_result = pairwise_tukeyhsd(data, labels, 0.05)
print(tukey_result)

These exercises should give you a strong foundation for performing hypothesis tests and understanding their practical significance. Feel free to run the code, tweak the variables, and see how the results change. Happy learning!

12.4 Practical Exercises Chapter 12: Hypothesis Testing

Exercise 1: Conducting a t-test

Question: You have been given the exam scores of a small class of 8 students before and after a coaching program. Conduct a paired t-test to find out if the coaching program made a significant impact on the scores.

Before Coaching: [55, 45, 67, 78, 49, 59, 61, 64]

After Coaching: [67, 53, 71, 85, 61, 66, 70, 80]

Solution:

from scipy import stats

before_coaching = [55, 45, 67, 78, 49, 59, 61, 64]
after_coaching = [67, 53, 71, 85, 61, 66, 70, 80]

# Conducting paired t-test
t_stat, p_value = stats.ttest_rel(before_coaching, after_coaching)
print(f't-statistic: {t_stat}, p-value: {p_value}')

Exercise 2: Performing One-Way ANOVA

Question: Three algorithms have been tested for their accuracy in classifying images, yielding the following results. Conduct a one-way ANOVA test to find out if the algorithms have different accuracies.

Algorithm A: [0.92, 0.88, 0.91, 0.87, 0.95]

Algorithm B: [0.77, 0.80, 0.76, 0.85, 0.81]

Algorithm C: [0.85, 0.89, 0.84, 0.88, 0.90]

Solution:

a = [0.92, 0.88, 0.91, 0.87, 0.95]
b = [0.77, 0.80, 0.76, 0.85, 0.81]
c = [0.85, 0.89, 0.84, 0.88, 0.90]

# Performing one-way ANOVA
f_stat, p_value = stats.f_oneway(a, b, c)
print(f'F-statistic: {f_stat}, p-value: {p_value}')

Exercise 3: Post-Hoc Analysis

Question: After conducting ANOVA as per Exercise 2, perform a post-hoc analysis to find out which pairs of algorithms are significantly different from each other.

Solution:

from statsmodels.stats.multicomp import pairwise_tukeyhsd

data = a + b + c
labels = ['A'] * len(a) + ['B'] * len(b) + ['C'] * len(c)

# Post-hoc comparison
tukey_result = pairwise_tukeyhsd(data, labels, 0.05)
print(tukey_result)

These exercises should give you a strong foundation for performing hypothesis tests and understanding their practical significance. Feel free to run the code, tweak the variables, and see how the results change. Happy learning!

12.4 Practical Exercises Chapter 12: Hypothesis Testing

Exercise 1: Conducting a t-test

Question: You have been given the exam scores of a small class of 8 students before and after a coaching program. Conduct a paired t-test to find out if the coaching program made a significant impact on the scores.

Before Coaching: [55, 45, 67, 78, 49, 59, 61, 64]

After Coaching: [67, 53, 71, 85, 61, 66, 70, 80]

Solution:

from scipy import stats

before_coaching = [55, 45, 67, 78, 49, 59, 61, 64]
after_coaching = [67, 53, 71, 85, 61, 66, 70, 80]

# Conducting paired t-test
t_stat, p_value = stats.ttest_rel(before_coaching, after_coaching)
print(f't-statistic: {t_stat}, p-value: {p_value}')

Exercise 2: Performing One-Way ANOVA

Question: Three algorithms have been tested for their accuracy in classifying images, yielding the following results. Conduct a one-way ANOVA test to find out if the algorithms have different accuracies.

Algorithm A: [0.92, 0.88, 0.91, 0.87, 0.95]

Algorithm B: [0.77, 0.80, 0.76, 0.85, 0.81]

Algorithm C: [0.85, 0.89, 0.84, 0.88, 0.90]

Solution:

a = [0.92, 0.88, 0.91, 0.87, 0.95]
b = [0.77, 0.80, 0.76, 0.85, 0.81]
c = [0.85, 0.89, 0.84, 0.88, 0.90]

# Performing one-way ANOVA
f_stat, p_value = stats.f_oneway(a, b, c)
print(f'F-statistic: {f_stat}, p-value: {p_value}')

Exercise 3: Post-Hoc Analysis

Question: After conducting ANOVA as per Exercise 2, perform a post-hoc analysis to find out which pairs of algorithms are significantly different from each other.

Solution:

from statsmodels.stats.multicomp import pairwise_tukeyhsd

data = a + b + c
labels = ['A'] * len(a) + ['B'] * len(b) + ['C'] * len(c)

# Post-hoc comparison
tukey_result = pairwise_tukeyhsd(data, labels, 0.05)
print(tukey_result)

These exercises should give you a strong foundation for performing hypothesis tests and understanding their practical significance. Feel free to run the code, tweak the variables, and see how the results change. Happy learning!