Spaces:
Runtime error
Runtime error
import numpy as np
#5
by
lvwerra
HF staff
- opened
Question:
import numpy as np
Answer:
import numpy as np def compute_variance(data): """Returns the variance of the given data.""" avg = compute_mean(data) variance = sum([(x-avg)**2 for x in data])/float(len(data)-1) return variance def compute_skewness(data): """Returns the skewness of the given data.""" avg = compute_mean(data) sigma = compute_standard_deviation(data) numerator = sum([(x-avg)**3 for x in data])/sigma**3 denominator = sum([(x-avg)**2 for x in data])/sigma**2 skewness = numerator/denominator**1.5 return skewness def compute_kurtosis(data): """Returns the kurtosis of the given data.""" avg = compute_mean(data) sigma = compute_standard_deviation(data) numerator = sum([(x-avg)**4 for x in data])/sigma**4 denominator = sum([(x-avg)**2 for x in data])/sigma**2 kurtosis = numerator/denominator**2 return kurtosis def compute_entropy(data): """Returns the entropy of the given data.""" pmf = {k:v/float(len(data)) for k, v in Counter(data).items()} log_pmf = {k:-math.log(v) for k, v in pmf.items()} total_prob = sum(pmf.values()) entropy = sum([-p*log_pmf[p]/total_prob for p in pmf.keys()]) return entropy def compute_iqr(data): """Returns the interquartile range of the given data.""" q75, q25 = np.percentile(data, [75, 25]) iqr = q75 - q25 return iqr def compute_outliers(data): """Returns the outlier values of the given data.""" q75, q25 = np.percentile(data, [75, 25]) lower_bound = q25-(1.5*(q75-q25)) upper_bound = q75+(1.5*(q75-q25)) return list(filter(lambda x: x < lower_bound or x > upper_bound, data)) def compute_zscore(value, mean, sd): """Returns the z score of the given value compared to the mean and SD.""" return (value - mean)/(sd * math.sqrt(2)) def compute_confidence_interval(data, confidence=95): """Returns the confidence interval around the given data.""" n = len(data) m = compute_mean(data) s = compute_standard_deviation(data) h = norm.ppf((100-confidence)/100.) * s / math.sqrt(n) return m-h, m+h <|endoftext|>