One-sample T-test with Python

The test will tell us whether means of the sample and the population are different

In [58]:
import pandas as pd
In [59]:
df = pd.read_csv("diabetes2.csv")
In [60]:
df.head()
Out[60]:
Pregnancies Glucose BloodPressure SkinThickness Insulin BMI DiabetesPedigreeFunction Age Outcome
0 6 148 72 35 0 33.6 0.627 50 1
1 1 85 66 29 0 26.6 0.351 31 0
2 8 183 64 0 0 23.3 0.672 32 1
3 1 89 66 23 94 28.1 0.167 21 0
4 0 137 40 35 168 43.1 2.288 33 1
In [62]:
age = df['Age']
In [63]:
import numpy as np
mean_age = np.mean(age)
print(mean_age)
33.240885416666664
In [64]:
np.random.seed(11)
sample_size = 10
sample_age = np.random.choice(age,sample_size)
In [65]:
sample_age
Out[65]:
array([41, 22, 34, 44, 28, 41, 22, 42, 28, 36], dtype=int64)
In [66]:
from scipy.stats import ttest_1samp
_,p_value = stats.ttest_1samp(a=sample_age,popmean=mean_age)
In [67]:
p_value
Out[67]:
0.8367793985047209
In [68]:
if p_value<0.05:
    print("reject the null hypothesis")
else:
    print("Accept the null hypothesis")
Accept the null hypothesis

Some more Examples

Consider the age of students in a college and in Class A

In [ ]:
import numpy as np
import pandas as pd
import scipy.stats as stats
import math
school_ages=stats.poisson.rvs(loc=18,mu=35,size=1500)
classA_ages=stats.poisson.rvs(loc=18,mu=30,size=60)
In [19]:
_,p_value = stats.ttest_1samp(a=classA_ages,popmean=school_ages.mean())
In [20]:
p_value
Out[20]:
1.2742764586221279e-06
In [14]:
if p_value<0.05:
    print("Reject the null hypothesis")
else:
    print("accept the null hypothesis")
Reject the null hypothesis

Two Sample T-test

The Independent Samples t Test or 2-sample t-test compares the means of two independent groups in order to determine whether there is statistical evidence that the associated population means are significantly different. The Independent Samples t Test is a parametric test. This test is also known as: Independent t Test

In [33]:
np.random.seed(12)
ClassB_ages=stats.poisson.rvs(loc=18,mu=33,size=60)
In [34]:
ClassB_ages.mean()
Out[34]:
50.63333333333333
In [35]:
_,p_value = stats.ttest_ind(a=classA_ages,b=ClassB_ages,equal_var=False)
In [36]:
p_value
Out[36]:
0.06846378853728861
In [37]:
if p_value<0.05:
    print("Reject the null hypothesis")
else:
    print("Accept the null hypothesis")
Accept the null hypothesis

Paired T-test With Python

When you want to check how different samples from the same group are, you can go for a paired T-test

In [43]:
np.random.seed(10)
weight1=[25,30,28,35,28,34,26,29,30,26,28,32,31,30,45]
weight2 = weight1 + stats.norm.rvs(size = 15)
In [44]:
print(weight1)
print(weight2)
[25, 30, 28, 35, 28, 34, 26, 29, 30, 26, 28, 32, 31, 30, 45]
[26.3315865  30.71527897 26.45459971 34.99161615 28.62133597 33.27991444
 26.26551159 29.10854853 30.00429143 25.82539979 28.43302619 33.20303737
 30.03493433 31.02827408 45.22863013]
In [45]:
np.mean(weight2)
Out[45]:
30.63506567885095
In [51]:
weight_dataFrame = pd.DataFrame({"weight 1":np.array(weight1),
                                 "weight 2":np.array(weight2),
                                 "difference": np.array(weight2)-np.array(weight1)})
In [52]:
weight_dataFrame
Out[52]:
weight 1 weight 2 difference
0 25 26.331587 1.331587
1 30 30.715279 0.715279
2 28 26.454600 -1.545400
3 35 34.991616 -0.008384
4 28 28.621336 0.621336
5 34 33.279914 -0.720086
6 26 26.265512 0.265512
7 29 29.108549 0.108549
8 30 30.004291 0.004291
9 26 25.825400 -0.174600
10 28 28.433026 0.433026
11 32 33.203037 1.203037
12 31 30.034934 -0.965066
13 30 31.028274 1.028274
14 45 45.228630 0.228630
In [55]:
_,p_value = stats.ttest_rel(a = weight1,b = weight2)
In [56]:
p_value
Out[56]:
0.42809382105357063
In [57]:
if p_value<0.05:
    print("reject null hypothesis")
else:
    print("accept null hypothesis")
accept null hypothesis