Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An independent t-test with a possibility to visually confirm normal distribution of values
import matplotlib.pyplot as plt
import pandas as pd
from scipy.stats import ttest_ind
p_criterium = 0.01 # adjust for desired p-value
data_1 = [0,2,4,1,3,4,5,6,5,4,3,2] # insert array-like data
data_2 = [1,3,4,123,1234,3,2,3,4] # same here
data_1 = pd.Series(data_1)
data_2 = pd.Series(data_2)
mean_1 = data_1.mean()
mean_2 = data_2.mean()
f, axarr = plt.subplots(2, sharex=True)
axarr[0].hist(data_1, bins=100, histtype='step')
axarr[0].set_title('data_1')
axarr[0].axvline(mean_1, color='r')
axarr[0].set_ylabel('n')
axarr[0].annotate(xy=(mean_1, 0), s=mean_1)
axarr[1].hist(mean_2, bins=100, histtype='step')
axarr[1].set_title('data_2')
axarr[1].axvline(mean_2, color='r')
axarr[1].set_xlabel('values')
axarr[1].set_ylabel('n')
axarr[1].annotate(xy=(mean_2, 0), s=mean_2)
print("Please check visually for normal distribution:")
plt.show()
t, p = ttest_ind(data_1, data_2, equal_var=False)
print("mean 1: ", mean_1)
print("mean 2: ", mean_2)
print("t = ", t, ", p = ", p)
if p <= p_criterium:
print("Yay! p <= ", p_criterium)
else:
print("Nay :( p > ", p_criterium)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.