Bird
Raised Fist0
Data Analysis Pythondata~10 mins

t-test with scipy.stats in Data Analysis Python - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the t-test function from scipy.stats.

Data Analysis Python
from scipy.stats import [1]
Drag options to blanks, or click blank then click option'
Attest_ind
Bttest_1samp
Cttest_rel
Dttest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ttest' which is not a valid function in scipy.stats.
Confusing paired and independent t-test functions.
2fill in blank
medium

Complete the code to perform an independent t-test on samples a and b.

Data Analysis Python
result = ttest_ind([1], b)
Drag options to blanks, or click blank then click option'
Aa
Bb
Csample
Dresult
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the same sample twice.
Passing the result variable as input.
3fill in blank
hard

Fix the error in the code to correctly perform a one-sample t-test on sample data.

Data Analysis Python
from scipy.stats import ttest_1samp

sample = [2, 3, 5, 6, 9]
result = ttest_1samp(sample, [1])
Drag options to blanks, or click blank then click option'
A0
BNone
C5
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Passing None or zero instead of the population mean.
Confusing the sample data with the population mean.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that stores t-statistics for samples with mean greater than 4.

Data Analysis Python
t_stats = {name: result.statistic for name, result in results.items() if result.statistic [1] 4 and result.pvalue [2] 0.05}
Drag options to blanks, or click blank then click option'
A>
B<
C>=
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators that reverse the logic.
Mixing up p-value and t-statistic conditions.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension filtering samples with mean less than 3 and p-value less than 0.01.

Data Analysis Python
filtered = {name: result.pvalue for name, result in results.items() if result.statistic [1] 3 and result.pvalue [2] 0.01 and name.[3]('test')}
Drag options to blanks, or click blank then click option'
A<
B<=
Cstartswith
Dendswith
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators.
Using 'endswith' instead of 'startswith' for name filtering.

Practice

(1/5)
1.

What does a t-test help you find out in data analysis?

easy
A. If two groups have different average values
B. The total number of data points in a dataset
C. The exact values of all data points
D. How to sort data in ascending order

Solution

  1. Step 1: Understand the purpose of a t-test

    A t-test compares the means (averages) of two groups to see if they are different.
  2. Step 2: Identify what the t-test does not do

    It does not count data points, show exact values, or sort data.
  3. Final Answer:

    If two groups have different average values -> Option A
  4. Quick Check:

    T-test checks mean difference = A [OK]
Hint: T-test compares averages between groups [OK]
Common Mistakes:
  • Thinking t-test counts data points
  • Confusing t-test with sorting methods
  • Assuming t-test shows exact data values
2.

Which of the following is the correct way to import the t-test function for independent samples from scipy.stats?

?
easy
A. from scipy.stats import ttest_ind
B. import scipy.stats.ttest_ind
C. from scipy import ttest_ind
D. import ttest_ind from scipy.stats

Solution

  1. Step 1: Recall Python import syntax

    To import a specific function, use 'from module import function'.
  2. Step 2: Match the correct import statement

    'from scipy.stats import ttest_ind' correctly imports the independent t-test function.
  3. Final Answer:

    from scipy.stats import ttest_ind -> Option A
  4. Quick Check:

    Correct import syntax = A [OK]
Hint: Use 'from module import function' to import specific functions [OK]
Common Mistakes:
  • Using 'import scipy.stats.ttest_ind' which is invalid
  • Trying 'from scipy import ttest_ind' missing 'stats'
  • Writing 'import ttest_ind from scipy.stats' which is wrong syntax
3.

What will be the output of this code snippet?

import numpy as np
from scipy.stats import ttest_1samp

data = np.array([5, 7, 8, 6, 9])
test_stat, p_value = ttest_1samp(data, popmean=6)
print(round(p_value, 3))
medium
A. 0.045
B. 0.100
C. 0.500
D. 0.317

Solution

  1. Step 1: Understand the t-test 1 sample function

    ttest_1samp tests if the sample mean differs from the population mean (6 here).
  2. Step 2: Calculate the p-value for the data

    Using the data, the p-value is approximately 0.317, meaning no strong evidence against mean=6.
  3. Final Answer:

    0.317 -> Option D
  4. Quick Check:

    p-value from ttest_1samp = 0.317 [OK]
Hint: Round p-value to 3 decimals to compare easily [OK]
Common Mistakes:
  • Confusing test statistic with p-value
  • Using wrong population mean in function
  • Not rounding p-value before comparing
4.

Identify the error in this code that tries to perform a paired t-test:

from scipy.stats import ttest_rel

data1 = [10, 12, 14, 16]
data2 = [11, 13, 15]

result = ttest_rel(data1, data2)
print(result.pvalue)
medium
A. Missing import for numpy
B. ttest_rel is not the correct function for paired tests
C. The two data lists have different lengths
D. print statement syntax is incorrect

Solution

  1. Step 1: Check input data lengths for paired t-test

    Paired t-test requires both data arrays to have the same length because it compares pairs.
  2. Step 2: Identify the mismatch in data lengths

    data1 has 4 elements, data2 has 3 elements, causing an error.
  3. Final Answer:

    The two data lists have different lengths -> Option C
  4. Quick Check:

    Paired t-test needs equal length arrays = C [OK]
Hint: Paired t-test needs equal length data arrays [OK]
Common Mistakes:
  • Using different length arrays for paired test
  • Confusing ttest_rel with independent t-test
  • Assuming print syntax error without checking data
5.

You have two groups of exam scores:

group1 = [78, 85, 90, 88, 76]
group2 = [82, 79, 88, 91, 77]

You want to check if their average scores differ significantly. Which code correctly performs this test and interprets the result?

hard
A.
from scipy.stats import ttest_ind
stat, p = ttest_ind(group1, group2)
if p > 0.05:
    print('Different averages')
else:
    print('No difference')
B.
from scipy.stats import ttest_ind
stat, p = ttest_ind(group1, group2)
if p < 0.05:
    print('Different averages')
else:
    print('No difference')
C.
from scipy.stats import ttest_rel
stat, p = ttest_rel(group1, group2)
if p < 0.01:
    print('Different averages')
else:
    print('No difference')
D.
from scipy.stats import ttest_1samp
stat, p = ttest_1samp(group1, group2)
if p < 0.05:
    print('Different averages')
else:
    print('No difference')

Solution

  1. Step 1: Choose the correct t-test for two independent groups

    ttest_ind is used for comparing two independent samples like group1 and group2.
  2. Step 2: Interpret the p-value correctly

    If p-value is less than 0.05, we say averages differ; otherwise, no difference.
  3. Final Answer:

    Use ttest_ind and check if p < 0.05 to decide difference -> Option B
  4. Quick Check:

    Independent t-test + p < 0.05 means difference [OK]
Hint: Use ttest_ind for independent groups and p < 0.05 for difference [OK]
Common Mistakes:
  • Using ttest_1samp for two groups
  • Using ttest_rel for independent groups
  • Reversing p-value condition for significance
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard