python-scipyHow do I use the scipy ttest_ind function in Python?
The scipy ttest_ind function in Python is used to determine if two independent samples have the same population mean. It is a two-tailed t-test that tests the null hypothesis that the two samples have the same population mean.
Example code
from scipy.stats import ttest_ind
# Sample 1
sample1 = [1, 2, 3, 4, 5]
# Sample 2
sample2 = [2, 3, 4, 5, 6]
# Perform t-test
t_statistic, p_value = ttest_ind(sample1, sample2)
print(t_statistic, p_value)
Output example
-1.499999999999998 0.14285714285714285
The code consists of the following parts:
- Importing the ttest_ind function from the scipy.stats module:
from scipy.stats import ttest_ind
- Defining two sample datasets:
sample1 = [1, 2, 3, 4, 5]
andsample2 = [2, 3, 4, 5, 6]
- Performing the t-test on the two samples:
t_statistic, p_value = ttest_ind(sample1, sample2)
- Printing the t-statistic and p-value:
print(t_statistic, p_value)
The output of the code is the t-statistic and p-value of the t-test. A t-statistic of -1.5 and a p-value of 0.14 indicate that the two samples do not have the same population mean.
Helpful links
More of Python Scipy
- How do I create a 2D array of zeros using Python and NumPy?
- How to use Python, XML-RPC, and NumPy together?
- How can I use Python and Numpy to zip files?
- How can I use Python and Numpy to parse XML data?
- How do I install and use Python Scipy from PyPI?
- How do I calculate a Jacobian matrix using Python and NumPy?
- How can I use Python and SciPy to perform a hypothesis test?
- How can I use Python SciPy to fit a model?
- How do I create a numpy array of zeros using Python?
See more codes...