The test will tell us whether means of the sample and the population are different
import pandas as pd
df = pd.read_csv("diabetes2.csv")
df.head()
age = df['Age']
import numpy as np
mean_age = np.mean(age)
print(mean_age)
np.random.seed(11)
sample_size = 10
sample_age = np.random.choice(age,sample_size)
sample_age
from scipy.stats import ttest_1samp
_,p_value = stats.ttest_1samp(a=sample_age,popmean=mean_age)
p_value
if p_value<0.05:
print("reject the null hypothesis")
else:
print("Accept the null hypothesis")
Consider the age of students in a college and in Class A
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)
_,p_value = stats.ttest_1samp(a=classA_ages,popmean=school_ages.mean())
p_value
if p_value<0.05:
print("Reject the null hypothesis")
else:
print("accept the null hypothesis")
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
np.random.seed(12)
ClassB_ages=stats.poisson.rvs(loc=18,mu=33,size=60)
ClassB_ages.mean()
_,p_value = stats.ttest_ind(a=classA_ages,b=ClassB_ages,equal_var=False)
p_value
if p_value<0.05:
print("Reject the null hypothesis")
else:
print("Accept the null hypothesis")
When you want to check how different samples from the same group are, you can go for a paired T-test
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)
print(weight1)
print(weight2)
np.mean(weight2)
weight_dataFrame = pd.DataFrame({"weight 1":np.array(weight1),
"weight 2":np.array(weight2),
"difference": np.array(weight2)-np.array(weight1)})
weight_dataFrame
_,p_value = stats.ttest_rel(a = weight1,b = weight2)
p_value
if p_value<0.05:
print("reject null hypothesis")
else:
print("accept null hypothesis")