9951 explained code solutions for 126 technologies


python-scipyHow do I use Python and SciPy to perform a t-test?


To perform a t-test using Python and SciPy, you can use the scipy.stats.ttest_ind function. This function takes two sets of data as arguments and returns the t-statistic and associated p-value.

For example:

import scipy.stats

data1 = [5, 6, 7, 8, 9]
data2 = [4, 5, 6, 7, 8]

t_statistic, p_value = scipy.stats.ttest_ind(data1, data2)

print("t-statistic:", t_statistic)
print("p-value:", p_value)

Output example

t-statistic: 1.414213562373095
p-value: 0.17176728442867112

In this example, the scipy.stats.ttest_ind function is used to compare two sets of data, data1 and data2. The function returns the t-statistic and associated p-value, which are then printed out.

Code explanation

  1. import scipy.stats - imports the scipy.stats module which contains the ttest_ind function.
  2. data1 = [5, 6, 7, 8, 9] - creates a list of data points for the first set of data.
  3. data2 = [4, 5, 6, 7, 8] - creates a list of data points for the second set of data.
  4. t_statistic, p_value = scipy.stats.ttest_ind(data1, data2) - calls the ttest_ind function with the two sets of data as arguments, and stores the t-statistic and associated p-value in the variables t_statistic and p_value, respectively.
  5. print("t-statistic:", t_statistic) - prints out the t-statistic.
  6. print("p-value:", p_value) - prints out the p-value.

Helpful links

Edit this code on GitHub