T-test — one Sample, Two Sample and paired T-test using python

Yogesh Khangode
5 min readApr 23, 2021

What is a T-Test?

A t-test is a type of inferential statistic used to determine if there is a significant difference between the means of two groups, which may be related in certain features. A t-test is used as a hypothesis testing tool, which allows testing of an assumption applicable to a population.

A t-test looks at the t-statistic, the t-distribution values, and the degrees of freedom to determine the statistical significance. To conduct a test with three or more means, one must use an analysis of variance.

Example:-

It is mostly used when the data sets, like the data set recorded as the outcome from flipping a coin 100 times, would follow a normal distribution and may have unknown variances.

Explaining the T-Test

A t-test allows us to compare the average values of the two data sets and determine if they came from the same population.

if we were to take a sample of students from class A and another sample of students from class B.

we would not expect them to have exactly the same mean and standard deviation.

Mathematically,
The t-test takes a sample from each of the two sets and establishes the problem statement by assuming a null hypothesis that the two means are equal.

Based on the applicable formulas,

Certain values are calculated and compared against the standard values, and the assumed null hypothesis is accepted or rejected accordingly.

If the null hypothesis qualifies to be rejected, it indicates that data readings are strong and are probably not due to chance. The t-test is just one of many tests used for this purpose.

Ambiguous Test Results

In a school, 100 students in class A scored an average of 85% with a standard deviation of 3%.

Another 100 students belonging to class B scored an average of 87% with a standard deviation of 4%.

While the average of class B is better than that of class A, it may not be correct to jump to the conclusion that the overall performance of students in class B is better than that of students in class A.

This is because there is natural variability in the test scores in both classes, so the difference could be due to chance alone. A t-test can help to determine whether one class fared better than the other.

Calculating T-Tests

Step 1

Calculating a t-test requires three key data values:-

They include the difference between the mean values from each data set (called the mean difference), the standard deviation of each group, and the number of data values of each group.

Step 2

The outcome of the t-test produces the t-value. This calculated t-value is then compared against a value obtained from a critical value table (called the T-Distribution Table).

Important point

This comparison helps to determine the effect of chance alone on the difference, and whether the difference is outside that chance range.

Practical

One-Sample T-test using python

Step 1 : Import library to required complete a task

Step 2 : taken any data of number and find the mean of all number store in a variable.

Step 3: Select Random 10 any Sample and store in a variable.

Step 4: import library for T-test for one Sample.

Step 5: let’s pass the parameter in ttest1_samp( , )

Step 6: Check Null Hypothesis is Rejected or not

Example 2 for Practice

Two-sample T-test using Python

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.

Paired T-test using Python

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

Step 1 : Taken data to test paired T-test

Step 2 : Convert into DataFrame

Step 3 : let’s pass the parameter in stats.ttest_rel(a=weight1,b=weight2)

Step 4: Check Null Hypothesis is Rejected or not

Gitup link:- Yogi776/Statistic-implementation-using-Python (github.com)

LinkedIn :- Yogesh Khangode

--

--