def mean(x): """Returns a mean value of some list of numeric values x ignoring NaN values. :param x: List of numeric values. :type x: list. :return: float """ check_numeric(x) return sum([i for i in x if not math.isnan(i)]) / len(x)
def data_range(x): """Counts data range of some data list x. :param x: List of numeric values. :type x: list. :return: float -- The required data range. """ check_numeric(x) return max(x) - min(x)
def variance(x): """Counts a variance :param x: List of numeric values. :type x: list. :return: float -- The required variance. """ check_numeric(x) m = mean(x) return sum([(d - m)**2 for d in x]) / len(x)
def mode(x): """Counts mode values :param x: List of numeric values. :type x: list. :return: list[float] """ check_numeric(x) counts = Counter(x) max_val = max(counts.values()) return [key for key, count in counts.items() if count == max_val]
def median(x): """Counts a median. :param x: List of numeric values. :type x: list. :return: float """ check_numeric(x) x_sorted = [i for i in sorted(x) if not math.isnan(i)] if len(x) % 2: return x_sorted[len(x) // 2] else: return (x_sorted[len(x) // 2] + x_sorted[len(x) // 2 - 1]) / 2
def dot(x, y): """Counts the dot product of two vectors :param x: First vector. :type x: list. :param y: Second vector. :type y: list. :return: float -- The required dot product. """ check_numeric(x) check_numeric(y) if len(x) == len(y): return sum([i * j for i, j in zip(x, y)]) raise ValueError('vectors must have equal length')
def quantile(x, p): """Counts a p-quantile of sample x. :param x: Sample (list of numeric values). :type x: list. :param p: Probability of the quantile. :type p: float. :return: float -- The required quantile. """ if p >= 1 or p < 0: raise ValueError('argument p should be in [0;1]') check_numeric(x) p_idx = int(p * len(x)) return sorted(x)[p_idx]
def make_buckets(x, bucket_size): """Divides the sample x into some buckets of size bucket_size. :param x: Sample (list of numeric values). :type x: list. :param bucket_size: Size of the bucket. :type bucket_size: int. :return: Counter -- x divided into buckets. """ check_numeric(x) return Counter([ bucket_size * math.floor(i / bucket_size) for i in x if not math.isnan(i) ])