Independent Samples t-test

Using Scipy to compare two independent groups with t-test

Python
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

# Generate sample data
np.random.seed(42)
group1 = np.random.normal(loc=5, scale=1, size=100)
group2 = np.random.normal(loc=5.5, scale=1, size=100)

# Perform independent samples t-test
t_statistic, p_value = stats.ttest_ind(group1, group2)

print(f"T-statistic: {t_statistic}")
print(f"P-value: {p_value}")

# Visualize the data
plt.figure(figsize=(10, 6))
plt.boxplot([group1, group2], labels=['Group 1', 'Group 2'])
plt.title("Comparison of Two Groups")
plt.ylabel("Values")
plt.show()

# Interpret the results
alpha = 0.05
if p_value < alpha:
    print("Reject the null hypothesis: There is a significant difference between the groups.")
else:
    print("Fail to reject the null hypothesis: There is no significant difference between the groups.")