Complete the code to calculate the mean of the data array.
import numpy as np data = np.array([5, 7, 8, 9, 10]) mean_value = np.[1](data) print(mean_value)
The np.mean() function calculates the average (mean) of the array elements.
Complete the code to calculate the standard error of the mean (SEM) for the data.
import numpy as np data = np.array([5, 7, 8, 9, 10]) sem = np.std(data, ddof=1) / np.sqrt([1]) print(sem)
The SEM is the standard deviation divided by the square root of the sample size, which is len(data).
Fix the error in the code to calculate a 95% confidence interval for the mean using scipy.stats.
from scipy import stats import numpy as np data = np.array([5, 7, 8, 9, 10]) confidence = 0.95 mean = np.mean(data) sem = stats.sem(data, ddof=1) margin = sem * stats.t.ppf((1 + confidence) / 2, df=[1]) ci_lower = mean - margin ci_upper = mean + margin print(ci_lower, ci_upper)
The degrees of freedom (df) for the t-distribution is the sample size minus 1, so len(data) - 1.
Fill both blanks to create a dictionary comprehension that maps words to their lengths only if the length is greater than 3.
words = ['apple', 'bat', 'carrot', 'dog', 'elephant'] lengths = {word: [1] for word in words if [2] print(lengths)
The dictionary comprehension maps each word to its length using len(word). The condition filters words with length greater than 3 using len(word) > 3.
Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if the length is greater than 4.
words = ['apple', 'bat', 'carrot', 'dog', 'elephant'] lengths = { [1]: [2] for word in words if [3] } print(lengths)
The dictionary comprehension uses word.upper() as keys, maps to len(word), and filters words with length greater than 4 using len(word) > 4.