Two Sample T-Test using Python

This blog article shows you how to do two sample t-test using python. I got the python code from the link in my reference. In that article, the example first finds the variance. Unfortunately, the sample code is missing the np declaration. The code below added the line.

import numpy as np

import scipy.stats as stats

data_group1 = np.array([14, 15, 15, 16, 13, 8, 14,

                        17, 16, 14, 19, 20, 21, 15,

                        15, 16, 16, 13, 14, 12])

data_group2 = np.array([15, 17, 14, 17, 14, 8, 12,

                        19, 19, 14, 17, 22, 24, 16,

                        13, 16, 13, 18, 15, 13])

print(np.var(data_group1), np.var(data_group2))

You should get the variance if you run the code as follows.

The t-test code is good, but you may need to install pingouin using pip install like. pip install pingouin.

from statsmodels.stats.weightstats import ttest_ind

import numpy as np

import pingouin as pg

data_group1 = np.array([160, 150, 160, 156.12, 163.24,

                        160.56, 168.56, 174.12,

                        167.123, 165.12])

data_group2 = np.array([157.97, 146, 140.2, 170.15,

                        167.34, 176.123, 162.35, 159.123,

                        169.43, 148.123])

result = pg.ttest(data_group1,

                data_group2,

                correction=True)

print(result)

You should get the t-test if you run the code as follows.

You can download the Python code belong and test them.

Video: https://youtu.be/QjXJzEhRZIc?WT.mc_id=DP-MVP-36769

Source code download: https://github.com/chanmmn/python/tree/main/T-Test/?WT.mc_id=DP-MVP-36769

Reference: https://www.geeksforgeeks.org/how-to-conduct-a-two-sample-t-test-in-python/

Unknown's avatar

About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress (http://www.pcworld.com/article/206455/Microsoft_Live_Spaces_Moves_to_WordPress_An_FAQ.html) till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net, Cloud, Community, Computers and Internet, Data Platform, programming and tagged . Bookmark the permalink.

1 Response to Two Sample T-Test using Python

  1. Pingback: One sample T- Test using C# | Chanmingman's Blog

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.