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 do I calculate variance using Python and SciPy?
- How can I use RK45 with Python and SciPy?
- How can I use Python Scipy to solve a Poisson equation?
- How do I install SciPy for Python?
- How can I use Python Scipy to zoom in on an image?
- How can I check if a certain version of Python is compatible with SciPy?
- How do I use Python Scipy to perform a Z test?
- How to use Python, XML-RPC, and NumPy together?
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
See more codes...